OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.i18n;

import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class TranslationPool<T, X extends Exception> {

    static public enum Mode {
        /**
         * Try to create if needed, but allow <code>null</code>.
         */
        OPTIONAL_CREATE(true, true),
        /**
         * Try to create if needed, but disallow <code>null</code>.
         */
        GET_OR_CREATE(true, false),
        /**
         * The item must already exist.
         */
        GET_CREATED(false, false);

        private final boolean allowCreation, allowNull;

        private Mode(boolean allowCreation, boolean allowNull) {
            this.allowCreation = allowCreation;
            this.allowNull = allowNull;
        }
    }

    // it's rare to load many Locale
    private final Map<Locale, T> byLocale = new HashMap<>(8);
    private final Function<Locale, T> createInstance;

    public TranslationPool() {
        this(null);
    }

    public TranslationPool(final Function<Locale, T> createInstance) {
        this.createInstance = createInstance;
    }

    protected T createTM(final Locale l) throws X {
        return this.createInstance.apply(l);
    }

    public final T get(final Locale l) throws X {
        return this.get(l, Mode.GET_OR_CREATE);
    }

    public final T get(final Locale l, final Mode mode) throws X {
        Objects.requireNonNull(l, "Missing locale");
        Objects.requireNonNull(mode, "Missing mode");
        T res;
        synchronized (this) {
            res = this.byLocale.get(l);
            if (res == null && mode.allowCreation) {
                res = this.createTM(l);
                if (res != null)
                    this.byLocale.put(l, res);
            }
            if (res == null && !mode.allowNull)
                throw new IllegalStateException("Missing instance for " + l);
        }
        return res;
    }

    public final T getCreated(final Locale l) {
        // Don't call get(Locale, Mode) since it throws a generic exception that can't be caught
        Objects.requireNonNull(l, "Missing locale");
        synchronized (this) {
            final T res = this.byLocale.get(l);
            if (res == null)
                throw new IllegalStateException("Missing instance for " + l);
            return res;
        }
    }

    final Collection<T> getCreated() {
        assert Thread.holdsLock(this);
        return this.byLocale.values();
    }
}