public enum BasicStringOperation {
TRIM("Removing leading and trailing spaces."),
TO_UPPER("Changing all characters into upper case."),
REVERSE("Reversing the given string.");
private String description;
// constructor and getter
}
public enum ExtendedStringOperation extends BasicStringOperation {
MD5_ENCODE("Encoding the given string using the MD5 algorithm."),
BASE64_ENCODE("Encoding the given string using the BASE64 algorithm.");
private String description;
// constructor and getter
}
原因:
Cannot inherit from enum ‘BasicStringOperation’ → Inheritance Is Not Allowed for Enums
原因:
$ javap BasicStringOperation
public final class com.baeldung.enums.extendenum.BasicStringOperation
extends java.lang.Enum<com.baeldung.enums.extendenum.BasicStringOperation> {
public static final com.baeldung.enums.extendenum.BasicStringOperation TRIM;
public static final com.baeldung.enums.extendenum.BasicStringOperation TO_UPPER;
public static final com.baeldung.enums.extendenum.BasicStringOperation REVERSE;
...
}
public interface StringOperation {
String getDescription();
}
public enum BasicStringOperation implements StringOperation {
TRIM("Removing leading and trailing spaces."),
TO_UPPER("Changing all characters into upper case."),
REVERSE("Reversing the given string.");
BasicStringOperation(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
public enum ExtendedStringOperation implements StringOperation {
MD5_ENCODE("Encoding the given string using the MD5 algorithm."),
BASE64_ENCODE("Encoding the given string using the BASE64 algorithm.");
ExtendedStringOperation(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
public class Application {
// 具体类型
public String getOperationDescription(BasicStringOperation stringOperation) {
return stringOperation.getDescription();
}
// 面相接口编程
public String getOperationDescription1(StringOperation stringOperation) {
return stringOperation.getDescription();
}
}
public interface StringOperation {
String getDescription();
String apply(String input);
}
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();
}
};
BasicStringOperation(String description) {
this.description = description;
}
@Override
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
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(Base64.getEncoder().encodeToString(input.getBytes()));
}
};
ExtendedStringOperation(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
public class Application {
public String applyOperation(StringOperation operation, String input) {
return operation.apply(input);
}
}
String input = " hello";
Application app = new Application();
System.out.println(app.applyOperation(BasicStringOperation.TRIM, input)); // "hello"
System.out.println(app.applyOperation(BasicStringOperation.TO_UPPER, input));// " HELLO"
System.out.println(app.applyOperation(BasicStringOperation.REVERSE, input));// "olleh"
System.out.println(app.applyOperation(ExtendedStringOperation.BASE64_ENCODE, input)); // "IGhlbGxv"
System.out.println(app.applyOperation(ExtendedStringOperation.MD5_ENCODE, input)); // "292a5af68d31c10e31ad449bd8f51263"
public class Application {
public enum ImmutableOperation {
REMOVE_WHITESPACES, TO_LOWER, INVERT_CASE
}
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", ""));
}
public String applyImmutableOperation(ImmutableOperation operation, String input) {
return OPERATION_MAP.get(operation).apply(input);
}
}
String input = " He ll O ";
Application app = new Application();
System.out.println(app.applyImmutableOperation(ImmutableOperation.TO_LOWER, input)); // " he ll o "
System.out.println(app.applyImmutableOperation(ImmutableOperation.INVERT_CASE, input));// " hE LL o "
System.out.println(app.applyImmutableOperation(ImmutableOperation.REMOVE_WHITESPACES, input));// "HellO"
public enum ImmutableOperation {
REMOVE_WHITESPACES, TO_LOWER, INVERT_CASE
}
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!");
}
}
Application app = new Application();
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalStateException: Unmapped enum constant found!
at javabasic.enumprac.extendenums.Application.<clinit>(Application.java:23)