com.alon.spring.crud.repository.specification.converter.EnumConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-crud-base Show documentation
Show all versions of spring-crud-base Show documentation
Projeto base para criação de serviços e recusos de CRUD com Spring Data JPA.
package com.alon.spring.crud.repository.specification.converter;
public class EnumConverter implements DecoderConverter {
private static EnumConverter INSTANCE;
private ThreadLocal> ENUM_TYPE_CONTEXT = new ThreadLocal<>();
public EnumConverter setEnumType(Class extends Enum> enumType) {
ENUM_TYPE_CONTEXT.set(enumType);
return this;
}
@Override
public Enum convert(String value) {
if (ENUM_TYPE_CONTEXT.get() == null)
return null;
Enum convertedValue;
try {
convertedValue = Enum.valueOf(ENUM_TYPE_CONTEXT.get(), value);
} catch (IllegalArgumentException ex) {
convertedValue = null;
} finally {
ENUM_TYPE_CONTEXT.remove();
}
return convertedValue;
}
public static EnumConverter getInstance() {
if (INSTANCE == null)
INSTANCE = new EnumConverter();
return INSTANCE;
}
}