Author: julien(a)jboss.com
Date: 2008-01-07 06:38:45 -0500 (Mon, 07 Jan 2008)
New Revision: 9446
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/i18n/LocalizedString.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/LocalizedStringTestCase.java
Log:
generified LocalizedString
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/i18n/LocalizedString.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/i18n/LocalizedString.java 2008-01-07
09:21:33 UTC (rev 9445)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/i18n/LocalizedString.java 2008-01-07
11:38:45 UTC (rev 9446)
@@ -24,7 +24,6 @@
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -41,11 +40,14 @@
public final class LocalizedString
{
+ /** Shared empty string. */
+ private static final String[] EMPTY_STRINGS = new String[0];
+
/** The logger. */
private static final Logger log = Logger.getLogger(LocalizedString.class);
- /** An unmodifiable <Locale,String>Map. */
- private final Map values;
+ /** A unmodifiable map. */
+ private final Map<Locale, Value> values;
/** The default locale. */
private final Locale defaultLocale;
@@ -88,8 +90,8 @@
}
//
- Map values = new HashMap(1);
- addValueForLocale(values, defaultLocale, defaultValue);
+ Map<Locale, Value> values = new HashMap<Locale, Value>(1);
+ values.put(defaultLocale, new Value(defaultLocale, defaultValue));
//
this.defaultLocale = defaultLocale;
@@ -111,17 +113,17 @@
//
this.defaultLocale = defaultLocale;
- this.values = Collections.EMPTY_MAP;
+ this.values = Collections.emptyMap();
}
/**
* Build a localized string using a <Locale,String>Map object.
*
* @param values the <Locale,String>Map
- * @param defaultLocale
+ * @param defaultLocale the default locale
* @throws IllegalArgumentException if one argument if null or if the map entries are
different from <Locale,String>Map.Entry
*/
- public LocalizedString(Map values, Locale defaultLocale) throws
IllegalArgumentException
+ public LocalizedString(Map<Locale, String> values, Locale defaultLocale) throws
IllegalArgumentException
{
if (values == null)
{
@@ -133,21 +135,12 @@
}
// Convert strings to value
- Map tmp = new HashMap(values.size());
- for (Iterator i = values.entrySet().iterator(); i.hasNext();)
+ Map<Locale, Value> tmp = new HashMap<Locale, Value>(values.size());
+ for (Map.Entry<Locale, String> entry : values.entrySet())
{
- Map.Entry entry = (Map.Entry)i.next();
- Object key = entry.getKey();
- if (!(key instanceof Locale))
- {
- throw new IllegalArgumentException("Key not a locale " +
entry.getKey());
- }
- Object value = entry.getValue();
- if (!(value instanceof String))
- {
- throw new IllegalArgumentException("Value not a string " +
entry.getValue());
- }
- addValueForLocale(tmp, (Locale)key, (String)value);
+ Locale key = entry.getKey();
+ String value = entry.getValue();
+ tmp.put(key, new Value(key, value));
}
//
@@ -156,19 +149,6 @@
}
/**
- * Adds a new value for the specified locale to this LocalizedString. Note that if a
value existed for this Locale,
- * it will be overwritten.
- *
- * @param locale the locale of the value
- * @param value the value
- * @since 2.4
- */
- private static void addValueForLocale(Map values, Locale locale, String value)
- {
- values.put(locale, new Value(locale, value));
- }
-
- /**
* Determines if this LocalizedString contains any values.
*
* @return <code>true</code> if this LocalizedString contains localized
values, <code>false</code> otherwise.
@@ -176,7 +156,7 @@
*/
public boolean hasValues()
{
- return values.isEmpty() == false;
+ return !values.isEmpty();
}
/**
@@ -209,6 +189,8 @@
public String getString(Locale locale, boolean resolve)
{
Value value = getValue(locale, resolve);
+
+ //
if (value != null)
{
return value.getString();
@@ -235,38 +217,39 @@
throw new IllegalArgumentException("No null locale accepted as
argument");
}
- // fail fast is there aren't any values
+ // Fail fast is there aren't any values
if (values.isEmpty())
{
return null;
}
+ //
if (resolve)
{
- Value value = (Value)values.get(locale);
+ Value value = values.get(locale);
if (value == null && !locale.getVariant().equals(""))
{
- value = (Value)values.get(new Locale(locale.getLanguage(),
locale.getCountry()));
+ value = values.get(new Locale(locale.getLanguage(), locale.getCountry()));
}
if (value == null && !locale.getCountry().equals(""))
{
- value = (Value)values.get(new Locale(locale.getLanguage()));
+ value = values.get(new Locale(locale.getLanguage()));
}
if (value == null)
{
- value = (Value)values.get(defaultLocale);
+ value = values.get(defaultLocale);
}
return value;
}
else
{
- return (Value)values.get(locale);
+ return values.get(locale);
}
}
- public Map getValues()
+ public Map<Locale, Value> getValues()
{
- return Collections.unmodifiableMap(values);
+ return values;
}
/**
@@ -292,7 +275,7 @@
public String getMostAppropriateValueFor(String[] desiredLocales) throws
IllegalArgumentException
{
Value mapping = getPreferredOrBestLocalizedMappingFor(desiredLocales);
- return (mapping == null) ? null : mapping.getString();
+ return mapping == null ? null : mapping.getString();
}
/**
@@ -368,7 +351,7 @@
{
if (cachedToString == null)
{
- cachedToString = "LocalizedString[value='" +
getMostAppropriateValueFor(new String[0]) + "',defaultLocale=" +
getDefaultLocale() + "]";
+ cachedToString = "LocalizedString[value='" +
getMostAppropriateValueFor(EMPTY_STRINGS) + "',defaultLocale=" +
getDefaultLocale() + "]";
}
return cachedToString;
}
@@ -379,27 +362,25 @@
{
return true;
}
- if (o == null || LocalizedString.class != o.getClass())
+ if (o instanceof LocalizedString)
{
- return false;
+ LocalizedString that = (LocalizedString)o;
+ return defaultLocale.equals(that.defaultLocale) &&
getMostAppropriateValueFor(EMPTY_STRINGS).equals(that.getMostAppropriateValueFor(EMPTY_STRINGS));
}
-
- //
- LocalizedString that = (LocalizedString)o;
- return defaultLocale.equals(that.defaultLocale) &&
getMostAppropriateValueFor(new String[0]).equals(that.getMostAppropriateValueFor(new
String[0]));
+ return false;
}
public int hashCode()
{
if (hashCode == null)
{
- hashCode = new Integer(31 * getMostAppropriateValueFor(new String[0]).hashCode()
+ defaultLocale.hashCode());
+ hashCode = 31 * getMostAppropriateValueFor(EMPTY_STRINGS).hashCode() +
defaultLocale.hashCode();
}
- return hashCode.intValue();
+ return hashCode;
}
/**
- * A localized string value.
+ * A unmodifiable localized string value.
*/
public static class Value
{
@@ -413,7 +394,7 @@
/**
* @param locale the locale
* @param string the string
- * @throws IllegalArgumentException if one argument is null
+ * @throws IllegalArgumentException if any argument is null
*/
public Value(Locale locale, String string) throws IllegalArgumentException
{
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/LocalizedStringTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/LocalizedStringTestCase.java 2008-01-07
09:21:33 UTC (rev 9445)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/LocalizedStringTestCase.java 2008-01-07
11:38:45 UTC (rev 9446)
@@ -35,7 +35,7 @@
*/
public class LocalizedStringTestCase extends TestCase
{
- private Map values = new HashMap();
+ private Map<Locale, String> values = new HashMap<Locale, String>();
private LocalizedString localizedString;
protected void setUp() throws Exception
@@ -46,6 +46,18 @@
localizedString = new LocalizedString(values, Locale.US);
}
+ public void testLocalizedStringValuesAreNotModifiable()
+ {
+ try
+ {
+ localizedString.getValues().clear();
+ }
+ catch (Exception expected)
+ {
+ assertTrue(localizedString.hasValues());
+ }
+ }
+
public void testPreferredOrBestLocalizedMappingFor()
{
try