DefaultDisplayable.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collections;
  30. import java.util.HashMap;
  31. import java.util.Locale;
  32. import java.util.Map;
  33. import java.util.SortedSet;
  34. import java.util.TreeSet;
  35. import java.io.Serializable;
  36. import it.tidalwave.role.ui.LocalizedDisplayable;

  37. /***********************************************************************************************************************
  38.  *
  39.  * A default implementation of {@link LocalizedDisplayable} which a single, immutable display name in
  40.  * {@code Locale.ENGLISH} language.
  41.  *
  42.  * This is no more a public class; use {@link it.tidalwave.role.ui.Displayable#of(String)} or
  43.  * {@link LocalizedDisplayable#fromBundle(Class, String)}}
  44.  *
  45.  * @author Fabrizio Giudici
  46.  * @it.tidalwave.javadoc.stable
  47.  *
  48.  **********************************************************************************************************************/
  49. public class DefaultDisplayable implements LocalizedDisplayable, Serializable
  50.   {
  51.     private static final long serialVersionUID = 45345436345634734L;

  52.     @Nonnull
  53.     private final String displayName;

  54.     @Nonnull
  55.     private final String toStringName;

  56.     @Nonnull
  57.     private final Map<Locale, String> displayNameMap = new HashMap<>();

  58.     private final Locale defaultLocale = Locale.ENGLISH;

  59.     /*******************************************************************************************************************
  60.      *
  61.      * Creates an instance with a given display name.
  62.      *
  63.      * @param  displayName   the display name
  64.      *
  65.      ******************************************************************************************************************/
  66.     public DefaultDisplayable (@Nonnull final String displayName)
  67.       {
  68.         this(displayName, "???");
  69.       }

  70.     /*******************************************************************************************************************
  71.      *
  72.      * Creates an instance with a given display name in {@code Locale.ENGLISH} and an explicit identifier for
  73.      * {@code toString()}.
  74.      *
  75.      * @param  displayName   the display name
  76.      * @param  toStringName  the name to be rendered when {@code toString()} is called
  77.      *
  78.      ******************************************************************************************************************/
  79.     public DefaultDisplayable (@Nonnull final String displayName, @Nonnull final String toStringName)
  80.       {
  81.         this.displayName = displayName;
  82.         this.toStringName = toStringName;
  83.         displayNameMap.put(defaultLocale, displayName);
  84.       }

  85.     /*******************************************************************************************************************
  86.      *
  87.      * {@inheritDoc}
  88.      *
  89.      ******************************************************************************************************************/
  90.     @Override @Nonnull
  91.     public String getDisplayName()
  92.       {
  93.         return displayName;
  94.       }

  95.     /*******************************************************************************************************************
  96.      *
  97.      * {@inheritDoc}
  98.      *
  99.      ******************************************************************************************************************/
  100.     @Override @Nonnull
  101.     public String getDisplayName (@Nonnull final Locale locale)
  102.       {
  103.         return displayNameMap.get(locale);
  104.       }

  105.     /*******************************************************************************************************************
  106.      *
  107.      * {@inheritDoc}
  108.      *
  109.      ******************************************************************************************************************/
  110.     @Override @Nonnull
  111.     public SortedSet<Locale> getLocales()
  112.       {
  113.         return new TreeSet<>(displayNameMap.keySet());
  114.       }

  115.     /*******************************************************************************************************************
  116.      *
  117.      * {@inheritDoc}
  118.      *
  119.      ******************************************************************************************************************/
  120.     @Override @Nonnull
  121.     public Map<Locale, String> getDisplayNames()
  122.       {
  123.         return Collections.unmodifiableMap(displayNameMap);
  124.       }

  125.     /*******************************************************************************************************************
  126.      *
  127.      * {@inheritDoc}
  128.      *
  129.      ******************************************************************************************************************/
  130.     @Override @Nonnull
  131.     public String toString()
  132.       {
  133.         return String.format("%s@%x$Displayable[]", toStringName, System.identityHashCode(this));
  134.       }
  135.   }