com.ovea.i18n.I18NServiceSkeleton Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Ovea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ovea.i18n;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author Mathieu Carbou ([email protected])
*/
public abstract class I18NServiceSkeleton implements I18NService {
public static enum MissingKeyBehaviour {
THROW_EXCEPTION,
RETURN_NULL,
RETURN_KEY
}
private final ConcurrentMap cache = new ConcurrentHashMap();
private final String bundleName;
private final ClassLoader classLoader;
private boolean debug;
private MissingKeyBehaviour missingKeyBehaviour = MissingKeyBehaviour.THROW_EXCEPTION;
protected I18NServiceSkeleton(String bundleName, ClassLoader classLoader) {
this.bundleName = bundleName.startsWith("/") ? bundleName.substring(1) : bundleName;
this.classLoader = classLoader;
}
public MissingKeyBehaviour getMissingKeyBehaviour() {
return missingKeyBehaviour;
}
public void setMissingKeyBehaviour(MissingKeyBehaviour missingKeyBehaviour) {
this.missingKeyBehaviour = missingKeyBehaviour;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
@Override
public final I18NBundle forLocale(Locale locale) {
I18NBundle bundle = cache.get(locale);
if (bundle == null) {
cache.putIfAbsent(locale, newBundle(bundleName, classLoader, locale));
bundle = cache.get(locale);
}
return bundle;
}
@Override
public final String toString() {
return bundleName;
}
abstract I18NBundle newBundle(String bundleName, ClassLoader classLoader, Locale locale);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy