nl.vpro.rs.converters.CaseInsensitiveEnumParamConverter Maven / Gradle / Ivy
package nl.vpro.rs.converters;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import jakarta.ws.rs.ext.ParamConverter;
/**
* @author Michiel Meeuwissen
* @since 1.75
*/
@Slf4j
public class CaseInsensitiveEnumParamConverter> implements ParamConverter {
public static > CaseInsensitiveEnumParamConverter getInstant(Class enumClass) {
return new CaseInsensitiveEnumParamConverter<>(enumClass);
}
private final Class enumClass;
public CaseInsensitiveEnumParamConverter(Class enumClass) {
this.enumClass = enumClass;
}
@SuppressWarnings("unchecked")
@Override
public T fromString(String value) {
if (value == null || value.isEmpty()) {
return null;
}
try {
return Enum.valueOf(enumClass, value);
} catch (IllegalArgumentException e) {
try {
return Enum.valueOf(enumClass, value.toUpperCase());
} catch (IllegalArgumentException e2) {
for (Field f : enumClass.getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
try {
T v = (T) f.get(null);
if (v.name().equalsIgnoreCase(value)) {
return v;
}
} catch (IllegalAccessException e1) {
log.warn(e1.getMessage(), e1);
}
}
}
}
throw e;
}
}
@Override
public String toString(T value) {
if (value == null) {
return null;
}
return value.name();
}
}