Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.baeldung.enums.values;

import java.util.Objects;

/**
* The simple enum has been enhanced to add the name of the element.
*/
Expand All @@ -12,8 +14,11 @@ public enum Element2 {
C("Carbon"),
N("Nitrogen"),
O("Oxygen"),
F("Flourine"),
NE("Neon");
// Fixed: "Flourine" -> "Fluorine"
F("Fluorine"),
NE("Neon"),
// Added: Dedicated UNKNOWN member
UNKNOWN("Unknown Element");

/** a final variable to store the label, which can't be changed */
public final String label;
Expand All @@ -30,15 +35,17 @@ private Element2(String 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.
* @return The Element2 instance with the label, or UNKNOWN if not found.
*/
public static Element2 valueOfLabel(String label) {
for (Element2 e2 : values()) {
if (e2.label.equals(label)) {
// Fixed: Used Objects.equals for null-safe comparison
if (Objects.equals(e2.label, label)) {
return e2;
}
}
return null;
// Fixed: Return UNKNOWN instead of null
return UNKNOWN;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public enum Element3 {
C("Carbon"),
N("Nitrogen"),
O("Oxygen"),
F("Flourine"),
// Fixed: "Flourine" -> "Fluorine"
F("Fluorine"),
NE("Neon");

/**
* A map to cache labels and their associated Element3 instances.
/** * 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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public enum Element4 implements Labeled {
C("Carbon", 6, 12.011f),
N("Nitrogen", 7, 14.007f),
O("Oxygen", 8, 15.999f),
F("Flourine", 9, 18.998f),
// Fixed: "Flourine" -> "Fluorine"
F("Fluorine", 9, 18.998f),
NE("Neon", 10, 20.180f);
/**
* Maps cache labels and their associated Element3 instances.

/** * 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<>();
Expand Down Expand Up @@ -55,7 +56,7 @@ public String label() {
}

/**
* Look up Element2 instances by the label field. This implementation finds the
* Look up Element4 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.
Expand All @@ -65,7 +66,7 @@ public static Element4 valueOfLabel(String label) {
}

/**
* Look up Element2 instances by the atomicNumber field. This implementation finds the
* Look up Element4 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.
Expand All @@ -75,7 +76,7 @@ public static Element4 valueOfAtomicNumber(int number) {
}

/**
* Look up Element2 instances by the atomicWeight field. This implementation finds the
* Look up Element4 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* 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;
Expand Down Expand Up @@ -43,10 +38,22 @@ public void tearDown() {
@Test
public void whenLocatebyLabel_thenReturnCorrectValue() {
for (Element2 e2 : Element2.values()) {
// FIX: Skip UNKNOWN element, as it's not meant to be looked up by its own label in this test
if (e2 == Element2.UNKNOWN) {
continue;
}
assertSame(e2, Element2.valueOfLabel(e2.label));
}
}

@Test
public void whenLocatebyUnknownLabel_thenReturnUNKNOWN() {
// New test to ensure an unknown label returns the UNKNOWN constant
assertSame(Element2.UNKNOWN, Element2.valueOfLabel("Unobtainium"));
// Test for null label, which should also return UNKNOWN
assertSame(Element2.UNKNOWN, Element2.valueOfLabel(null));
}

/**
* Test of toString method, of class Element2.
*/
Expand Down