-
Notifications
You must be signed in to change notification settings - Fork 17
/
CustomEnum.java
55 lines (45 loc) · 1.61 KB
/
CustomEnum.java
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
package by.andd3dfx.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Imagine situation when Enum isn't present in Java and write its custom implementation.
*
* @see <a href="https://youtu.be/41sJn7MQFc4">Video solution</a>
*/
public class CustomEnum {
private static List<CustomEnum> items = new ArrayList<>();
private static Map<String, CustomEnum> lookup = new HashMap<>();
private final String value;
private final int ordinal;
public static final CustomEnum MONDAY = add("MONDAY");
public static final CustomEnum TUESDAY = add("TUESDAY");
public static final CustomEnum WEDNESDAY = add("WEDNESDAY");
public static final CustomEnum THURSDAY = add("THURSDAY");
public static final CustomEnum FRIDAY = add("FRIDAY");
public static final CustomEnum SATURDAY = add("SATURDAY");
public static final CustomEnum SUNDAY = add("SUNDAY");
public CustomEnum(String value, int ordinal) {
this.value = value;
this.ordinal = ordinal;
}
public static CustomEnum[] values() {
return items.toArray(new CustomEnum[0]);
}
public static CustomEnum valueOf(String value) {
return lookup.get(value);
}
public String name() {
return value;
}
public int ordinal() {
return ordinal;
}
private static CustomEnum add(String value) {
CustomEnum customEnum = new CustomEnum(value, items.size());
items.add(customEnum);
lookup.put(value, customEnum);
return customEnum;
}
}