Skip to content

Commit bd35c24

Browse files
authored
Bael 9484 update article "attaching values to java enums" (#18917)
* Update Element2.java * Update Element3.java * Update Element4.java * Update Element2UnitTest.java
1 parent 151a48e commit bd35c24

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/values/Element2.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.enums.values;
22

3+
import java.util.Objects;
4+
35
/**
46
* The simple enum has been enhanced to add the name of the element.
57
*/
@@ -12,8 +14,11 @@ public enum Element2 {
1214
C("Carbon"),
1315
N("Nitrogen"),
1416
O("Oxygen"),
15-
F("Flourine"),
16-
NE("Neon");
17+
// Fixed: "Flourine" -> "Fluorine"
18+
F("Fluorine"),
19+
NE("Neon"),
20+
// Added: Dedicated UNKNOWN member
21+
UNKNOWN("Unknown Element");
1722

1823
/** a final variable to store the label, which can't be changed */
1924
public final String label;
@@ -30,15 +35,17 @@ private Element2(String label) {
3035
* Look up Element2 instances by the label field. This implementation iterates through
3136
* the values() list to find the label.
3237
* @param label The label to look up
33-
* @return The Element2 instance with the label, or null if not found.
38+
* @return The Element2 instance with the label, or UNKNOWN if not found.
3439
*/
3540
public static Element2 valueOfLabel(String label) {
3641
for (Element2 e2 : values()) {
37-
if (e2.label.equals(label)) {
42+
// Fixed: Used Objects.equals for null-safe comparison
43+
if (Objects.equals(e2.label, label)) {
3844
return e2;
3945
}
4046
}
41-
return null;
47+
// Fixed: Return UNKNOWN instead of null
48+
return UNKNOWN;
4249
}
4350

4451
/**

core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/values/Element3.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public enum Element3 {
1515
C("Carbon"),
1616
N("Nitrogen"),
1717
O("Oxygen"),
18-
F("Flourine"),
18+
// Fixed: "Flourine" -> "Fluorine"
19+
F("Fluorine"),
1920
NE("Neon");
2021

21-
/**
22-
* A map to cache labels and their associated Element3 instances.
22+
/** * A map to cache labels and their associated Element3 instances.
2323
* Note that this only works if the labels are all unique!
2424
*/
2525
private static final Map<String, Element3> BY_LABEL = new HashMap<>();

core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/values/Element4.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public enum Element4 implements Labeled {
1515
C("Carbon", 6, 12.011f),
1616
N("Nitrogen", 7, 14.007f),
1717
O("Oxygen", 8, 15.999f),
18-
F("Flourine", 9, 18.998f),
18+
// Fixed: "Flourine" -> "Fluorine"
19+
F("Fluorine", 9, 18.998f),
1920
NE("Neon", 10, 20.180f);
20-
/**
21-
* Maps cache labels and their associated Element3 instances.
21+
22+
/** * Maps cache labels and their associated Element3 instances.
2223
* Note that this only works if the values are all unique!
2324
*/
2425
private static final Map<String, Element4> BY_LABEL = new HashMap<>();
@@ -55,7 +56,7 @@ public String label() {
5556
}
5657

5758
/**
58-
* Look up Element2 instances by the label field. This implementation finds the
59+
* Look up Element4 instances by the label field. This implementation finds the
5960
* label in the BY_LABEL cache.
6061
* @param label The label to look up
6162
* @return The Element4 instance with the label, or null if not found.
@@ -65,7 +66,7 @@ public static Element4 valueOfLabel(String label) {
6566
}
6667

6768
/**
68-
* Look up Element2 instances by the atomicNumber field. This implementation finds the
69+
* Look up Element4 instances by the atomicNumber field. This implementation finds the
6970
* atomicNUmber in the cache.
7071
* @param number The atomicNumber to look up
7172
* @return The Element4 instance with the label, or null if not found.
@@ -75,7 +76,7 @@ public static Element4 valueOfAtomicNumber(int number) {
7576
}
7677

7778
/**
78-
* Look up Element2 instances by the atomicWeight field. This implementation finds the
79+
* Look up Element4 instances by the atomicWeight field. This implementation finds the
7980
* atomic weight in the cache.
8081
* @param weight the atomic weight to look up
8182
* @return The Element4 instance with the label, or null if not found.

core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/enums/values/Element2UnitTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
*/
61
package com.baeldung.enums.values;
72

83
import org.junit.After;
@@ -43,10 +38,22 @@ public void tearDown() {
4338
@Test
4439
public void whenLocatebyLabel_thenReturnCorrectValue() {
4540
for (Element2 e2 : Element2.values()) {
41+
// FIX: Skip UNKNOWN element, as it's not meant to be looked up by its own label in this test
42+
if (e2 == Element2.UNKNOWN) {
43+
continue;
44+
}
4645
assertSame(e2, Element2.valueOfLabel(e2.label));
4746
}
4847
}
4948

49+
@Test
50+
public void whenLocatebyUnknownLabel_thenReturnUNKNOWN() {
51+
// New test to ensure an unknown label returns the UNKNOWN constant
52+
assertSame(Element2.UNKNOWN, Element2.valueOfLabel("Unobtainium"));
53+
// Test for null label, which should also return UNKNOWN
54+
assertSame(Element2.UNKNOWN, Element2.valueOfLabel(null));
55+
}
56+
5057
/**
5158
* Test of toString method, of class Element2.
5259
*/

0 commit comments

Comments
 (0)