|
3 | 3 | # java11-enum-clean-code
|
4 | 4 | Overview of how to to construct modern enums using lambda.
|
5 | 5 |
|
| 6 | +# enum compliation |
| 7 | +* https://stackoverflow.com/questions/32354107/how-are-enums-internally-represented-in-java |
| 8 | +* only syntactic sugar |
| 9 | + |
| 10 | +``` |
| 11 | +public enum Ordinals { |
| 12 | + FIRST("st"), |
| 13 | + SECOND("nd"), |
| 14 | + THIRD("rd"); |
| 15 | + private String notation; |
| 16 | + private Ordinals(String notation) { |
| 17 | + this.notation = notation; |
| 18 | + } |
| 19 | + public String getNotation() { |
| 20 | + return notation; |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +``` |
| 26 | +public final class Ordinals extends java.lang.Enum<Ordinals> { |
| 27 | + public static final Ordinals FIRST; |
| 28 | +
|
| 29 | + public static final Ordinals SECOND; |
| 30 | +
|
| 31 | + public static final Ordinals THIRD; |
| 32 | +
|
| 33 | + private String notation; |
| 34 | +
|
| 35 | + private static final Ordinals[] $VALUES; |
| 36 | +
|
| 37 | + public static Ordinals[] values() { |
| 38 | + return $VALUES.clone(); |
| 39 | + } |
| 40 | +
|
| 41 | + public static Ordinals valueOf(String name) { |
| 42 | + return (Ordinals) Enum.valueOf(Ordinals.class, name); |
| 43 | + } |
| 44 | +
|
| 45 | + private Ordinals(String name, int ordinal, String notation) { |
| 46 | + super(name, ordinal); |
| 47 | + this.notation = notation |
| 48 | + } |
| 49 | +
|
| 50 | + static { |
| 51 | + FIRST = new Ordinals("FIRST", 0, "st"); |
| 52 | + SECOND = new Ordinals("SECOND", 1, "nd"); |
| 53 | + THIRD = new Ordinals("THIRD", 2, "rd"); |
| 54 | + Ordinals[] $VALUES = new Ordinals[3]; |
| 55 | + $VALUES[0] = FIRST; |
| 56 | + $VALUES[1] = SECOND; |
| 57 | + $VALUES[2] = THIRD; |
| 58 | + Ordinals.$VALUES = $VALUES; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
6 | 63 | # project description
|
7 | 64 | We have enum `JobTitle`
|
8 | 65 |
|
|
0 commit comments