org.geotoolkit.util
Class DefaultInternationalString

Object
  extended by AbstractInternationalString
      extended by DefaultInternationalString
All Implemented Interfaces:
Serializable, CharSequence, Comparable<InternationalString>, Formattable, InternationalString

@ThreadSafe
public class DefaultInternationalString
extends AbstractInternationalString
implements Serializable

An international string using a map of strings for different locales. Strings for new locales can be added, but existing strings can't be removed or modified. This behavior is a compromise between making constructions easier, and being suitable for use in immutable objects.

Since:
2.1
Version:
3.17
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
Serialized Form
Module:
utility/geotk-utility (download)    View source code for this class

Constructor Summary
DefaultInternationalString()
          Creates an initially empty international string.
DefaultInternationalString(Map<Locale,String> strings)
          Creates an international string initialized with the given localized strings.
DefaultInternationalString(String string)
          Creates an international string initialized with the given string.
 
Method Summary
 void add(Locale locale, String string)
          Adds a string for the given locale.
 boolean add(String prefix, String key, String string)
          Adds a string for the given property key.
 boolean equals(Object object)
          Compares this international string with the specified object for equality.
 Set<Locale> getLocales()
          Returns the set of locales defined in this international string.
 int hashCode()
          Returns a hash code value for this international text.
 boolean isSubsetOf(Object candidate)
          Returns true if all localized texts stored in this international string are contained in the specified object.
 String toString(Locale locale)
          Returns a string in the specified locale.
 
Methods inherited from class AbstractInternationalString
charAt, compareTo, formatTo, length, subSequence, toString
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultInternationalString

public DefaultInternationalString()
Creates an initially empty international string. Localized strings can been added using one of add(...) methods.


DefaultInternationalString

public DefaultInternationalString(String string)
Creates an international string initialized with the given string. Additional localized strings can been added using one of add(...) methods. The string specified to this constructor is the one that will be returned if no localized string is found for the Locale argument in a call to toString(Locale).

Parameters:
string - The string in no specific locale, or null if none.

DefaultInternationalString

public DefaultInternationalString(Map<Locale,String> strings)
Creates an international string initialized with the given localized strings. The content of the given map is copied, so changes to that map after construction will not be reflected into this international string.

Parameters:
strings - The strings in various locales, or null if none.
Since:
3.00
Method Detail

add

public void add(Locale locale,
                String string)
         throws IllegalArgumentException
Adds a string for the given locale.

Parameters:
locale - The locale for the string value, or null.
string - The localized string.
Throws:
IllegalArgumentException - if a different string value was already set for the given locale.

add

public boolean add(String prefix,
                   String key,
                   String string)
            throws IllegalArgumentException
Adds a string for the given property key. This is a convenience method for constructing an AbstractInternationalString during iteration through the entries in a Map. It infers the Locale from the property key, using the following steps:

For example if the prefix is "remarks", then the "remarks_fr" property key stands for remarks in French while the "remarks_fr_CA" property key stands for remarks in French Canadian.

Parameters:
prefix - The prefix to skip at the beginning of the key.
key - The property key.
string - The localized string for the specified key.
Returns:
true if the key has been recognized, or false otherwise.
Throws:
IllegalArgumentException - if the locale after the prefix is an illegal code, or a different string value was already set for the given locale.

getLocales

public Set<Locale> getLocales()
Returns the set of locales defined in this international string.

Returns:
The set of locales.
TODO:
Current implementation does not return a synchronized set. We should synchronize on the same lock than the one used for accessing the internal locale map.

toString

public String toString(Locale locale)
Returns a string in the specified locale. If there is no string for that locale, then this method searches for a locale without the variant part. If no string are found, then this method searches for a locale without the country part. For example if the "fr_CA" locale was requested but not found, then this method looks for the "fr" locale. The null locale (which stand for unlocalized message) is tried last.

Handling of null argument value
A null argument value can be given to this method for requesting a "unlocalized" string - typically some programmatic strings like UML identifiers. While such identifiers often look like English words, they are not considered as the English localization. In order to produce a value close to the common practice, this method handles null argument value as below:

Specified by:
toString in interface InternationalString
Specified by:
toString in class AbstractInternationalString
Parameters:
locale - The locale to look for, or null.
Returns:
The string in the specified locale, or in a default locale.

isSubsetOf

public boolean isSubsetOf(Object candidate)
Returns true if all localized texts stored in this international string are contained in the specified object. More specifically:

Parameters:
candidate - The object which may contains this international string.
Returns:
true if the given object contains all localized strings found in this international string.
Since:
2.3

equals

public boolean equals(Object object)
Compares this international string with the specified object for equality.

Overrides:
equals in class Object
Parameters:
object - The object to compare with this international string.
Returns:
true if the given object is equal to this string.

hashCode

public int hashCode()
Returns a hash code value for this international text.

Overrides:
hashCode in class Object


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.