Skip to content

Instantly share code, notes, and snippets.

@dfparker2002
Created January 6, 2019 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfparker2002/85147bf6f84728a27161543030f88bd3 to your computer and use it in GitHub Desktop.
Save dfparker2002/85147bf6f84728a27161543030f88bd3 to your computer and use it in GitHub Desktop.
Example of Enum Singleton with internal lookup
import java.util.HashMap;
import java.util.Map;
/**
* A Map has been added to cache labels for faster lookup.
* from https://github.com/pandachris/tutorials/blob/53e6490c61cf0b6a41ab31e394b1472188987bc9/core-java-8/src/main/java/com/baeldung/enums/values/Element3.java
*/
public enum EnumSingletonElement3 {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment