Skip to content

Method: comparing()

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