-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumExample.java
More file actions
449 lines (350 loc) · 18.9 KB
/
EnumExample.java
File metadata and controls
449 lines (350 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package DataStructures;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.lang.Enum.EnumDesc;
import java.lang.constant.ClassDesc;;
/**
* <pre>
* public enum must be in a own file, and so we can have n number of default enums in a file
*
* public abstract class Enum<E extends Enum<E>> implements Constable, Comparable<E>, Serializable {
* public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V> implements java.io.Serializable, Cloneable
* public abstract sealed class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, java.io.Serializable permits JumboEnumSet, RegularEnumSet
*
* Two types of Enums: 1. Standard Enum, 2. Constructor Enum
* Use standard enums when you need a simple enumeration of constants.
* Use constructor enums when you need to associate additional data or behavior with each constant.
*
* So, enum can be converted to other types like below:
* to Arrays using EnumClass.values() or Arrays.asList(EnumClass.class.getEnumConstants()),
* to List using Arrays.asList(EnumClass.values()) or Arrays.asList(EnumClass.class.getEnumConstants()) or new ArrayList<>(EnumClass.values()) or Stream.of(WeekDay.values()).collect(Collectors.toList()),
* to EnumSet using EnumSet.allOf(EnumClass.class),
* to EnumMap using new EnumMap<>(EnumClass.class),
* to Stream using Arrays.stream(EnumClass.values()) or Stream.of(EnumClass.values()) or Arrays.stream(EnumClass.class.getEnumConstants())
* and later on we can convert them to traditional Map, List, Set and so on
*
* see {@link DataStructures.WeekDay} for standard enum -> no constructor
* see {@link DataStructures.Roman} for single parameter constructor
* see {@link DataStructures.Frequency} for multi parameter constructor
*
* see {@link DataStructures.SingletonEnum} for SINGLETON ENUMS -> ENUM static and non-static variables 🔥
* --------------------------------------------------------------
* 1) Enums are Singletons, and so there is only one instance of each enum constant ---> use them like a Singleton bean
* 2) Enums are sealed and final, and so we cannot extend them
* 3) We can have constructor, variables, methods, etc.
* 4) They are not thread-safe so use variables in a synchronized block or concurrent collections like AtomicInteger
* 5) So, if we change the variable value in enum in classA, it will be changed globally and we get the new value in classB
* 6) Same like static and non-static methods, we access static by WeekDay.getStaticValue() and non-static by WeekDay.MONDAY.getNonStaticValue()
*
* {@link DataStructures.EnumMethods} ENUM with static, non-static and abstract methods 🔥
* --------------------------------------------------------------------------------------
* 1) If non-static method then we have to get the method from the enum constant like WeekDay.MONDAY.print()
* 2) If static method then we have to get the method from the enum like WeekDay.print(), no need to get the method from the enum constant
* 3) Declare the abstract method in enum and override it in the enum constant like CONSTANT { @Override public void print() } --> all constants must override the method
*
*
* </pre>
*
* @see {@linkplain java.lang.Enum java.lang.Enum} - since JDK 1.5 2004
* @see {@linkplain java.util.EnumMap java.util.EnumMap} - since JDK 1.5 2004
* @see {@linkplain java.util.EnumSet java.util.EnumSet} - since JDK 1.5 2004
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 17 Oct 2024
*
*/
public class EnumExample {
@SuppressWarnings("unlikely-arg-type")
public static void main(String[] args) {
// WeekDay - standard enum operations
System.out.println("WeekDay enum ----------------");
WeekDay dayEnum = WeekDay.MONDAY;
String day1 = WeekDay.MONDAY.name(); // built-in final static method, same as toSting()
WeekDay day1Enum = WeekDay.valueOf(day1); // throws IllegalArgumentException if the value is not in the enum
day1Enum = WeekDay.valueOf("MONDAY"); // built-in final static method & same as above
String day2 = WeekDay.MONDAY.toString(); // built-in non-final static & can be @Override as shown inside the WeekDay enum
int dayIndex = WeekDay.MONDAY.ordinal(); // built-in final static method for index
WeekDay[] days = WeekDay.values(); // built-in final static method
Class<? extends Enum<?>> decClass = day1Enum.getDeclaringClass(); // prints class DataStructures.WeekDay
/**/ System.out.println(WeekDay.SUNDAY); /* prints Sunday --> @Override toString() custom method in WeekDay enum */
/**/ System.out.println(WeekDay.SUNDAY.name()); /* prints SUNDAY i.e specific Enum constant as String */
/**/ System.out.println(WeekDay.SUNDAY.toString()); /* prints Sunday --> @Override toString() custom method in WeekDay enum */
System.out.println(WeekDay.MONDAY.equals(day1)); // false because day1 is a string but .equals(Object param)
System.out.println(WeekDay.MONDAY.equals(day1Enum)); // true always compare enum to enum or string to string
System.out.println(WeekDay.MONDAY.name().equals(day1)); // true as we compare string to string
System.out.println(dayEnum);
System.out.println(day1);
System.out.println(day1Enum);
System.out.println(day2);
System.out.println(dayIndex);
System.out.println(days);
System.out.println(decClass);
// Enum Descriptor --- with private constructor and one .of() static method
System.out.println("Enum Descriptor -----------------"); // TODO
Optional<EnumDesc<WeekDay>> enumDesc1 = WeekDay.MONDAY.describeConstable();
System.out.println(enumDesc1.get().constantName());
EnumDesc<WeekDay> enumDesc2 = EnumDesc.of(ClassDesc.of("DataStructures", "EnumExample$WeekDay"), "MONDAY");
System.out.println(enumDesc2);
System.out.println(enumDesc2.constantName());
// to Array
System.out.println("Array -----------------");
WeekDay[] daysArr = WeekDay.values();
daysArr = WeekDay.class.getEnumConstants(); // same as above
System.out.println(Arrays.toString(daysArr));
// to List
System.out.println("List -----------------");
List<WeekDay> daysList = Arrays.asList(WeekDay.values());
daysList = new ArrayList<>(Arrays.asList(WeekDay.values())); // same as above
daysList = Arrays.asList(WeekDay.class.getEnumConstants()); // same as above
daysList = Stream.of(WeekDay.values()).collect(Collectors.toList()); // same as above
System.out.println(daysList);
// to EnumMap
System.out.println("EnumMap -----------------");
EnumMap<WeekDay, String> scheduleEnumMap = new EnumMap<>(WeekDay.class);
scheduleEnumMap.put(WeekDay.MONDAY, "Work"); // Adding elements or custom constructor parameter to the EnumMap
scheduleEnumMap.put(WeekDay.TUESDAY, "Work");
scheduleEnumMap.put(WeekDay.WEDNESDAY, "Study");
scheduleEnumMap.put(WeekDay.THURSDAY, "Study");
scheduleEnumMap.put(WeekDay.FRIDAY, "Relax");
System.out.println(scheduleEnumMap); // Output: Work
System.out.println(scheduleEnumMap.get(WeekDay.MONDAY)); // Output: Work
System.out.println(scheduleEnumMap.get(WeekDay.FRIDAY)); // Output: Relax
// to EnumSet
System.out.println("EnumSet -----------------");
EnumSet<WeekDay> weekDaysSet = EnumSet.allOf(WeekDay.class);
int mondayOrdinal = EnumSet.allOf(WeekDay.class).stream().filter(d->d.name().equals("MONDAY")).map(d->d.ordinal()).findFirst().orElse(0);
EnumSet<WeekDay> weekDaysRangeSet = EnumSet.range(WeekDay.MONDAY, WeekDay.FRIDAY); // from MONDAY to FRIDAY
EnumSet<WeekDay> weekDaysEnumSet = EnumSet.of(WeekDay.MONDAY, WeekDay.TUESDAY); // only mentioned enums
EnumSet.of(WeekDay.MONDAY, WeekDay.TUESDAY).contains(WeekDay.MONDAY); // same like Arrays.asList().contains()
weekDaysEnumSet.add(WeekDay.WEDNESDAY);
weekDaysEnumSet.remove(WeekDay.MONDAY);
EnumSet<WeekDay> weekDaysCopy = EnumSet.copyOf(weekDaysEnumSet);
EnumSet<WeekDay> weekDaysClear = EnumSet.noneOf(WeekDay.class);
weekDaysClear.addAll(weekDaysEnumSet);
EnumSet.complementOf(weekDaysEnumSet).forEach(System.out::println);
System.out.println(mondayOrdinal);
System.out.println(weekDaysSet);
System.out.println(weekDaysRangeSet);
System.out.println(weekDaysEnumSet);
System.out.println(weekDaysCopy);
System.out.println(weekDaysClear);
// to Stream and other types like Map, List, Set
System.out.println("Enum with Stream -----------------");
Stream<WeekDay> enumStream = Stream.of(WeekDay.values());
enumStream = Stream.of(WeekDay.class.getEnumConstants()); // same as above
enumStream = Arrays.stream(WeekDay.values()); // same as above
enumStream = Arrays.stream(WeekDay.class.getEnumConstants()); // same as above
enumStream = Arrays.asList(WeekDay.values()).stream(); // same as above
enumStream = Arrays.asList(WeekDay.class.getEnumConstants()).stream(); // same as above
WeekDay day = Stream.of(WeekDay.values()).filter(d->d.name().equals("MONDAY")).findFirst().get();
Map<String, WeekDay> scheduleMap = Stream.of(WeekDay.values()).collect(Collectors.toMap(WeekDay::name, Function.identity()));
Map<WeekDay, String> scheduleMap2 = Stream.of(WeekDay.values()).collect(Collectors.toMap(Function.identity(), WeekDay::name));
enumStream.forEach(System.out::println);
System.out.println(day);
System.out.println(scheduleMap);
System.out.println(scheduleMap2);
// Roman enum (Constructor with 1 parameter)
System.out.println("Roman enum ----------------");
Roman romanEnum = Roman.I;
romanEnum = Roman.valueOf("I");
System.out.println(romanEnum);
// System.out.println(romanEnum.value); --- if value class variable is not private
System.out.println(romanEnum.getValue());
System.out.println(Roman.valueOf("L"));
System.out.println(Roman.valueOf("D").getValue()); // java.lang.IllegalArgumentException if we use small "d", i.e enum is case sensitive
System.out.println(Arrays.toString(Roman.values())); // .values() is a built-in final static method
// Frequency enum (Constructor with 2 parameters)
System.out.println("Frequency enum ----------------");
String frequency = "Every Week";
Frequency freqEnum = Frequency.WEEKLY; // by default a constructor initializes the EVERY WEEK enum and all the static methods can be used
System.out.println(Frequency.isFrequency(frequency));
System.out.println(Frequency.toCode(frequency));
System.out.println(Frequency.getValueByDisplayName(frequency));
System.out.println(freqEnum.getDisplayName());
System.out.println(freqEnum.getCode());
System.out.println(freqEnum.name());
System.out.println(freqEnum.ordinal());
// SINGLETON ENUM
System.out.println("\n\nSingleton Enum ----------------");
SingletonEnum.INSTANCE.print();
SingletonEnum.INSTANCE.setValue(10);
System.out.printf("We set the SingletonEnum.INSTANCE value to %s, but we didn't changed the other constants%n", SingletonEnum.INSTANCE.getValue());
System.out.printf("SingletonEnum.ONE.getValue() -> %s%n", SingletonEnum.ONE.getValue());
System.out.printf("SingletonEnum.INSTANCE.getValue() -> %s%n", SingletonEnum.INSTANCE.getValue());
System.out.printf("SingletonEnum.getStaticValue() -> %s%n", SingletonEnum.getStaticValue());
// STATIC, NON-STATIC AND ABSTRACT METHODS IN ENUM
System.out.println("\n\nStatic, Non-Static and Abstract Methods in enum ----------------");
System.out.printf("ABSTRACT METHOD ---> Operation.ADD.apply(1,2) = %s\n", EnumMethods.ADD.apply(1,2));
EnumMethods.ADD.print();
EnumMethods.printAll(); // ---> HERE IN STATIC METHOD, NO NEED TO CALL ENUM CONSTANT LIKE Operation.ADD.printAll();
}
}
// for static and non-static variables
enum SingletonEnum {
INSTANCE, ONE, TWO, THREE;
private int value = 0; // can only be accessed by SingletonEnum.CONSTANT.getValue()
private static int staticValue = 0; // can only be accessed by SingletonEnum.getStaticValue() // no need of SingletonEnum.CONSTANT
public void print() {
System.out.printf("Singleton Enum was called by %s constant\n", this.name()); // this.name() is the constant that we used to invoke this method
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static void setStaticValue(int value) {
staticValue = value;
}
public static int getStaticValue() {
return staticValue;
}
}
// for static, non-static and abstract methods
enum EnumMethods {
ADD {
@Override // annotation is optional
public int apply(int a, int b) { return a + b; }
},
SUBTRACT {
@Override // annotation is optional
public int apply(int a, int b) { return a - b; }
};
public abstract int apply(int a, int b); // we can implement this abstract method inside the enum constants like ADD and SUBTRACT
public void print() {
System.out.printf("Operation enum non-static method print() is called by %s =\n", this.name());
}
public static void printAll() {
System.out.println("Operation enum static method printAll() is called");
Arrays.stream(values()).forEach(EnumMethods::print); // 🔥 we can use Operation.values() or just use values() directly
}
}
// Standard Enum --> no constructor
enum WeekDay {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
@Override
public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
// print all enum values
public static void printAll() {
Arrays.stream(values()).forEach(System.out::println); // 🔥 we can use WeekDay.values() or just use values() directly
}
}
// Constructor Enum with 1 non-final parameter
enum Roman {
I(1), V(5), X(10), L(50), C(100), D(500), M(1000);
int value; // Must be "non-static" and "private" & "final" is optional and if private then param can only be accessed by getter i.e getValue()
int foo; // extra variable --> just for testing purpose
Roman(int value) { // constructor is private by default
this.value = value;
}
public int getValue() { // optional for non-private value variable
return value;
}
}
// Constructor Enum with 2 final parameters and public static methods
enum Frequency {
ONCE("Once", "1"), // code can be String or int but we still have the ordinal() built-in static method
WEEKLY("Every Week", "2"),
EVERY_TWO_WEEKS("Every Two Weeks", "3"),
EVERY_FIRST_AND_FIFTEENTH("Every 1st and 15th of the Month", "4"),
MONTHLY("Monthly", "5"),
EVERY_TWO_MONTHS("Every Two Months", "6"),
QUARTERLY("Quarterly", "7"),
SEMI_ANNUALLY("Semi-Annually", "8"),
ANNUALLY("Annually", "9");
private final String displayName; // final because it is initialized in the constructor and cannot be changed
private final String code;
public String getDisplayName() {
return displayName;
}
public String getCode() {
return code;
}
// enum constructor is always a private by default because -> Frequency foo = new Frequency("new day","44"); will not work like class
Frequency(String displayName, String code) {
this.displayName = displayName;
this.code = code;
}
/*
* @TimeComplexity O(1)
is item present in the enum -----
*/
public static boolean isFrequency(String frequency) {
try {
Frequency.valueOf(frequency); // or Frequency.valueOf(frequency.toUpperCase());
} catch (IllegalArgumentException e) {
return false;
}
return true;
}
/*
* @TimeComplexity O(1)
*
*/
public static boolean isFrequency2(String frequency) {
try {
Enum.valueOf(Frequency.class, frequency); // or Enum.valueOf(Frequency.class, frequency.toUpperCase());
} catch (IllegalArgumentException e) {
return false;
}
return true;
}
/*
* @TimeComplexity O(n)
*
* WeekDay.values() is same as values()
*/
public static boolean isFrequency3(String frequency) {
return Stream.of(values()).anyMatch(lifeFrequency -> lifeFrequency.name().equalsIgnoreCase(frequency));
// return Stream.of(WeekDay.values()).anyMatch(lifeFrequency -> lifeFrequency.name().equalsIgnoreCase(frequency));
}
/*
NOTE:
-----
If you know for sure, your enum constants are uppercase (like MONDAY, ACTIVE), then:
Frequency.valueOf(frequency.toUpperCase()); ---------
is clean, fast, and preferred.
If you aren’t sure whether enum constants use uppercase, camel case (Monday), or mixed case, then:
Arrays.stream(Frequency.values())
.anyMatch(f -> f.name().equalsIgnoreCase(frequency)); ---------
or a similar .equalsIgnoreCase() approach
is safer and more flexible, though slower (O(n)).
*/
// EnumDescriptor ---- used to provide metadata about an enum class
// public static boolean isFrequency3(String frequency) {
// return EnumDesc
// }
// get item if present -----
public static String toCode(String frequency) {
try {
return Frequency.valueOf(frequency).code; // or EnumSet.allOf(Frequency.class).stream().filter(d->d.name().equals("WEEKLY")).map(d->d.getCode()).findFirst().orElse("N/A");
} catch (IllegalArgumentException e) {
System.out.println("Unable to map requested frequency " + frequency + " to field value");
}
return "N/A";
// throw new FrequencyMappingException("Unable to map requested frequency " + frequency + to field value.") ");
}
// get item if present - alternate method ------
// If we compare with only getDisplayName() then it will fail for "Semi-Annually" because of "-" in the display name. So, use both(getDisplayName() & name()) or replace getDisplayName() with name() or use above toCode() method instead
public static String getValueByDisplayName(String frequency) {
return Arrays.stream(Frequency.values())
.filter(lifeFrequency -> lifeFrequency.getDisplayName().equalsIgnoreCase(frequency) || lifeFrequency.name().equalsIgnoreCase(frequency))
.findFirst()
.map(Frequency::getCode)
.orElse("N/A");
}
}
class FrequencyMappingException extends Exception {
public FrequencyMappingException(String message) {
super(message);
}
}