Package: DefaultDisplayable
DefaultDisplayable
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultDisplayable(String) |
|
|
|
|
|
||||||||||||||||||||
DefaultDisplayable(String, String) |
|
|
|
|
|
||||||||||||||||||||
getDisplayName() |
|
|
|
|
|
||||||||||||||||||||
getDisplayName(Locale) |
|
|
|
|
|
||||||||||||||||||||
getDisplayNames() |
|
|
|
|
|
||||||||||||||||||||
getLocales() |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
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
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:
29: import javax.annotation.Nonnull;
30: import java.util.Collections;
31: import java.util.HashMap;
32: import java.util.Locale;
33: import java.util.Map;
34: import java.util.SortedSet;
35: import java.util.TreeSet;
36: import java.io.Serializable;
37: import it.tidalwave.role.ui.LocalizedDisplayable;
38:
39: /***********************************************************************************************************************
40: *
41: * A default implementation of {@link LocalizedDisplayable} which a single, immutable display name in
42: * {@code Locale.ENGLISH} language.
43: *
44: * This is no more a public class; use {@link it.tidalwave.role.ui.Displayable#of(String)} or
45: * {@link LocalizedDisplayable#fromBundle(Class, String)}}
46: *
47: * @author Fabrizio Giudici
48: * @it.tidalwave.javadoc.stable
49: *
50: **********************************************************************************************************************/
51: public class DefaultDisplayable implements LocalizedDisplayable, Serializable
52: {
53: private static final long serialVersionUID = 45345436345634734L;
54:
55: @Nonnull
56: private final String displayName;
57:
58: @Nonnull
59: private final String toStringName;
60:
61: @Nonnull
62: private final Map<Locale, String> displayNameMap = new HashMap<>();
63:
64: private final Locale defaultLocale = Locale.ENGLISH;
65:
66: /*******************************************************************************************************************
67: *
68: * Creates an instance with a given display name.
69: *
70: * @param displayName the display name
71: *
72: ******************************************************************************************************************/
73: public DefaultDisplayable (@Nonnull final String displayName)
74: {
75: this(displayName, "???");
76: }
77:
78: /*******************************************************************************************************************
79: *
80: * Creates an instance with a given display name in {@code Locale.ENGLISH} and an explicit identifier for
81: * {@code toString()}.
82: *
83: * @param displayName the display name
84: * @param toStringName the name to be rendered when {@code toString()} is called
85: *
86: ******************************************************************************************************************/
87: public DefaultDisplayable (@Nonnull final String displayName, @Nonnull final String toStringName)
88: {
89: this.displayName = displayName;
90: this.toStringName = toStringName;
91: displayNameMap.put(defaultLocale, displayName);
92: }
93:
94: /*******************************************************************************************************************
95: *
96: * {@inheritDoc}
97: *
98: ******************************************************************************************************************/
99: @Override @Nonnull
100: public String getDisplayName()
101: {
102: return displayName;
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * {@inheritDoc}
108: *
109: ******************************************************************************************************************/
110: @Override @Nonnull
111: public String getDisplayName (@Nonnull final Locale locale)
112: {
113: return displayNameMap.get(locale);
114: }
115:
116: /*******************************************************************************************************************
117: *
118: * {@inheritDoc}
119: *
120: ******************************************************************************************************************/
121: @Override @Nonnull
122: public SortedSet<Locale> getLocales()
123: {
124: return new TreeSet<>(displayNameMap.keySet());
125: }
126:
127: /*******************************************************************************************************************
128: *
129: * {@inheritDoc}
130: *
131: ******************************************************************************************************************/
132: @Override @Nonnull
133: public Map<Locale, String> getDisplayNames()
134: {
135: return Collections.unmodifiableMap(displayNameMap);
136: }
137:
138: /*******************************************************************************************************************
139: *
140: * {@inheritDoc}
141: *
142: ******************************************************************************************************************/
143: @Override @Nonnull
144: public String toString()
145: {
146: return String.format("%s@%x$Displayable[]", toStringName, System.identityHashCode(this));
147: }
148: }