Skip to content

Package: Displayable

Displayable

nameinstructionbranchcomplexitylinemethod
asComparing()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
comparing()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
display(Consumer)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
fromBundle(Class, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDisplayName(Locale)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getDisplayNames()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getLocales()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$of$0(Function, Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(Function, Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(String, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(Supplier)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
render(Object[])
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
renderTo(Consumer, Object[])
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 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/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.role;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Comparator;
30: import java.util.Locale;
31: import java.util.Map;
32: import java.util.SortedSet;
33: import java.util.TreeSet;
34: import java.util.function.Consumer;
35: import java.util.function.Function;
36: import java.util.function.Supplier;
37: import it.tidalwave.ui.core.role.impl.AsDisplayableComparator;
38: import it.tidalwave.ui.core.role.impl.DefaultDisplayable;
39: import it.tidalwave.ui.core.role.impl.DisplayableComparator;
40: import it.tidalwave.util.As;
41: import static it.tidalwave.util.BundleUtilities.getMessage;
42:
43: /***************************************************************************************************************************************************************
44: *
45: * The role of an object which can provide its own display name.
46: *
47: * @stereotype Role
48: *
49: * @since 2.0-ALPHA-1
50: * @author Fabrizio Giudici
51: * @it.tidalwave.javadoc.stable
52: *
53: **************************************************************************************************************************************************************/
54: @FunctionalInterface
55: public interface Displayable
56: {
57: /** Shortcut for {@link it.tidalwave.util.As}. */
58: public static final Class<Displayable> _Displayable_ = Displayable.class;
59:
60: /***********************************************************************************************************************************************************
61: * A default {@code Displayable} with an empty display name.
62: **********************************************************************************************************************************************************/
63: public static final Displayable DEFAULT = new DefaultDisplayable("", "DEFAULT");
64:
65: /***********************************************************************************************************************************************************
66: * Returns the display name in the current {@link java.util.Locale}.
67: *
68: * @return the display name
69: **********************************************************************************************************************************************************/
70: @Nonnull
71: public String getDisplayName();
72:
73: /***********************************************************************************************************************************************************
74: * Returns the display name in the given {@link Locale}.
75: * @param locale the {@code Locale}
76: * @since 2.0-ALPHA-2
77: * @return the display name
78: **********************************************************************************************************************************************************/
79: @Nonnull
80: public default String getDisplayName (@Nonnull final Locale locale)
81: {
82: return getDisplayName();
83: }
84:
85: /***********************************************************************************************************************************************************
86: * Returns all the display names in {@link Map} where they are indexed by {@code Locale}.
87: * @since 2.0-ALPHA-2
88: * @return the display names
89: **********************************************************************************************************************************************************/
90: @Nonnull
91: public default Map<Locale, String> getDisplayNames()
92: {
93: return Map.of();
94: }
95:
96: /***********************************************************************************************************************************************************
97: * Returns the supported {@code Locale}s.
98: * @since 2.0-ALPHA-2
99: * @return the available {@code Locale}s
100: **********************************************************************************************************************************************************/
101: @Nonnull
102: public default SortedSet<Locale> getLocales()
103: {
104: return new TreeSet<>();
105: }
106:
107: /***********************************************************************************************************************************************************
108: * Sends the display name in the current {@link java.util.Locale} to a given customer.
109: *
110: * @param consumer the {@code Consumer}
111: * @since 3.2-ALPHA-15
112: **********************************************************************************************************************************************************/
113: @SuppressWarnings("BoundedWildcard")
114: public default void display (@Nonnull final Consumer<String> consumer)
115: {
116: consumer.accept(getDisplayName());
117: }
118:
119: /***********************************************************************************************************************************************************
120: * Creates an instance with a given display name.
121: *
122: * @param displayName the display name
123: * @return the new instance
124: * @since 3.2-ALPHA-1 (was {@code DefaultDisplayable}
125: **********************************************************************************************************************************************************/
126: @Nonnull
127: public static Displayable of (@Nonnull final String displayName)
128: {
129: return of(displayName, "???");
130: }
131:
132: /***********************************************************************************************************************************************************
133: * Creates an instance with a given display name iand an explicit label for {@code toString()}.
134: *
135: * @param displayName the display name
136: * @param toStringName the name to be rendered when {@code toString()} is called
137: * @return the new instance
138: * @since 3.2-ALPHA-1 (was {@code DefaultDisplayable}
139: **********************************************************************************************************************************************************/
140: @Nonnull
141: public static Displayable of (@Nonnull final String displayName, @Nonnull final String toStringName)
142: {
143: return new DefaultDisplayable(displayName, toStringName);
144: }
145:
146: /***********************************************************************************************************************************************************
147: * Creates an instance from a {@link Supplier}{@code <String>}. The supplier is invoked each time
148: * {@link #getDisplayName()} is called.
149: *
150: * @param supplier the {@code Supplier}
151: * @return the new instance
152: * @since 3.2-ALPHA-3
153: * @it.tidalwave.javadoc.experimental
154: **********************************************************************************************************************************************************/
155: @Nonnull
156: public static Displayable of (@Nonnull final Supplier<String> supplier)
157: {
158: return supplier::get;
159: }
160:
161: /***********************************************************************************************************************************************************
162: * Creates an instance from a {@link Function}{@code <T, String>} and a generic object that the function is applied
163: * to. The function is invoked each time {@link #getDisplayName()} is called.
164: *
165: * @param <T> the type of the object
166: * @param function the {@code Function}
167: * @param object the object
168: * @return the new instance
169: * @since 3.2-ALPHA-3
170: * @it.tidalwave.javadoc.experimental
171: **********************************************************************************************************************************************************/
172: @Nonnull
173: public static <T> Displayable of (@Nonnull final Function<T, String> function, @Nonnull final T object)
174: {
175: return () -> function.apply(object);
176: }
177:
178: /***********************************************************************************************************************************************************
179: * Creates a {@link Displayable} from a resource bundle. The bundle resource file is named {@code Bundle.properties} and it should be placed in the same
180: * package as the owner class.
181: * @param ownerClass the class that owns the bundle
182: * @param key the resource key
183: * @since 2.0-ALPHA-2
184: * @return the new instance
185: **********************************************************************************************************************************************************/
186: @Nonnull
187: public static Displayable fromBundle (@Nonnull final Class<?> ownerClass, @Nonnull final String key)
188: {
189: return new DefaultDisplayable(getMessage(ownerClass, key));
190: }
191:
192: /***********************************************************************************************************************************************************
193: * Returns a {@link Comparator} for comparing two instances of {@code Displayable}.
194: *
195: * @return the {@code Comparator}
196: * @since 3.2-ALPHA-6
197: **********************************************************************************************************************************************************/
198: @Nonnull
199: public static Comparator<Displayable> comparing()
200: {
201: return DisplayableComparator.getInstance();
202: }
203:
204: /***********************************************************************************************************************************************************
205: * Returns a {@link Comparator} for comparing two instances of objects implementing {@code As} that contain the
206: * {@code Displayable} role.
207: *
208: * @return the {@code Comparator}
209: * @since 3.2-ALPHA-6
210: **********************************************************************************************************************************************************/
211: @Nonnull
212: public static Comparator<As> asComparing()
213: {
214: return AsDisplayableComparator.getInstance();
215: }
216:
217: /***********************************************************************************************************************************************************
218: * Renders the attached object into a {@link String}. The method accepts optional parameters that can be used to control the format of the rendering; they
219: * are usually specific of the object attached to this role.
220: * @param args optional rendering parameters
221: * @return the string
222: **********************************************************************************************************************************************************/
223: @Nonnull
224: public default String render (@Nonnull final Object ... args)
225: {
226: return getDisplayName();
227: }
228:
229: /***********************************************************************************************************************************************************
230: * Renders the attached object providing the string tu a {@link Consumer}. The method accepts optional parameters that can be used to control the format of
231: * the rendering; they are usually specific of the object attached to this role.
232: * @param consumer the {@code Consumer} to append to
233: * @param args optional rendering parameters
234: **********************************************************************************************************************************************************/
235: public default void renderTo (@Nonnull final Consumer<? super String> consumer, @Nonnull final Object ... args)
236: {
237: consumer.accept(render(args));
238: }
239: }