JAVA-622: Moved code from core-java-lang to core-java-lang-oop-types
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
/**
|
||||
* This is a simple enum of periodic table elements
|
||||
*/
|
||||
public enum Element1 {
|
||||
H,
|
||||
HE,
|
||||
LI,
|
||||
BE,
|
||||
B,
|
||||
C,
|
||||
N,
|
||||
O,
|
||||
F,
|
||||
NE
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
/**
|
||||
* The simple enum has been enhanced to add the name of the element.
|
||||
*/
|
||||
public enum Element2 {
|
||||
H("Hydrogen"),
|
||||
HE("Helium"),
|
||||
LI("Lithium"),
|
||||
BE("Beryllium"),
|
||||
B("Boron"),
|
||||
C("Carbon"),
|
||||
N("Nitrogen"),
|
||||
O("Oxygen"),
|
||||
F("Flourine"),
|
||||
NE("Neon");
|
||||
|
||||
/** a final variable to store the label, which can't be changed */
|
||||
public final String label;
|
||||
|
||||
/**
|
||||
* A private constructor that sets the label.
|
||||
* @param label
|
||||
*/
|
||||
private Element2(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up Element2 instances by the label field. This implementation iterates through
|
||||
* the values() list to find the label.
|
||||
* @param label The label to look up
|
||||
* @return The Element2 instance with the label, or null if not found.
|
||||
*/
|
||||
public static Element2 valueOfLabel(String label) {
|
||||
for (Element2 e2 : values()) {
|
||||
if (e2.label.equals(label)) {
|
||||
return e2;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the toString() method to return the label instead of the declared name.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.label;
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A Map has been added to cache labels for faster lookup.
|
||||
*/
|
||||
public enum Element3 {
|
||||
H("Hydrogen"),
|
||||
HE("Helium"),
|
||||
LI("Lithium"),
|
||||
BE("Beryllium"),
|
||||
B("Boron"),
|
||||
C("Carbon"),
|
||||
N("Nitrogen"),
|
||||
O("Oxygen"),
|
||||
F("Flourine"),
|
||||
NE("Neon");
|
||||
|
||||
/**
|
||||
* A map to cache labels and their associated Element3 instances.
|
||||
* Note that this only works if the labels are all unique!
|
||||
*/
|
||||
private static final Map<String, Element3> BY_LABEL = new HashMap<>();
|
||||
|
||||
/** populate the BY_LABEL cache */
|
||||
static {
|
||||
for (Element3 e3 : values()) {
|
||||
BY_LABEL.put(e3.label, e3);
|
||||
}
|
||||
}
|
||||
|
||||
/** a final variable to store the label, which can't be changed */
|
||||
public final String label;
|
||||
|
||||
/**
|
||||
* A private constructor that sets the label.
|
||||
* @param label
|
||||
*/
|
||||
private Element3(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up Element2 instances by the label field. This implementation finds the
|
||||
* label in the BY_LABEL cache.
|
||||
* @param label The label to look up
|
||||
* @return The Element3 instance with the label, or null if not found.
|
||||
*/
|
||||
public static Element3 valueOfLabel(String label) {
|
||||
return BY_LABEL.get(label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the toString() method to return the label instead of the declared name.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.label;
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Multiple fields have been added and the Labeled interface is implemented.
|
||||
*/
|
||||
public enum Element4 implements Labeled {
|
||||
H("Hydrogen", 1, 1.008f),
|
||||
HE("Helium", 2, 4.0026f),
|
||||
LI("Lithium", 3, 6.94f),
|
||||
BE("Beryllium", 4, 9.01722f),
|
||||
B("Boron", 5, 10.81f),
|
||||
C("Carbon", 6, 12.011f),
|
||||
N("Nitrogen", 7, 14.007f),
|
||||
O("Oxygen", 8, 15.999f),
|
||||
F("Flourine", 9, 18.998f),
|
||||
NE("Neon", 10, 20.180f);
|
||||
/**
|
||||
* Maps cache labels and their associated Element3 instances.
|
||||
* Note that this only works if the values are all unique!
|
||||
*/
|
||||
private static final Map<String, Element4> BY_LABEL = new HashMap<>();
|
||||
private static final Map<Integer, Element4> BY_ATOMIC_NUMBER = new HashMap<>();
|
||||
private static final Map<Float, Element4> BY_ATOMIC_WEIGHT = new HashMap<>();
|
||||
|
||||
/** populate the caches */
|
||||
static {
|
||||
for (Element4 e4 : values()) {
|
||||
BY_LABEL.put(e4.label, e4);
|
||||
BY_ATOMIC_NUMBER.put(e4.atomicNumber, e4);
|
||||
BY_ATOMIC_WEIGHT.put(e4.atomicWeight, e4);
|
||||
}
|
||||
}
|
||||
|
||||
/** final variables to store the values, which can't be changed */
|
||||
public final String label;
|
||||
public final int atomicNumber;
|
||||
public final float atomicWeight;
|
||||
|
||||
private Element4(String label, int atomicNumber, float atomicWeight) {
|
||||
this.label = label;
|
||||
this.atomicNumber = atomicNumber;
|
||||
this.atomicWeight = atomicWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement the Labeled interface.
|
||||
* @return the label value
|
||||
*/
|
||||
@Override
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up Element2 instances by the label field. This implementation finds the
|
||||
* label in the BY_LABEL cache.
|
||||
* @param label The label to look up
|
||||
* @return The Element4 instance with the label, or null if not found.
|
||||
*/
|
||||
public static Element4 valueOfLabel(String label) {
|
||||
return BY_LABEL.get(label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up Element2 instances by the atomicNumber field. This implementation finds the
|
||||
* atomicNUmber in the cache.
|
||||
* @param number The atomicNumber to look up
|
||||
* @return The Element4 instance with the label, or null if not found.
|
||||
*/
|
||||
public static Element4 valueOfAtomicNumber(int number) {
|
||||
return BY_ATOMIC_NUMBER.get(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up Element2 instances by the atomicWeight field. This implementation finds the
|
||||
* atomic weight in the cache.
|
||||
* @param weight the atomic weight to look up
|
||||
* @return The Element4 instance with the label, or null if not found.
|
||||
*/
|
||||
public static Element4 valueOfAtomicWeight(float weight) {
|
||||
return BY_ATOMIC_WEIGHT.get(weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the toString() method to return the label instead of the declared name.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.label;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
public interface Labeled {
|
||||
String label();
|
||||
}
|
||||
Reference in New Issue
Block a user