1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * TheseFoolishThings: Miscellaneous utilities
5 * http://tidalwave.it/projects/thesefoolishthings
6 *
7 * Copyright (C) 2009 - 2024 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 the License.
12 * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.role.ui.impl;
27
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 *
40 * A default implementation of {@link LocalizedDisplayable} which a single, immutable display name in
41 * {@code Locale.ENGLISH} language.
42 *
43 * This is no more a public class; use {@link it.tidalwave.role.ui.Displayable#of(String)} or
44 * {@link LocalizedDisplayable#fromBundle(Class, String)}}
45 *
46 * @author Fabrizio Giudici
47 * @it.tidalwave.javadoc.stable
48 *
49 **************************************************************************************************************************************************************/
50 public class DefaultDisplayable implements LocalizedDisplayable, Serializable
51 {
52 private static final long serialVersionUID = 45345436345634734L;
53
54 @Nonnull
55 private final String displayName;
56
57 @Nonnull
58 private final String toStringName;
59
60 @Nonnull
61 private final Map<Locale, String> displayNameMap = new HashMap<>();
62
63 private final Locale defaultLocale = Locale.ENGLISH;
64
65 /***********************************************************************************************************************************************************
66 * Creates an instance with a given display name.
67 *
68 * @param displayName the display name
69 **********************************************************************************************************************************************************/
70 public DefaultDisplayable (@Nonnull final String displayName)
71 {
72 this(displayName, "???");
73 }
74
75 /***********************************************************************************************************************************************************
76 * Creates an instance with a given display name in {@code Locale.ENGLISH} and an explicit identifier for
77 * {@code toString()}.
78 *
79 * @param displayName the display name
80 * @param toStringName the name to be rendered when {@code toString()} is called
81 **********************************************************************************************************************************************************/
82 public DefaultDisplayable (@Nonnull final String displayName, @Nonnull final String toStringName)
83 {
84 this.displayName = displayName;
85 this.toStringName = toStringName;
86 displayNameMap.put(defaultLocale, displayName);
87 }
88
89 /***********************************************************************************************************************************************************
90 * {@inheritDoc}
91 **********************************************************************************************************************************************************/
92 @Override @Nonnull
93 public String getDisplayName()
94 {
95 return displayName;
96 }
97
98 /***********************************************************************************************************************************************************
99 * {@inheritDoc}
100 **********************************************************************************************************************************************************/
101 @Override @Nonnull
102 public String getDisplayName (@Nonnull final Locale locale)
103 {
104 return displayNameMap.get(locale);
105 }
106
107 /***********************************************************************************************************************************************************
108 * {@inheritDoc}
109 **********************************************************************************************************************************************************/
110 @Override @Nonnull
111 public SortedSet<Locale> getLocales()
112 {
113 return new TreeSet<>(displayNameMap.keySet());
114 }
115
116 /***********************************************************************************************************************************************************
117 * {@inheritDoc}
118 **********************************************************************************************************************************************************/
119 @Override @Nonnull
120 public Map<Locale, String> getDisplayNames()
121 {
122 return Collections.unmodifiableMap(displayNameMap);
123 }
124
125 /***********************************************************************************************************************************************************
126 * {@inheritDoc}
127 **********************************************************************************************************************************************************/
128 @Override @Nonnull
129 public String toString()
130 {
131 return String.format("%s@%x$Displayable[]", toStringName, System.identityHashCode(this));
132 }
133 }