Skip to content

Commit 7d437b6

Browse files
Bael 5275 (eugenp#11561)
* Initial impl of enum * Added Java 8 based enum searcher and test for it. * Moved code to core-java-enums * Fixed test name and readme * Reused existing module core-java-modules/core-java-lang-oop-types-2 instead of creating a new one. * Reused existing module core-java-modules/core-java-lang-oop-types-2 instead of creating a new one.
1 parent 9d47189 commit 7d437b6

6 files changed

Lines changed: 336 additions & 0 deletions

File tree

core-java-modules/core-java-lang-oop-types-2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ This module contains articles about types in Java
55
### Relevant Articles:
66

77
- [Convert an Array of Primitives to an Array of Objects](https://www.baeldung.com/java-primitive-array-to-object-array)
8+
- [Check if an Enum Value Exists in Java](https://www.baeldung.com/java-find-enum-by-criteria)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.enums;
2+
3+
/**
4+
* Represents directions.
5+
*/
6+
public enum Direction {
7+
EAST, WEST, SOUTH, NORTH;
8+
9+
/**
10+
* Finds direction by name.
11+
*
12+
* @param name the name
13+
* @return the direction if found else null
14+
*/
15+
public static Direction findByName(String name) {
16+
Direction result = null;
17+
for (Direction direction : values()) {
18+
if (direction.name().equalsIgnoreCase(name)) {
19+
result = direction;
20+
break;
21+
}
22+
}
23+
return result;
24+
}
25+
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.enums;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class EnumSearcher {
7+
private static final Logger LOG = LoggerFactory.getLogger(EnumSearcher.class);
8+
9+
public static void main(String[] args) {
10+
EnumSearcher enumSearcher = new EnumSearcher();
11+
enumSearcher.printWeekdays();
12+
}
13+
14+
private void printWeekdays() {
15+
for (Weekday day: Weekday.values()) {
16+
LOG.info("Name {}, Value {}, Stringified {}", day.name(), day.getValue(), day);
17+
}
18+
}
19+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.baeldung.enums;
2+
3+
import java.util.Arrays;
4+
import java.util.Optional;
5+
6+
/**
7+
* Represents months in a year.
8+
*/
9+
public enum Month {
10+
/**
11+
* January.
12+
*/
13+
JANUARY("January", 1),
14+
/**
15+
* February.
16+
*/
17+
FEBRUARY("February", 2),
18+
/**
19+
* March.
20+
*/
21+
MARCH("March", 3),
22+
/**
23+
* April.
24+
*/
25+
APRIL("April", 4),
26+
/**
27+
* May.
28+
*/
29+
MAY("May", 5),
30+
/**
31+
* June.
32+
*/
33+
JUNE("June", 6),
34+
/**
35+
* July.
36+
*/
37+
JULY("July", 7),
38+
/**
39+
* August.
40+
*/
41+
AUGUST("August", 8),
42+
/**
43+
* September.
44+
*/
45+
SEPTEMBER("September", 9),
46+
/**
47+
* October.
48+
*/
49+
OCTOBER("October", 10),
50+
/**
51+
* November.
52+
*/
53+
NOVEMBER("November", 11),
54+
/**
55+
* December.
56+
*/
57+
DECEMBER("December", 12),
58+
;
59+
60+
private final String value;
61+
private final int code;
62+
63+
Month(String value, int code) {
64+
this.value = value;
65+
this.code = code;
66+
}
67+
68+
/**
69+
* Gets value.
70+
*
71+
* @return the value
72+
*/
73+
public String getValue() {
74+
return value;
75+
}
76+
77+
/**
78+
* Gets code.
79+
*
80+
* @return the code
81+
*/
82+
public int getCode() {
83+
return code;
84+
}
85+
86+
/**
87+
* Find by name.
88+
*
89+
* @param name the name
90+
* @return the month if found else <code>Optional.empty()</code>
91+
*/
92+
public static Optional<Month> findByName(String name) {
93+
return Arrays.stream(values()).filter(month -> month.name().equalsIgnoreCase(name)).findFirst();
94+
}
95+
96+
/**
97+
* Find by code.
98+
*
99+
* @param code the code
100+
* @return the month if found else <code>Optional.empty()</code>
101+
*/
102+
public static Optional<Month> findByCode(int code) {
103+
return Arrays.stream(values()).filter(month -> month.getCode() == code).findFirst();
104+
}
105+
106+
/**
107+
* Finds month by value.
108+
*
109+
* @param value value
110+
* @return month if value is valid
111+
* @throws IllegalArgumentException if month not found for given value
112+
*/
113+
public static Month findByValue(String value) {
114+
return Arrays.stream(values()).filter(month -> month.getValue().equalsIgnoreCase(value)).findFirst().orElseThrow(IllegalArgumentException::new);
115+
}
116+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.enums;
2+
3+
/**
4+
* Represents day in a week.
5+
*/
6+
public enum Weekday {
7+
/**
8+
* Monday.
9+
*/
10+
MONDAY("Monday"),
11+
/**
12+
* Tuesday.
13+
*/
14+
TUESDAY("Tuesday"),
15+
/**
16+
* Wednesday.
17+
*/
18+
WEDNESDAY("Wednesday"),
19+
/**
20+
* Thursday.
21+
*/
22+
THURSDAY("Thursday"),
23+
/**
24+
* Friday.
25+
*/
26+
FRIDAY("Friday"),
27+
/**
28+
* Saturday.
29+
*/
30+
SATURDAY("Saturday"),
31+
/**
32+
* Sunday.
33+
*/
34+
SUNDAY("Sunday"),
35+
;
36+
37+
private final String value;
38+
39+
Weekday(String value) {
40+
this.value = value;
41+
}
42+
43+
/**
44+
* Gets value.
45+
*
46+
* @return the value
47+
*/
48+
public String getValue() {
49+
return value;
50+
}
51+
52+
/**
53+
* Find by name.
54+
*
55+
* @param name the name
56+
* @return the weekday if found else null
57+
*/
58+
public static Weekday findByName(String name) {
59+
Weekday result = null;
60+
for (Weekday day : values()) {
61+
if (day.name().equalsIgnoreCase(name)) {
62+
result = day;
63+
break;
64+
}
65+
}
66+
return result;
67+
}
68+
69+
/**
70+
* Find by value.
71+
*
72+
* @param value the value
73+
* @return the weekday if found else null
74+
*/
75+
public static Weekday findByValue(String value) {
76+
Weekday result = null;
77+
for (Weekday day : values()) {
78+
if (day.getValue().equalsIgnoreCase(value)) {
79+
result = day;
80+
break;
81+
}
82+
}
83+
return result;
84+
}
85+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.enums;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
6+
import java.util.Optional;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
10+
11+
public class EnumSearcherUnitTest {
12+
13+
@Test
14+
public void givenWeekdays_whenValidDirectionNameProvided_directionIsFound() {
15+
Direction result = Direction.findByName("EAST");
16+
Assertions.assertThat(result).isEqualTo(Direction.EAST);
17+
}
18+
19+
@Test
20+
public void givenWeekdays_whenValidDirectionNameLowerCaseProvided_directionIsFound() {
21+
Direction result = Direction.findByName("east");
22+
Assertions.assertThat(result).isEqualTo(Direction.EAST);
23+
}
24+
25+
@Test
26+
public void givenWeekdays_whenInvalidDirectionNameProvided_nullIsReturned() {
27+
Direction result = Direction.findByName("E");
28+
Assertions.assertThat(result).isNull();
29+
}
30+
31+
@Test
32+
public void givenWeekdays_whenValidWeekdayNameProvided_weekdayIsFound() {
33+
Weekday result = Weekday.findByName("MONDAY");
34+
Assertions.assertThat(result).isEqualTo(Weekday.MONDAY);
35+
}
36+
37+
@Test
38+
public void givenWeekdays_whenInvalidWeekdayNameProvided_nullIsReturned() {
39+
Weekday result = Weekday.findByName("MON");
40+
Assertions.assertThat(result).isNull();
41+
}
42+
43+
@Test
44+
public void givenWeekdays_whenValidWeekdayValueProvided_weekdayIsFound() {
45+
Weekday result = Weekday.findByValue("Monday");
46+
Assertions.assertThat(result).isEqualTo(Weekday.MONDAY);
47+
}
48+
49+
@Test
50+
public void givenWeekdays_whenInvalidWeekdayValueProvided_nullIsReturned() {
51+
Weekday result = Weekday.findByValue("mon");
52+
Assertions.assertThat(result).isNull();
53+
}
54+
55+
@Test
56+
public void givenMonths_whenValidMonthNameProvided_optionalMonthIsReturned() {
57+
Optional<Month> result = Month.findByName("JANUARY");
58+
Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY));
59+
}
60+
61+
@Test
62+
public void givenMonths_whenInvalidMonthNameProvided_optionalEmptyIsReturned() {
63+
Optional<Month> result = Month.findByName("JAN");
64+
Assertions.assertThat(result).isEmpty();
65+
}
66+
67+
@Test
68+
public void givenMonths_whenValidMonthCodeProvided_optionalMonthIsReturned() {
69+
Optional<Month> result = Month.findByCode(1);
70+
Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY));
71+
}
72+
73+
@Test
74+
public void givenMonths_whenInvalidMonthCodeProvided_optionalEmptyIsReturned() {
75+
Optional<Month> result = Month.findByCode(0);
76+
Assertions.assertThat(result).isEmpty();
77+
}
78+
79+
@Test
80+
public void givenMonths_whenValidMonthValueProvided_monthIsReturned() {
81+
Month result = Month.findByValue("January");
82+
Assertions.assertThat(result).isEqualTo(Month.JANUARY);
83+
}
84+
85+
@Test
86+
public void givenMonths_whenInvalidMonthValueProvided_illegalArgExIsThrown() {
87+
assertThatIllegalArgumentException().isThrownBy(() -> Month.findByValue("Jan"));
88+
}
89+
}

0 commit comments

Comments
 (0)