diff --git a/core-java-modules/core-java-lang-oop-types/pom.xml b/core-java-modules/core-java-lang-oop-types/pom.xml index ee167bbae2..c4efd9b8d2 100644 --- a/core-java-modules/core-java-lang-oop-types/pom.xml +++ b/core-java-modules/core-java-lang-oop-types/pom.xml @@ -12,12 +12,20 @@ core-java-lang-oop-types core-java-lang-oop-types jar - + org.apache.commons commons-lang3 ${commons-lang3.version} + + commons-codec + commons-codec + ${commons-codec.version} + + + 1.15 + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Application.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Application.java new file mode 100644 index 0000000000..ad3d861d6a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Application.java @@ -0,0 +1,35 @@ +package com.baeldung.enums.extendenum; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.EnumMap; +import java.util.Map; + +public class Application { + private static final Map OPERATION_MAP; + + static { + OPERATION_MAP = new EnumMap<>(ImmutableOperation.class); + OPERATION_MAP.put(ImmutableOperation.TO_LOWER, String::toLowerCase); + OPERATION_MAP.put(ImmutableOperation.INVERT_CASE, StringUtils::swapCase); + OPERATION_MAP.put(ImmutableOperation.REMOVE_WHITESPACES, input -> input.replaceAll("\\s", "")); + + if (Arrays.stream(ImmutableOperation.values()).anyMatch(it -> !OPERATION_MAP.containsKey(it))) { + throw new IllegalStateException("Unmapped enum constant found!"); + } + } + + public String applyImmutableOperation(ImmutableOperation operation, String input) { + return OPERATION_MAP.get(operation).apply(input); + } + + public String getDescription(StringOperation stringOperation) { + return stringOperation.getDescription(); + } + + public String applyOperation(StringOperation operation, String input) { + return operation.apply(input); + } + +} diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ApplicationWithEx.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ApplicationWithEx.java new file mode 100644 index 0000000000..e9cbad6b7c --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ApplicationWithEx.java @@ -0,0 +1,26 @@ +package com.baeldung.enums.extendenum; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.EnumMap; +import java.util.Map; + +public class ApplicationWithEx { + private static final Map OPERATION_MAP; + + static { + OPERATION_MAP = new EnumMap<>(ImmutableOperation.class); + OPERATION_MAP.put(ImmutableOperation.TO_LOWER, String::toLowerCase); + OPERATION_MAP.put(ImmutableOperation.INVERT_CASE, StringUtils::swapCase); + // ImmutableOperation.REMOVE_WHITESPACES is not mapped + + if (Arrays.stream(ImmutableOperation.values()).anyMatch(it -> !OPERATION_MAP.containsKey(it))) { + throw new IllegalStateException("Unmapped enum constant found!"); + } + } + + public String applyImmutableOperation(ImmutableOperation operation, String input) { + return OPERATION_MAP.get(operation).apply(input); + } +} diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/BasicStringOperation.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/BasicStringOperation.java new file mode 100644 index 0000000000..267b02daf4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/BasicStringOperation.java @@ -0,0 +1,33 @@ +package com.baeldung.enums.extendenum; + +public enum BasicStringOperation implements StringOperation { + TRIM("Removing leading and trailing spaces.") { + @Override + public String apply(String input) { + return input.trim(); + } + }, + TO_UPPER("Changing all characters into upper case.") { + @Override + public String apply(String input) { + return input.toUpperCase(); + } + }, + REVERSE("Reversing the given string.") { + @Override + public String apply(String input) { + return new StringBuilder(input).reverse().toString(); + } + }; + + private String description; + + public String getDescription() { + return description; + } + + BasicStringOperation(String description) { + this.description = description; + } +} + diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ExtendedStringOperation.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ExtendedStringOperation.java new file mode 100644 index 0000000000..6184837b26 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ExtendedStringOperation.java @@ -0,0 +1,31 @@ +package com.baeldung.enums.extendenum; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; + +public enum ExtendedStringOperation implements StringOperation { + MD5_ENCODE("Encoding the given string using the MD5 algorithm.") { + @Override + public String apply(String input) { + return DigestUtils.md5Hex(input); + } + }, + BASE64_ENCODE("Encoding the given string using the BASE64 algorithm.") { + @Override + public String apply(String input) { + return new String(new Base64().encode(input.getBytes())); + } + }; + + private String description; + + ExtendedStringOperation(String description) { + this.description = description; + } + + + @Override + public String getDescription() { + return description; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ImmutableOperation.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ImmutableOperation.java new file mode 100644 index 0000000000..66f26a4806 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/ImmutableOperation.java @@ -0,0 +1,6 @@ +package com.baeldung.enums.extendenum; + +public enum ImmutableOperation { + REMOVE_WHITESPACES, TO_LOWER, INVERT_CASE +} + diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Operator.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Operator.java new file mode 100644 index 0000000000..a65fea4f92 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/Operator.java @@ -0,0 +1,5 @@ +package com.baeldung.enums.extendenum; + +public interface Operator { + String apply(String input); +} diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/StringOperation.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/StringOperation.java new file mode 100644 index 0000000000..faf4f38274 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/enums/extendenum/StringOperation.java @@ -0,0 +1,7 @@ +package com.baeldung.enums.extendenum; + +public interface StringOperation { + String getDescription(); + + String apply(String input); +} diff --git a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/enums/extendenum/ExtendEnumUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/enums/extendenum/ExtendEnumUnitTest.java new file mode 100644 index 0000000000..0b5ed1e826 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/enums/extendenum/ExtendEnumUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.enums.extendenum; + +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ExtendEnumUnitTest { + private Application app = new Application(); + + @Test + public void givenAStringAndOperation_whenApplyOperation_thenGetExpectedResult() { + String input = " hello"; + String expectedToUpper = " HELLO"; + String expectedReverse = "olleh "; + String expectedTrim = "hello"; + String expectedBase64 = "IGhlbGxv"; + String expectedMd5 = "292a5af68d31c10e31ad449bd8f51263"; + assertEquals(expectedTrim, app.applyOperation(BasicStringOperation.TRIM, input)); + assertEquals(expectedToUpper, app.applyOperation(BasicStringOperation.TO_UPPER, input)); + assertEquals(expectedReverse, app.applyOperation(BasicStringOperation.REVERSE, input)); + assertEquals(expectedBase64, app.applyOperation(ExtendedStringOperation.BASE64_ENCODE, input)); + assertEquals(expectedMd5, app.applyOperation(ExtendedStringOperation.MD5_ENCODE, input)); + } + + @Test + public void givenAStringAndImmutableOperation_whenApplyOperation_thenGetExpectedResult() { + String input = " He ll O "; + String expectedToLower = " he ll o "; + String expectedRmWhitespace = "HellO"; + String expectedInvertCase = " hE LL o "; + assertEquals(expectedToLower, app.applyImmutableOperation(ImmutableOperation.TO_LOWER, input)); + assertEquals(expectedRmWhitespace, app.applyImmutableOperation(ImmutableOperation.REMOVE_WHITESPACES, input)); + assertEquals(expectedInvertCase, app.applyImmutableOperation(ImmutableOperation.INVERT_CASE, input)); + } + + @Test + public void givenUnmappedImmutableOperationValue_whenAppStarts_thenGetException() { + Throwable throwable = assertThrows(ExceptionInInitializerError.class, () -> { + ApplicationWithEx appEx = new ApplicationWithEx(); + }); + assertTrue(throwable.getCause() instanceof IllegalStateException); + } +} \ No newline at end of file