This project allows to create a Java enum which inherits enum item value automatic conversion functionality.
Imagine you have an enum to hold your application JVM arguments. To get the actual JVM argument value as a URL object you would normally do:
String webServerKey = JVMArgument.WEB_SERVER.getKey();
String webServer = System.getProperty(webServerKey);
try {
URL webServerUrl = new URL(webServer);
} catch(MalformedUrlException e) {
// error handling
}
This is clearly too much work to get the job done!
The solution is to have your enum implementing CastableEnum
interface and define your custom value function. The value function defines how to fetch the value corresponding to an enum element. In the example the value is fetched from System properties.
public enum JVMArgument implements CastableEnum {
SOURCE_ID("sourceId"),
WEB_SERVER("webServer"),
FEATURES("features");
private final String key;
JVMArgument(String key) {
this.key = key;
}
@Override
public Function<String, String> getValueFunction() {
return key -> System.getProperty(key);
}
@Override
public String getKey() {
return key;
}
}
By implementing the CastableEnum
you will be able to access cast functionality
System.setProperty(JVMArgument.SOURCE_ID.getKey(), "123");
System.setProperty(JVMArgument.WEB_SERVER.getKey(), "http://webserver.com");
System.setProperty(JVMArgument.FEATURES.getKey(), "logDebug;disCache;sso");
int sourceId = JVMArgument.SOURCE_ID.asInt();
URL url = JVMArgument.WEB_SERVER.asURL();
List<String> list = JVMArgument.FEATURES.asList(";", String.class);