|
| 1 | +package com.baeldung.enums.values; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * Multiple fields have been added and the Labeled interface is implemented. |
| 8 | + */ |
| 9 | +public enum Element4 implements Labeled { |
| 10 | + H("Hydrogen", 1, 1.008f), |
| 11 | + HE("Helium", 2, 4.0026f), |
| 12 | + LI("Lithium", 3, 6.94f), |
| 13 | + BE("Beryllium", 4, 9.01722f), |
| 14 | + B("Boron", 5, 10.81f), |
| 15 | + C("Carbon", 6, 12.011f), |
| 16 | + N("Nitrogen", 7, 14.007f), |
| 17 | + O("Oxygen", 8, 15.999f), |
| 18 | + F("Flourine", 9, 18.998f), |
| 19 | + NE("Neon", 10, 20.180f); |
| 20 | + /** |
| 21 | + * Maps cache labels and their associated Element3 instances. |
| 22 | + * Note that this only works if the values are all unique! |
| 23 | + */ |
| 24 | + private static final Map<String, Element4> BY_LABEL = new HashMap<>(); |
| 25 | + private static final Map<Integer, Element4> BY_ATOMIC_NUMBER = new HashMap<>(); |
| 26 | + private static final Map<Float, Element4> BY_ATOMIC_WEIGHT = new HashMap<>(); |
| 27 | + |
| 28 | + /** populate the caches */ |
| 29 | + static { |
| 30 | + for (Element4 e4 : values()) { |
| 31 | + BY_LABEL.put(e4.label, e4); |
| 32 | + BY_ATOMIC_NUMBER.put(e4.atomicNumber, e4); |
| 33 | + BY_ATOMIC_WEIGHT.put(e4.atomicWeight, e4); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** final variables to store the values, which can't be changed */ |
| 38 | + public final String label; |
| 39 | + public final int atomicNumber; |
| 40 | + public final float atomicWeight; |
| 41 | + |
| 42 | + private Element4(String label, int atomicNumber, float atomicWeight) { |
| 43 | + this.label = label; |
| 44 | + this.atomicNumber = atomicNumber; |
| 45 | + this.atomicWeight = atomicWeight; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Implement the Labeled interface. |
| 50 | + * @return the label value |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public String label() { |
| 54 | + return label; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Look up Element2 instances by the label field. This implementation finds the |
| 59 | + * label in the BY_LABEL cache. |
| 60 | + * @param label The label to look up |
| 61 | + * @return The Element4 instance with the label, or null if not found. |
| 62 | + */ |
| 63 | + public static Element4 valueOfLabel(String label) { |
| 64 | + return BY_LABEL.get(label); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Look up Element2 instances by the atomicNumber field. This implementation finds the |
| 69 | + * atomicNUmber in the cache. |
| 70 | + * @param number The atomicNumber to look up |
| 71 | + * @return The Element4 instance with the label, or null if not found. |
| 72 | + */ |
| 73 | + public static Element4 valueOfAtomicNumber(int number) { |
| 74 | + return BY_ATOMIC_NUMBER.get(number); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Look up Element2 instances by the atomicWeight field. This implementation finds the |
| 79 | + * atomic weight in the cache. |
| 80 | + * @param weight the atomic weight to look up |
| 81 | + * @return The Element4 instance with the label, or null if not found. |
| 82 | + */ |
| 83 | + public static Element4 valueOfAtomicWeight(float weight) { |
| 84 | + return BY_ATOMIC_WEIGHT.get(weight); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Override the toString() method to return the label instead of the declared name. |
| 89 | + * @return |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + return this.label; |
| 94 | + } |
| 95 | +} |
0 commit comments