Package dev.orne.i18n

Class I18nStringMap

java.lang.Object
dev.orne.i18n.I18nStringMap
All Implemented Interfaces:
I18nString, Serializable

@API(status=STABLE, since="0.1") public class I18nStringMap extends Object implements I18nString
Implementation of I18nString populated with translations.

Useful for translations edition and visualization.

Since:
0.1
Version:
1.0, 2021-01
Author:
(w) Iker Hernaez
See Also:
  • Constructor Details

    • I18nStringMap

      public I18nStringMap()
      Creates a new instance with empty default text and no translations.
    • I18nStringMap

      public I18nStringMap(@NotNull @NotNull String defaultText)
      Creates a new instance with specified default text and no translations.
      Parameters:
      defaultText - The default text
    • I18nStringMap

      public I18nStringMap(@NotNull @NotNull I18nString copy)
      Copy constructor.

      If copy is of I18nStringMap type a full copy is performed. If copy is of I18nResourcesString type the formatted default text is set as default text and the result of get() as translation for current language. Otherwise the result of get() is used as default text.

      Parameters:
      copy - The instance to copy
  • Method Details

    • getDefaultText

      @NotNull public @NotNull String getDefaultText()
      Returns the default text.
      Returns:
      The default text
    • setDefaultText

      @NotNull public @NotNull I18nStringMap setDefaultText(@NotNull @NotNull String defaultText)
      Sets the default text.
      Parameters:
      defaultText - The default text
      Returns:
      This instance for method chaining
    • getI18n

      @NotNull public @NotNull Map<@NotNull String,@NotNull String> getI18n()
      Returns the text translations.
      Returns:
      The text translations
    • setI18n

      @NotNull public @NotNull I18nStringMap setI18n(@NotNull @NotNull Map<@NotNull String,@NotNull String> translations)
      Sets the text translations. This methods modifies the internal Map. Further modifications on argument will have no effect in internal translations.
      Parameters:
      translations - The text translations
      Returns:
      This instance for method chaining
    • get

      @NotNull public @NotNull String get()
      Returns the text for the current language.
      Specified by:
      get in interface I18nString
      Returns:
      The text for the current language
    • get

      @NotNull public @NotNull String get(@NotNull @NotNull String language)
      Returns the text for the specified language, or the default text if no translation is available.
      Specified by:
      get in interface I18nString
      Parameters:
      language - The language of the requested text
      Returns:
      The text for the specified language
    • get

      @NotNull public @NotNull String get(@NotNull @NotNull Locale locale)
      Returns the text for language of the specified locale, or the default text if no translation is available.
      Specified by:
      get in interface I18nString
      Parameters:
      locale - The language of the requested text
      Returns:
      The text for the specified language
    • set

      @NotNull public @NotNull I18nStringMap set(@NotNull @NotNull String language, @NotNull @NotNull String text)
      Sets the translation for the specified locale.
      Parameters:
      language - The language of the translation
      text - The translated text
      Returns:
      This instance for method chaining
    • set

      @NotNull public @NotNull I18nStringMap set(@NotNull @NotNull Locale locale, @NotNull @NotNull String text)
      Sets the translation for the specified locale.
      Parameters:
      locale - The language of the translation
      text - The translated text
      Returns:
      This instance for method chaining
    • remove

      @NotNull public @NotNull I18nStringMap remove(@NotNull @NotNull String language)
      Removes the translation for the specified language.
      Parameters:
      language - The language of the translation
      Returns:
      This instance for method chaining
    • remove

      @NotNull public @NotNull I18nStringMap remove(@NotNull @NotNull Locale locale)
      Removes the translation for the specified language.
      Parameters:
      locale - The language of the translation
      Returns:
      This instance for method chaining
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • isEquivalent

      public boolean isEquivalent(I18nString obj)
      Returns true if the specified I18nString is equivalent to this instance.
      1. If other is null returns false.
      2. If other is of the same type calls equals().
      3. Otherwise checks that the translations for current language of both instances are equal.
      Specified by:
      isEquivalent in interface I18nString
      Parameters:
      obj - The instance to compare against
      Returns:
      If both instances are equivalent
    • asMap

      @NotNull public @NotNull I18nStringMap asMap()
      Returns this instance represented as an I18nStringMap instance.
      Specified by:
      asMap in interface I18nString
      Returns:
      This instance represented as an I18nStringMap instance
    • toString

      @NotNull public @NotNull String toString()
      Returns the default text.
      Overrides:
      toString in class Object
      Returns:
      The default text.