diff --git a/core-java-modules/core-java-lang-oop-types-2/README.md b/core-java-modules/core-java-lang-oop-types-2/README.md index a4e7b101c3..e1ce057035 100644 --- a/core-java-modules/core-java-lang-oop-types-2/README.md +++ b/core-java-modules/core-java-lang-oop-types-2/README.md @@ -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) diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java new file mode 100644 index 0000000000..935aca4d65 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java @@ -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; + } + +} diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/EnumSearcher.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/EnumSearcher.java new file mode 100644 index 0000000000..fa85cff38b --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/EnumSearcher.java @@ -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); + } + } +} diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Month.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Month.java new file mode 100644 index 0000000000..f674a4be0a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Month.java @@ -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 Optional.empty() + */ + public static Optional 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 Optional.empty() + */ + public static Optional 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); + } +} diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Weekday.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Weekday.java new file mode 100644 index 0000000000..3c1031cde7 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Weekday.java @@ -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; + } +} diff --git a/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/EnumSearcherUnitTest.java b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/EnumSearcherUnitTest.java new file mode 100644 index 0000000000..2c96837809 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/EnumSearcherUnitTest.java @@ -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 result = Month.findByName("JANUARY"); + Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY)); + } + + @Test + public void givenMonths_whenInvalidMonthNameProvided_optionalEmptyIsReturned() { + Optional result = Month.findByName("JAN"); + Assertions.assertThat(result).isEmpty(); + } + + @Test + public void givenMonths_whenValidMonthCodeProvided_optionalMonthIsReturned() { + Optional result = Month.findByCode(1); + Assertions.assertThat(result).isEqualTo(Optional.of(Month.JANUARY)); + } + + @Test + public void givenMonths_whenInvalidMonthCodeProvided_optionalEmptyIsReturned() { + Optional 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")); + } +}