Skip to content

Method: of(String)

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;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Comparator;
31: import java.util.function.Consumer;
32: import java.util.function.Function;
33: import java.util.function.Supplier;
34: import it.tidalwave.util.As;
35: import it.tidalwave.role.ui.impl.AsDisplayableComparator;
36: import it.tidalwave.role.ui.impl.DefaultDisplayable;
37: import it.tidalwave.role.ui.impl.DisplayableComparator;
38: import static it.tidalwave.util.BundleUtilities.getMessage;
39:
40: /***********************************************************************************************************************
41: *
42: * The role of an object which can provide its own display name.
43: *
44: * @stereotype Role
45: *
46: * @author Fabrizio Giudici
47: * @it.tidalwave.javadoc.stable
48: *
49: **********************************************************************************************************************/
50: @FunctionalInterface
51: public interface Displayable
52: {
53: public static final Class<Displayable> _Displayable_ = Displayable.class;
54:
55: /*******************************************************************************************************************
56: *
57: * A default {@code Displayable} with an empty display name.
58: *
59: ******************************************************************************************************************/
60: public static final Displayable DEFAULT = new DefaultDisplayable("", "DEFAULT");
61:
62: /*******************************************************************************************************************
63: *
64: * Returns the display name in the current {@link java.util.Locale}.
65: *
66: * @return the display name
67: *
68: ******************************************************************************************************************/
69: @Nonnull
70: public String getDisplayName();
71:
72: /*******************************************************************************************************************
73: *
74: * Sends the display name in the current {@link java.util.Locale} to a given customer.
75: *
76: * @param consumer the {@code Consumer}
77: * @since 3.2-ALPHA-15
78: *
79: ******************************************************************************************************************/
80: @SuppressWarnings("BoundedWildcard")
81: public default void display (@Nonnull final Consumer<String> consumer)
82: {
83: consumer.accept(getDisplayName());
84: }
85:
86: /*******************************************************************************************************************
87: *
88: * Creates an instance with a given display name.
89: *
90: * @param displayName the display name
91: * @return the new instance
92: * @since 3.2-ALPHA-1 (was {@code DefaultDisplayable}
93: *
94: ******************************************************************************************************************/
95: @Nonnull
96: public static Displayable of (@Nonnull final String displayName)
97: {
98: return of(displayName, "???");
99: }
100:
101: /*******************************************************************************************************************
102: *
103: * Creates an instance with a given display name iand an explicit label for {@code toString()}.
104: *
105: * @param displayName the display name
106: * @param toStringName the name to be rendered when {@code toString()} is called
107: * @return the new instance
108: * @since 3.2-ALPHA-1 (was {@code DefaultDisplayable}
109: *
110: ******************************************************************************************************************/
111: @Nonnull
112: public static Displayable of (@Nonnull final String displayName, @Nonnull final String toStringName)
113: {
114: return new DefaultDisplayable(displayName, toStringName);
115: }
116:
117: /*******************************************************************************************************************
118: *
119: * Creates an instance from a {@link Supplier}{@code <String>}. The supplier is invoked each time
120: * {@link #getDisplayName()} is called.
121: *
122: * @param supplier the {@code Supplier}
123: * @return the new instance
124: * @since 3.2-ALPHA-3
125: * @it.tidalwave.javadoc.experimental
126: *
127: ******************************************************************************************************************/
128: @Nonnull
129: public static Displayable of (@Nonnull final Supplier<String> supplier)
130: {
131: return supplier::get;
132: }
133:
134: /*******************************************************************************************************************
135: *
136: * Creates an instance from a {@link Function}{@code <T, String>} and a generic object that the function is applied
137: * to. The function is invoked each time {@link #getDisplayName()} is called.
138: *
139: * @param <T> the type of the object
140: * @param function the {@code Function}
141: * @param object the object
142: * @return the new instance
143: * @since 3.2-ALPHA-3
144: * @it.tidalwave.javadoc.experimental
145: *
146: ******************************************************************************************************************/
147: @Nonnull
148: public static <T> Displayable of (@Nonnull final Function<T, String> function, @Nonnull final T object)
149: {
150: return () -> function.apply(object);
151: }
152:
153: /*******************************************************************************************************************
154: *
155: * Creates a {@link LocalizedDisplayable} from a resource bundle. The bundle resource file is named
156: * {@code Bundle.properties} and it should be placed in the same package as the owner class.
157: *
158: * @param ownerClass the class that owns the bundle
159: * @param key the resource key
160: * @return the new instance
161: * @since 3.2-ALPHA-1 (was previously in {@code Displayable8}
162: *
163: ******************************************************************************************************************/
164: @Nonnull
165: public static LocalizedDisplayable fromBundle (@Nonnull final Class<?> ownerClass, @Nonnull final String key)
166: {
167: return new DefaultDisplayable(getMessage(ownerClass, key));
168: }
169:
170: /*******************************************************************************************************************
171: *
172: * Returns a {@link Comparator} for comparing two instances of {@code Displayable}.
173: *
174: * @return the {@code Comparator}
175: * @since 3.2-ALPHA-6
176: *
177: ******************************************************************************************************************/
178: @Nonnull
179: public static Comparator<Displayable> comparing()
180: {
181: return DisplayableComparator.getInstance();
182: }
183:
184: /*******************************************************************************************************************
185: *
186: * Returns a {@link Comparator} for comparing two instances of objects implementing {@code As} that contain the
187: * {@code Displayable} role.
188: *
189: * @return the {@code Comparator}
190: * @since 3.2-ALPHA-6
191: *
192: ******************************************************************************************************************/
193: @Nonnull
194: public static Comparator<As> asComparing()
195: {
196: return AsDisplayableComparator.getInstance();
197: }
198: }