cdc.util.enums.StringToDynamicEnum Maven / Gradle / Ivy
package cdc.util.enums;
import cdc.util.args.AbstractFactory;
import cdc.util.args.Args;
import cdc.util.args.Factory;
import cdc.util.args.FormalArg;
import cdc.util.args.FormalArgs;
import cdc.util.args.Necessity;
import cdc.util.converters.AbstractConverter;
import cdc.util.lang.FailureReaction;
import cdc.util.lang.Introspection;
/**
* Converter from String to DynamicEnum.
*
* @author Damien Carbonne
*
* @param The dynamic enum type.
*/
public class StringToDynamicEnum extends AbstractConverter {
private final EnumType enumType;
public static final FormalArg> ENUM_TYPE =
new FormalArg<>("enumType", Introspection.uncheckedCast(EnumType.class), Necessity.MANDATORY);
public static final FormalArgs FARGS = new FormalArgs(ENUM_TYPE);
public static final Factory> FACTORY =
new AbstractFactory>(Introspection.uncheckedCast(StringToDynamicEnum.class),
Args.builder()
.setArg(SOURCE_CLASS, String.class)
.setArg(TARGET_CLASS, DynamicEnum.class)
.build(),
FARGS) {
@Override
protected StringToDynamicEnum> create(Args args,
FormalArgs fargs) {
final EnumType> enumType = args.getValue(ENUM_TYPE);
return StringToDynamicEnum.create(Introspection.uncheckedCast(enumType));
}
};
protected StringToDynamicEnum(EnumType enumType) {
super(String.class,
enumType.getValueClass());
this.enumType = enumType;
}
public static StringToDynamicEnum create(EnumType enumType) {
return new StringToDynamicEnum<>(enumType);
}
public EnumType getEnumType() {
return enumType;
}
@Override
public V apply(String source) {
return source == null ? null : enumType.valueOf(source, FailureReaction.FAIL);
}
@Override
public Args getParams() {
return Args.builder()
.setArg(ENUM_TYPE, enumType)
.build();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy