All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.commons.locale.country.CountryCache Maven / Gradle / Ivy

/**
 * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * 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.helger.commons.locale.country;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.CGlobal;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.annotation.Singleton;
import com.helger.commons.collection.CollectionHelper;
import com.helger.commons.locale.LocaleCache;
import com.helger.commons.locale.LocaleHelper;
import com.helger.commons.state.EChange;
import com.helger.commons.string.StringHelper;

/**
 * This is a global cache for country objects to avoid too many object flowing
 * around.
* This cache is application independent. * * @author Philip Helger */ @ThreadSafe @Singleton public final class CountryCache { private static final class SingletonHolder { static final CountryCache s_aInstance = new CountryCache (); } private static final Logger s_aLogger = LoggerFactory.getLogger (CountryCache.class); private static boolean s_bDefaultInstantiated = false; private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock (); /** Contains all known countries (as ISO 3166 2-letter codes). */ private final Set m_aCountries = new HashSet (); private CountryCache () { reinitialize (); } public static boolean isInstantiated () { return s_bDefaultInstantiated; } @Nonnull public static CountryCache getInstance () { final CountryCache ret = SingletonHolder.s_aInstance; s_bDefaultInstantiated = true; return ret; } @Nonnull EChange addCountry (@Nonnull final String sCountry) { ValueEnforcer.notNull (sCountry, "Country"); final String sValidCountry = LocaleHelper.getValidCountryCode (sCountry); if (sValidCountry == null) throw new IllegalArgumentException ("illegal country code '" + sCountry + "'"); if (!sCountry.equals (sValidCountry)) throw new IllegalArgumentException ("invalid casing of '" + sCountry + "'"); m_aRWLock.writeLock ().lock (); try { return EChange.valueOf (m_aCountries.add (sValidCountry)); } finally { m_aRWLock.writeLock ().unlock (); } } @Nullable public Locale getCountry (@Nullable final Locale aCountry) { return aCountry == null ? null : getCountry (aCountry.getCountry ()); } @Nullable public Locale getCountry (@Nullable final String sCountry) { if (StringHelper.hasNoText (sCountry)) return null; // Was something like "_AT" (e.g. the result of getCountry (...).toString // ()) passed in? -> indirect recursion if (sCountry.indexOf (CGlobal.LOCALE_SEPARATOR) >= 0) return getCountry (LocaleCache.getInstance ().getLocale (sCountry)); final String sValidCountry = LocaleHelper.getValidCountryCode (sCountry); if (!containsCountry (sValidCountry)) s_aLogger.warn ("Trying to retrieve unsupported country " + sCountry); return LocaleCache.getInstance ().getLocale ("", sValidCountry, ""); } /** * @return a set with all contained countries. Never null. */ @Nonnull @ReturnsMutableCopy public Set getAllCountries () { m_aRWLock.readLock ().lock (); try { return CollectionHelper.newSet (m_aCountries); } finally { m_aRWLock.readLock ().unlock (); } } /** * @return a set with all contained country locales. Never null. */ @Nonnull @ReturnsMutableCopy public Set getAllCountryLocales () { final Set ret = new HashSet (); for (final String sCountry : getAllCountries ()) ret.add (LocaleCache.getInstance ().getLocale ("", sCountry, "")); return ret; } /** * Check if the passed country is known. * * @param aCountry * The country to check. May be null. * @return true if the passed country is contained, * false otherwise. */ public boolean containsCountry (@Nullable final Locale aCountry) { return aCountry != null && containsCountry (aCountry.getCountry ()); } /** * Check if the passed country is known. * * @param sCountry * The country to check. May be null. * @return true if the passed country is contained, * false otherwise. */ public boolean containsCountry (@Nullable final String sCountry) { if (sCountry == null) return false; final String sValidCountry = LocaleHelper.getValidCountryCode (sCountry); if (sValidCountry == null) return false; m_aRWLock.readLock ().lock (); try { return m_aCountries.contains (sValidCountry); } finally { m_aRWLock.readLock ().unlock (); } } /** * Reset the cache to the initial state. */ public void reinitialize () { m_aRWLock.writeLock ().lock (); try { m_aCountries.clear (); } finally { m_aRWLock.writeLock ().unlock (); } for (final Locale aLocale : LocaleCache.getInstance ().getAllLocales ()) { final String sCountry = aLocale.getCountry (); if (StringHelper.hasText (sCountry)) addCountry (sCountry); } if (s_aLogger.isDebugEnabled ()) s_aLogger.debug ("Reinitialized " + CountryCache.class.getName ()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy