org.onetwo.common.spring.converter.IntegerToEnumConverterFactory Maven / Gradle / Ivy
package org.onetwo.common.spring.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
/**
* @author weishao zeng
*
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class IntegerToEnumConverterFactory implements ConverterFactory {
@Override
public Converter getConverter(Class targetType) {
return new IntegerToEnum(targetType);
}
private class IntegerToEnum implements Converter {
private final Class enumType;
public IntegerToEnum(Class enumType) {
super();
this.enumType = enumType;
}
@Override
public T convert(Integer source) {
T[] values = (T[]) enumType.getEnumConstants();
int ordinal = source;
return ordinal>values.length?null:values[ordinal];
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy