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

io.geewit.web.convert.converter.EnumNameConverter Maven / Gradle / Ivy

The newest version!
package io.geewit.web.convert.converter;

import io.geewit.core.utils.enums.EnumUtils;
import io.geewit.core.utils.enums.Name;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;


/**
 * 自动转换类型
 * @param 
 * @author geewit
 */
public class EnumNameConverter & Name> implements Converter, ConditionalConverter {

    private TypeDescriptor targetType;

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        if(!sourceType.isAssignableTo(TypeDescriptor.valueOf(String.class))) {
            return false;
        }
        if(targetType.isAssignableTo(TypeDescriptor.valueOf(Enum.class)) && targetType.isAssignableTo(TypeDescriptor.valueOf(Name.class))) {
            this.targetType = targetType;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public T convert(String source) {
        return EnumUtils.forToken((Class) this.targetType.getObjectType(), source);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy