Class GrowableInternationalString

  • All Implemented Interfaces:
    Serializable, CharSequence, Comparable<InternationalString>, InternationalString

    public class GrowableInternationalString
    extends AbstractInternationalString
    implements Serializable
    An implementation of 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 constructionss easier, and being suitable for use in immutable objects.

    This class is mutable and not thread-safe.

    Since:
    2.1
    Author:
    Martin Desruisseaux (IRD)
    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(String prefix, String key, String string)
      Adds a string for the given property key.
      void add​(Locale locale, String string)
      Adds a string for the given locale.
      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 Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface CharSequence

        chars, codePoints
    • Constructor Detail

      • GrowableInternationalString

        public GrowableInternationalString()
        Constructs an initially empty international string. Localized strings can been added using one of add(...) methods.
      • GrowableInternationalString

        public GrowableInternationalString​(String string)
        Constructs an international string initialized with the specified 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.
      • GrowableInternationalString

        public GrowableInternationalString​(InternationalString internationalString)
        Constructs an international string from the specified InternationalString. It avoids to add an entry for each locales in cases where InternationalString.toString(Locale) returns always the same string, by considering the result returned by InternationalString.toString() as the default value.
        Parameters:
        internationalString - the internationalString from which construct a new instance.
    • 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:
        • If the key do not starts with the specified prefix, then this method do nothing and returns false.
        • Otherwise, the characters after the prefix are parsed as an ISO language and country code, and the add(Locale,String) method is invoked.

        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 begining 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.

        The returned set may contain a null object, signifying there's a "default" value (as created wither through the single-string constructor, or with a null "locale" parameter to add(Locale, String)

        Returns:
        The set of locales.
      • toString

        public String toString​(Locale locale)
        Returns a string in the specified locale. If there is no string for the specified locale, then this method search for a locale without the variant part. If no string are found, then this method search 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.
        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:
        • If candidate is an instance of InternationalString, then this method returns true if, for all locale-string pairs contained in this, candidate.toString(locale) returns a string equals to string.

        • If candidate is an instance of CharSequence, then this method returns true if toString(Locale) returns a string equals to candidate.toString() for all locales.

        • If candidate is an instance of Map, then this methods returns true if all locale-string pairs are contained into candidate.

        • Otherwise, this method returns false.

        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 equals to this string.
      • hashCode

        public int hashCode()
        Returns a hash code value for this international text.
        Overrides:
        hashCode in class Object