Class MultilingualString

java.lang.Object
cz.cvut.kbss.jopa.model.MultilingualString
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ChangeTrackingIndirectMultilingualString

public class MultilingualString extends Object implements Serializable
Represents a string with translations to (possibly) multiple languages.

This allows an application to naturally work in an internationalized environment leveraging the language-tagged strings of the Semantic Web.

Instances of this class basically represent all available translations of the string, allowing to read (and write) particular versions by specifying the corresponding language tag. A special case supported by this class are simple literals (language-less strings with type xsd:string), for which the language tag is null.

Note that this class is not thread-safe.

See Also:
  • Constructor Details

    • MultilingualString

      public MultilingualString()
    • MultilingualString

      public MultilingualString(Map<String,String> value)
      Creates a new MultilingualString instance based on the specified value.

      Convenience constructor for simple initialization with existing data.

      Parameters:
      value - Map of language to translation values
  • Method Details

    • set

      public MultilingualString set(String language, String value)
      Sets value in the specified language.

      This overrides any previous value in the specified language, if it existed.

      Parameters:
      language - Language to use with the specified value. Passing null has the same effect as set(String)
      value - String value in the specified language
      Returns:
      This instance
      See Also:
    • set

      public MultilingualString set(String value)
      Sets value without language.

      That is, the specified value will be stored as a simple literal (type xsd:string).

      Parameters:
      value - Value to set
      Returns:
      This instance
      See Also:
    • get

      public String get(String language)
      Gets value for the specified language.

      If no language is specified, either the simple literal value is returned (if present), or any other existing value is returned. However, note that, in case of missing simple literal, repeated calls may return values in different languages. If there are no translations, null is returned.

      Parameters:
      language - Requested language (language tag). Can be null
      Returns:
      Value of this string for the specified language (or null if not available)
    • get

      public String get()
      Gets the value of a simple literal represented by this instance.

      If this instances does not represent a simple literal, any other existing value is returned. However, note that in that case repeated calls may return values in different languages.

      If this object is empty (i.e., neither simple literal nor any translations are available), null is returned.

      Returns:
      Value of simple literal represented by this string
    • contains

      public boolean contains(String language)
      Checks whether this string contains value in the specified language.

      If no language is specified (argument is null), this method will return true if there is value without language tag (simple literal) or in any other language.

      Parameters:
      language - Requested language (language tag)
      Returns:
      true if this string exists in the specified language
    • containsSimple

      public boolean containsSimple()
      Checks whether this string contains a simple literal value.

      That is, whether this instance contains a value without a language tag.

      Returns:
      true if and only if this string exists as a simple literal
    • isEmpty

      public boolean isEmpty()
      Checks whether this instance does not contain any language representations (including a simple literal).
      Returns:
      true if no translations exist in this instance
    • remove

      public void remove(String language)
      Removes translation in the specified language.

      Passing in null will remove the simple literal contained in this instance.

      Parameters:
      language - Language for which translation should be removed
    • getLanguages

      public Set<String> getLanguages()
      Gets the set of languages for which translations exist in the instance.

      Note that the result may contain null, indicating a simple literal.

      Returns:
      Set of languages available in this multilingual string
    • getValue

      public Map<String,String> getValue()
      Gets the translations contained in this instance as a map of language-value pairs.

      Convenience method for accessing all values at once. Note that this method returns the actual value used by this instance, so any changes to it will be reflected in the instance's behavior. It is not a general purpose method, rather a way to access and manipulate the underlying data structure more efficiently when necessary.

      Returns:
      Map of language to translation values
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • create

      public static MultilingualString create(String value, String language)
      Creates a new instance of MultilingualString and sets the specified value in the specified language.

      Convenience method for creating strings with one (initial) translation.

      Parameters:
      value - String value
      language - Language of the value (language tag)
      Returns:
      New instance of MultiLangString