Skip to content

Commit fe44881

Browse files
pandachrismaibin
authored andcommitted
BAEL-2565 (eugenp#6077)
* BAEL-2565 * BAEL-2565 Test code consistency
1 parent 956c2e7 commit fe44881

File tree

9 files changed

+467
-0
lines changed

9 files changed

+467
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.enums.values;
2+
3+
/**
4+
* This is a simple enum of periodic table elements
5+
*/
6+
public enum Element1 {
7+
H,
8+
HE,
9+
LI,
10+
BE,
11+
B,
12+
C,
13+
N,
14+
O,
15+
F,
16+
NE
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.enums.values;
2+
3+
/**
4+
* The simple enum has been enhanced to add the name of the element.
5+
*/
6+
public enum Element2 {
7+
H("Hydrogen"),
8+
HE("Helium"),
9+
LI("Lithium"),
10+
BE("Beryllium"),
11+
B("Boron"),
12+
C("Carbon"),
13+
N("Nitrogen"),
14+
O("Oxygen"),
15+
F("Flourine"),
16+
NE("Neon");
17+
18+
/** a final variable to store the label, which can't be changed */
19+
public final String label;
20+
21+
/**
22+
* A private constructor that sets the label.
23+
* @param label
24+
*/
25+
private Element2(String label) {
26+
this.label = label;
27+
}
28+
29+
/**
30+
* Look up Element2 instances by the label field. This implementation iterates through
31+
* the values() list to find the label.
32+
* @param label The label to look up
33+
* @return The Element2 instance with the label, or null if not found.
34+
*/
35+
public static Element2 valueOfLabel(String label) {
36+
for (Element2 e2 : values()) {
37+
if (e2.label.equals(label)) {
38+
return e2;
39+
}
40+
}
41+
return null;
42+
}
43+
44+
/**
45+
* Override the toString() method to return the label instead of the declared name.
46+
* @return
47+
*/
48+
@Override
49+
public String toString() {
50+
return this.label;
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.enums.values;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* A Map has been added to cache labels for faster lookup.
8+
*/
9+
public enum Element3 {
10+
H("Hydrogen"),
11+
HE("Helium"),
12+
LI("Lithium"),
13+
BE("Beryllium"),
14+
B("Boron"),
15+
C("Carbon"),
16+
N("Nitrogen"),
17+
O("Oxygen"),
18+
F("Flourine"),
19+
NE("Neon");
20+
21+
/**
22+
* A map to cache labels and their associated Element3 instances.
23+
* Note that this only works if the labels are all unique!
24+
*/
25+
private static final Map<String, Element3> BY_LABEL = new HashMap<>();
26+
27+
/** populate the BY_LABEL cache */
28+
static {
29+
for (Element3 e3 : values()) {
30+
BY_LABEL.put(e3.label, e3);
31+
}
32+
}
33+
34+
/** a final variable to store the label, which can't be changed */
35+
public final String label;
36+
37+
/**
38+
* A private constructor that sets the label.
39+
* @param label
40+
*/
41+
private Element3(String label) {
42+
this.label = label;
43+
}
44+
45+
/**
46+
* Look up Element2 instances by the label field. This implementation finds the
47+
* label in the BY_LABEL cache.
48+
* @param label The label to look up
49+
* @return The Element3 instance with the label, or null if not found.
50+
*/
51+
public static Element3 valueOfLabel(String label) {
52+
return BY_LABEL.get(label);
53+
}
54+
55+
/**
56+
* Override the toString() method to return the label instead of the declared name.
57+
* @return
58+
*/
59+
@Override
60+
public String toString() {
61+
return this.label;
62+
}
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.enums.values;
2+
3+
public interface Labeled {
4+
String label();
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.enums.values;
2+
3+
import org.junit.After;
4+
import org.junit.AfterClass;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
/**
11+
*
12+
* @author chris
13+
*/
14+
public class Element1UnitTest {
15+
16+
public Element1UnitTest() {
17+
}
18+
19+
@BeforeClass
20+
public static void setUpClass() {
21+
}
22+
23+
@AfterClass
24+
public static void tearDownClass() {
25+
}
26+
27+
@Before
28+
public void setUp() {
29+
}
30+
31+
@After
32+
public void tearDown() {
33+
}
34+
35+
@Test
36+
public void whenAccessingToString_thenItShouldEqualName() {
37+
for (Element1 e1 : Element1.values()) {
38+
assertEquals(e1.name(), e1.toString());
39+
}
40+
}
41+
42+
@Test
43+
public void whenCallingValueOf_thenReturnTheCorrectEnum() {
44+
for (Element1 e1 : Element1.values()) {
45+
assertSame(e1, Element1.valueOf(e1.name()));
46+
}
47+
}
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.enums.values;
7+
8+
import org.junit.After;
9+
import org.junit.AfterClass;
10+
import org.junit.Before;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
import static org.junit.Assert.*;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
/**
18+
*
19+
* @author chris
20+
*/
21+
public class Element2UnitTest {
22+
private static final Logger LOGGER = LoggerFactory.getLogger(Element2UnitTest.class);
23+
24+
public Element2UnitTest() {
25+
}
26+
27+
@BeforeClass
28+
public static void setUpClass() {
29+
}
30+
31+
@AfterClass
32+
public static void tearDownClass() {
33+
}
34+
35+
@Before
36+
public void setUp() {
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
}
42+
43+
@Test
44+
public void whenLocatebyLabel_thenReturnCorrectValue() {
45+
for (Element2 e2 : Element2.values()) {
46+
assertSame(e2, Element2.valueOfLabel(e2.label));
47+
}
48+
}
49+
50+
/**
51+
* Test of toString method, of class Element2.
52+
*/
53+
@Test
54+
public void whenCallingToString_thenReturnLabel() {
55+
for (Element2 e2 : Element2.values()) {
56+
assertEquals(e2.label, e2.toString());
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)