Package org.apache.sis.util
Class DefaultInternationalString
java.lang.Object
org.apache.sis.util.AbstractInternationalString
org.apache.sis.util.DefaultInternationalString
- All Implemented Interfaces:
Serializable,CharSequence,Comparable<org.opengis.util.InternationalString>,Formattable,org.opengis.util.InternationalString
An international string using a map of strings for different locales.
Strings for new locales can be added,
but existing strings cannot be removed or modified.
This behavior is a compromise between making constructions easier, and being suitable for
use in immutable objects.
Thread safety
Instances ofDefaultInternationalString are thread-safe. While those instances are not strictly immutable,
SIS typically references them as if they were immutable because of their add-only behavior.- Since:
- 1.1
- Version:
- 1.1
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe string values in different locales (nevernull).An unmodifiable view of the entry set inlocaleMap.private static final longSerial number for inter-operability with different versions.Fields inherited from class org.apache.sis.util.AbstractInternationalString
defaultValue -
Constructor Summary
ConstructorsConstructorDescriptionCreates an initially empty international string.DefaultInternationalString(String string) Creates an international string initialized with the given string.DefaultInternationalString(Map<Locale, String> strings) Creates an international string initialized with the given localized strings. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a string for the given locale.booleanCompares this international string with the specified object for equality.Returns the set of locales defined in this international string.private StringReturns a string in the specified locale.inthashCode()Returns a hash code value for this international text.booleanisSubsetOf(Object candidate) Returnstrueif all localized texts stored in this international string are contained in the specified object.private voidCanonicalize the locales after deserialization.Returns a string in the specified locale.Methods inherited from class org.apache.sis.util.AbstractInternationalString
charAt, compareTo, formatTo, length, subSequence, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial number for inter-operability with different versions.- See Also:
-
localeMap
The string values in different locales (nevernull). -
localeSet
An unmodifiable view of the entry set inlocaleMap. This is the set of locales defined in this international string. Will be constructed only when first requested.
-
-
Constructor Details
-
DefaultInternationalString
public DefaultInternationalString()Creates an initially empty international string. Localized strings can be added using one ofadd(…)methods. -
DefaultInternationalString
Creates an international string initialized with the given string. Additional localized strings can be added using one ofadd(…)methods. The string specified to this constructor is the one that will be returned if no localized string is found for theLocaleargument in a call totoString(Locale).- Parameters:
string- the string in no specific locale, ornullif none.
-
DefaultInternationalString
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, ornullif none.- See Also:
-
-
Method Details
-
add
Adds a string for the given locale.- Parameters:
locale- the locale for thestringvalue.string- the localized string.- Throws:
IllegalArgumentException- if a different string value was already set for the given locale.
-
getLocales
Returns the set of locales defined in this international string.- Returns:
- the set of locales.
-
getString
Returns a string in the specified locale. If there is no string for thatlocale, 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. If none are found, then this method returnsnull.- Parameters:
locale- the locale to look for, ornull.- Returns:
- the string in the specified locale, or
nullif none was found.
-
toString
Returns a string in the specified locale. If there is no string for thatlocale, 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 root locale is tried last.Handling of
Locale.ROOTargument valueLocale.ROOTcan be given to this method for requesting a "unlocalized" string, typically some programmatic values like enumerations or identifiers. While identifiers often look like English words,Locale.ROOTis not considered synonymous toLocale.ENGLISHbecause the values may differ in the way numbers and dates are formatted (e.g. using the ISO 8601 standard for dates instead of English conventions). In order to produce a value close to the common practice, this method handlesLocale.ROOTas below:- If a string has been explicitly added for
Locale.ROOT, then that string is returned. - Otherwise, acknowledging that UML identifiers in OGC/ISO specifications are primarily
expressed in the English language, this method looks for strings associated to
Locale.USas an approximation of "unlocalized" strings. - If no English string was found, then this method looks for a string for the system default locale.
- If none of the above steps found a string, then this method returns an arbitrary string.
Handling of
In the default implementation, thenullargument valuenulllocale is handled as a synonymous ofLocale.ROOT. However, subclasses are free to use a different fallback. Client code are encouraged to specify only non-null values for more determinist behavior.- Specified by:
toStringin interfaceorg.opengis.util.InternationalString- Specified by:
toStringin classAbstractInternationalString- Parameters:
locale- the desired locale for the string to be returned.- Returns:
- the string in the given locale if available, or in an implementation-dependent fallback locale otherwise.
- See Also:
- If a string has been explicitly added for
-
isSubsetOf
Returnstrueif all localized texts stored in this international string are contained in the specified object. More specifically:- If
candidateis an instance ofInternationalString, then this method returnstrueif, for all locale-string pairs contained inthis,candidate.toString(locale)returns a string equals tostring. - If
candidateis an instance ofCharSequence, then this method returnstrueiftoString(Locale)returns a string equals tocandidate.toString()for all locales. - If
candidateis an instance ofMap, then this methods returnstrueif all locale-string pairs are contained intocandidate. - Otherwise, this method returns
false.
- Parameters:
candidate- the object which may contains this international string.- Returns:
trueif the given object contains all localized strings found in this international string.
- If
-
equals
Compares this international string with the specified object for equality. -
hashCode
public int hashCode()Returns a hash code value for this international text. -
readObject
Canonicalize the locales after deserialization.- Parameters:
in- the input stream from which to deserialize an international string.- Throws:
IOException- if an I/O error occurred while reading or if the stream contains invalid data.ClassNotFoundException- if the class serialized on the stream is not on the classpath.
-