* 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.
This commit is contained in:
Sachin Raverkar 2021-12-09 09:22:20 +05:30 committed by GitHub
parent 0524b8b313
commit 1b2115b2ce
6 changed files with 336 additions and 0 deletions

View File

@ -5,3 +5,4 @@ This module contains articles about types in Java
### Relevant Articles:
- [Convert an Array of Primitives to an Array of Objects](https://www.baeldung.com/java-primitive-array-to-object-array)
- [Check if an Enum Value Exists in Java](https://www.baeldung.com/java-find-enum-by-criteria)

View File

@ -0,0 +1,26 @@
package com.baeldung.enums;
/**
* Represents directions.
*/
public enum Direction {
EAST, WEST, SOUTH, NORTH;
/**
* Finds direction by name.
*
* @param name the name
* @return the direction if found else null
*/
public static Direction findByName(String name) {
Direction result = null;
for (Direction direction : values()) {
if (direction.name().equalsIgnoreCase(name)) {
result = direction;
break;
}
}
return result;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.enums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EnumSearcher {
private static final Logger LOG = LoggerFactory.getLogger(EnumSearcher.class);
public static void main(String[] args) {
EnumSearcher enumSearcher = new EnumSearcher();
enumSearcher.printWeekdays();
}
private void printWeekdays() {
for (Weekday day: Weekday.values()) {
LOG.info("Name {}, Value {}, Stringified {}", day.name(), day.getValue(), day);
}
}
}

View File

@ -0,0 +1,116 @@
package com.baeldung.enums;
import java.util.Arrays;
import java.util.Optional;
/**
* Represents months in a year.
*/
public enum Month {
/**
* January.
*/
JANUARY("January", 1),
/**
* February.
*/
FEBRUARY("February", 2),
/**
* March.
*/
MARCH("March", 3),
/**
* April.
*/
APRIL("April", 4),
/**
* May.
*/
MAY("May", 5),
/**
* June.
*/
JUNE("June", 6),
/**
* July.
*/
JULY("July", 7),
/**
* August.
*/
AUGUST("August", 8),
/**
* September.
*/
SEPTEMBER("September", 9),
/**
* October.
*/
OCTOBER("October", 10),
/**
* November.
*/
NOVEMBER("November", 11),
/**
* December.
*/
DECEMBER("December", 12),
;
private final String value;
private final int code;
Month(String value, int code) {
this.value = value;
this.code = code;
}
/**
* Gets value.
*
* @return the value
*/
public String getValue() {
return value;
}
/**
* Gets code.
*
* @return the code
*/
public int getCode() {
return code;
}
/**
* Find by name.
*
* @param name the name
* @return the month if found else <code>Optional.empty()</code>
*/
public static Optional<Month> findByName(String name) {
return Arrays.stream(values()).filter(month -> month.name().equalsIgnoreCase(name)).findFirst();
}
/**
* Find by code.
*
* @param code the code
* @return the month if found else <code>Optional.empty()</code>
*/
public static Optional<Month> findByCode(int code) {
return Arrays.stream(values()).filter(month -> month.getCode() == code).findFirst();
}
/**
* Finds month by value.
*
* @param value value
* @return month if value is valid
* @throws IllegalArgumentException if month not found for given value
*/
public static Month findByValue(String value) {
return Arrays.stream(values()).filter(month -> month.getValue().equalsIgnoreCase(value)).findFirst().orElseThrow(IllegalArgumentException::new);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.enums;
/**
* Represents day in a week.
*/
public enum Weekday {
/**
* Monday.
*/
MONDAY("Monday"),
/**
* Tuesday.
*/
TUESDAY("Tuesday"),
/**
* Wednesday.
*/
WEDNESDAY("Wednesday"),
/**
* Thursday.
*/
THURSDAY("Thursday"),
/**
* Friday.
*/
FRIDAY("Friday"),
/**
* Saturday.
*/
SATURDAY("Saturday"),
/**
* Sunday.
*/
SUNDAY("Sunday"),
;
private final String value;
Weekday(String value) {
this.value = value;
}
/**
* Gets value.
*
* @return the value
*/
public String getValue() {
return value;
}
/**
* Find by name.
*
* @param name the name
* @return the weekday if found else null
*/
public static Weekday findByName(String name) {
Weekday result = null;
for (Weekday day : values()) {
if (day.name().equalsIgnoreCase(name)) {
result = day;
break;
}
}
return result;
}
/**
* Find by value.
*
* @param value the value
* @return the weekday if found else null
*/
public static Weekday findByValue(String value) {
Weekday result = null;
for (Weekday day : values()) {
if (day.getValue().equalsIgnoreCase(value)) {
result = day;
break;
}
}
return result;
}
}

View File

@ -0,0 +1,89 @@
package com.baeldung.enums;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class EnumSearcherUnitTest {
@Test
public void givenWeekdays_whenValidDirectionNameProvided_directionIsFound() {
Direction result = Direction.findByName("EAST");
Assertions.assertThat(result).isEqualTo(Direction.EAST);
}
@Test
public void givenWeekdays_whenValidDirectionNameLowerCaseProvided_directionIsFound() {
Direction result = Direction.findByName("east");
Assertions.assertThat(result).isEqualTo(Direction.EAST);
}
@Test
public void givenWeekdays_whenInvalidDirectionNameProvided_nullIsReturned() {
Direction result = Direction.findByName("E");
Assertions.assertThat(result).isNull();
}
@Test
public void givenWeekdays_whenValidWeekdayNameProvided_weekdayIsFound() {
Weekday result = Weekday.findByName("MONDAY");
Assertions.assertThat(result).isEqualTo(Weekday.MONDAY);
}
@Test
public void givenWeekdays_whenInvalidWeekdayNameProvided_nullIsReturned() {
Weekday result = Weekday.findByName("MON");
Assertions.assertThat(result).isNull();
}
@Test
public void givenWeekdays_whenValidWeekdayValueProvided_weekdayIsFound() {
Weekday result = Weekday.findByValue("Monday");
Assertions.assertThat(result).isEqualTo(Weekday.MONDAY);
}
@Test
public void givenWeekdays_whenInvalidWeekdayValueProvided_nullIsReturned() {
Weekday result = Weekday.findByValue("mon");
Assertions.assertThat(result).isNull();
}
@Test
public void givenMonths_whenValidMonthNameProvided_optionalMonthIsReturned() {
Optional<Month> result = Month.findByName("JANUARY");
Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY));
}
@Test
public void givenMonths_whenInvalidMonthNameProvided_optionalEmptyIsReturned() {
Optional<Month> result = Month.findByName("JAN");
Assertions.assertThat(result).isEmpty();
}
@Test
public void givenMonths_whenValidMonthCodeProvided_optionalMonthIsReturned() {
Optional<Month> result = Month.findByCode(1);
Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY));
}
@Test
public void givenMonths_whenInvalidMonthCodeProvided_optionalEmptyIsReturned() {
Optional<Month> result = Month.findByCode(0);
Assertions.assertThat(result).isEmpty();
}
@Test
public void givenMonths_whenValidMonthValueProvided_monthIsReturned() {
Month result = Month.findByValue("January");
Assertions.assertThat(result).isEqualTo(Month.JANUARY);
}
@Test
public void givenMonths_whenInvalidMonthValueProvided_illegalArgExIsThrown() {
assertThatIllegalArgumentException().isThrownBy(() -> Month.findByValue("Jan"));
}
}