All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alon.spring.crud.repository.specification.converter.EnumConverter Maven / Gradle / Ivy

Go to download

Projeto base para criação de serviços e recusos de CRUD com Spring Data JPA.

There is a newer version: 3.0.0
Show newest version
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 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;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy