[BAEL-4617] extending enums (#10130)
This commit is contained in:
parent
f84d19d6b0
commit
600896e842
|
@ -12,12 +12,20 @@
|
|||
<artifactId>core-java-lang-oop-types</artifactId>
|
||||
<name>core-java-lang-oop-types</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<commons-codec.version>1.15</commons-codec.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -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<ImmutableOperation, Operator> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ImmutableOperation, Operator> 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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.enums.extendenum;
|
||||
|
||||
public enum ImmutableOperation {
|
||||
REMOVE_WHITESPACES, TO_LOWER, INVERT_CASE
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.enums.extendenum;
|
||||
|
||||
public interface Operator {
|
||||
String apply(String input);
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.enums.extendenum;
|
||||
|
||||
public interface StringOperation {
|
||||
String getDescription();
|
||||
|
||||
String apply(String input);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue