com.kenshoo.pl.entity.converters.EnumAsStringValueConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.pl.entity.converters;
import com.kenshoo.pl.entity.ValueConverter;
/**
* Converts from/to Enum using its string representation (Enum.name())
*/
public class EnumAsStringValueConverter> implements ValueConverter {
private final Class cls;
public EnumAsStringValueConverter(Class cls) {
this.cls = cls;
}
public static > ValueConverter create(Class cls) {
return new EnumAsStringValueConverter<>(cls);
}
@Override
public String convertTo(E value) {
if (value == null) {
return null;
}
return value.name();
}
@Override
public E convertFrom(String value) {
if (value == null) {
return null;
}
try {
return Enum.valueOf(cls, value);
} catch (IllegalArgumentException e) {
return null;
}
}
@Override
public Class getValueClass() {
return cls;
}
}