Author: julien_viet
Date: 2009-12-17 12:06:31 -0500 (Thu, 17 Dec 2009)
New Revision: 1052
Removed:
components/common/trunk/common/src/main/java/org/gatein/common/i18n/LocaleManager.java
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/i18n/DefaultLocaleFormat.java
Log:
GTNPORTAL-8 : Remove unnecessary blocking method call (2s)
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/i18n/DefaultLocaleFormat.java
===================================================================
---
components/common/trunk/common/src/main/java/org/gatein/common/i18n/DefaultLocaleFormat.java 2009-12-17
15:23:11 UTC (rev 1051)
+++
components/common/trunk/common/src/main/java/org/gatein/common/i18n/DefaultLocaleFormat.java 2009-12-17
17:06:31 UTC (rev 1052)
@@ -27,7 +27,6 @@
import java.io.IOException;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -40,20 +39,9 @@
class DefaultLocaleFormat extends AbstractLocaleFormat
{
- /** . */
- private static final Map<String, Locale> CACHE = new HashMap<String,
Locale>();
+ /** A copy on write cache (yeah not volatile, so what?). */
+ private Map<String, Locale> CACHE = new HashMap<String, Locale>();
- static
- {
- for (Iterator<Locale> i = LocaleManager.getLocales().iterator();
i.hasNext();)
- {
- Locale locale = (Locale)i.next();
-
- //
- CACHE.put(locale.toString(), locale);
- }
- }
-
/** . */
private LocaleFactory factory;
@@ -69,7 +57,7 @@
protected Locale internalGetLocale(String value) throws FormatConversionException
{
- Locale locale = (Locale)CACHE.get(value);
+ Locale locale = CACHE.get(value);
if (locale != null)
{
return locale;
@@ -79,38 +67,44 @@
int p1 = value.lastIndexOf('_');
if (p1 < 0)
{
- return factory.createLocale(value);
+ locale = factory.createLocale(value);
}
-
- //
- String a = (p1 == (value.length() - 1)) ? "" : value.substring(p1 + 1,
value.length());
-
- //
- int p2 = value.lastIndexOf('_', p1 - 1);
- if (p2 < 0)
+ else
{
- if (a.length() == 0)
+ String a = (p1 == (value.length() - 1)) ? "" : value.substring(p1 + 1,
value.length());
+ int p2 = value.lastIndexOf('_', p1 - 1);
+ if (p2 < 0)
{
- throw new FormatConversionException();
+ if (a.length() == 0)
+ {
+ throw new FormatConversionException();
+ }
+ else
+ {
+ locale = factory.createLocale(value.substring(0, p1), a);
+ }
}
else
{
- return factory.createLocale(value.substring(0, p1), a);
+ boolean emptyLanguage = p2 == p1 - 1;
+ if (p2 == 0 && emptyLanguage)
+ {
+ throw new FormatConversionException();
+ }
+
+ //
+ String b = emptyLanguage ? "" : value.substring(p2 + 1, p1);
+ locale = factory.createLocale(value.substring(0, p2), b, a);
}
}
//
- boolean emptyLanguage = p2 == p1 - 1;
- if (p2 == 0 && emptyLanguage)
- {
- throw new FormatConversionException();
- }
+ Map<String, Locale> copy = new HashMap<String, Locale>(CACHE);
+ copy.put(locale.toString(), locale);
+ CACHE = copy;
//
- String b = emptyLanguage ? "" : value.substring(p2 + 1, p1);
-
- //
- return factory.createLocale(value.substring(0, p2), b, a);
+ return locale;
}
protected void internalWrite(Locale locale, CharWriter writer) throws IOException
Deleted:
components/common/trunk/common/src/main/java/org/gatein/common/i18n/LocaleManager.java
===================================================================
---
components/common/trunk/common/src/main/java/org/gatein/common/i18n/LocaleManager.java 2009-12-17
15:23:11 UTC (rev 1051)
+++
components/common/trunk/common/src/main/java/org/gatein/common/i18n/LocaleManager.java 2009-12-17
17:06:31 UTC (rev 1052)
@@ -1,46 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2009, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.gatein.common.i18n;
-
-import org.gatein.common.util.Tools;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Locale;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 7228 $
- */
-public class LocaleManager
-{
-
- /** . */
- private static final Collection<Locale> all =
Collections.unmodifiableSet(Tools.toSet(Locale.getAvailableLocales()));
-
- /** Return a collection of all available locale info for the platform. */
- public static Collection<Locale> getLocales()
- {
- return all;
- }
-}