fitnesse.slim.converters.GenericEnumConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fitnesse Show documentation
Show all versions of fitnesse Show documentation
The fully integrated standalone wiki, and acceptance testing framework.
package fitnesse.slim.converters;
import fitnesse.slim.SlimError;
import fitnesse.util.StringUtils;
import fitnesse.slim.Converter;
public class GenericEnumConverter> implements Converter {
private final Class enumClass;
public GenericEnumConverter(Class enumClass) {
this.enumClass = enumClass;
}
@Override
public String toString(T o) {
return o != null ? o.name() : NULL_VALUE;
}
@Override
public T fromString(String name) {
if (StringUtils.isBlank(name))
return null;
try {
return Enum.valueOf(enumClass, name);
} catch (IllegalArgumentException e) {
for (T value : enumClass.getEnumConstants()) {
if (value.name().equalsIgnoreCase(name)) {
return value;
}
}
throw new SlimError(String.format("message:<>", name, enumClass.getName()), e);
}
}
}