parent
956c2e7dd6
commit
fe44881bb1
@ -0,0 +1,17 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
public interface Labeled {
|
||||
String label();
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Element1UnitTest {
|
||||
|
||||
public Element1UnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAccessingToString_thenItShouldEqualName() {
|
||||
for (Element1 e1 : Element1.values()) {
|
||||
assertEquals(e1.name(), e1.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingValueOf_thenReturnTheCorrectEnum() {
|
||||
for (Element1 e1 : Element1.values()) {
|
||||
assertSame(e1, Element1.valueOf(e1.name()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Element2UnitTest {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Element2UnitTest.class);
|
||||
|
||||
public Element2UnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocatebyLabel_thenReturnCorrectValue() {
|
||||
for (Element2 e2 : Element2.values()) {
|
||||
assertSame(e2, Element2.valueOfLabel(e2.label));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class Element2.
|
||||
*/
|
||||
@Test
|
||||
public void whenCallingToString_thenReturnLabel() {
|
||||
for (Element2 e2 : Element2.values()) {
|
||||
assertEquals(e2.label, e2.toString());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Element3UnitTest {
|
||||
|
||||
public Element3UnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocatebyLabel_thenReturnCorrectValue() {
|
||||
for (Element3 e3 : Element3.values()) {
|
||||
assertSame(e3, Element3.valueOfLabel(e3.label));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class Element3.
|
||||
*/
|
||||
@Test
|
||||
public void whenCallingToString_thenReturnLabel() {
|
||||
for (Element3 e3 : Element3.values()) {
|
||||
assertEquals(e3.label, e3.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.enums.values;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Element4UnitTest {
|
||||
|
||||
public Element4UnitTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocatebyLabel_thenReturnCorrectValue() {
|
||||
for (Element4 e4 : Element4.values()) {
|
||||
assertSame(e4, Element4.valueOfLabel(e4.label));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocatebyAtmNum_thenReturnCorrectValue() {
|
||||
for (Element4 e4 : Element4.values()) {
|
||||
assertSame(e4, Element4.valueOfAtomicNumber(e4.atomicNumber));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLocatebyAtmWt_thenReturnCorrectValue() {
|
||||
for (Element4 e4 : Element4.values()) {
|
||||
assertSame(e4, Element4.valueOfAtomicWeight(e4.atomicWeight));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class Element4.
|
||||
*/
|
||||
@Test
|
||||
public void whenCallingToString_thenReturnLabel() {
|
||||
for (Element4 e4 : Element4.values()) {
|
||||
assertEquals(e4.label, e4.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user