org.simpleflatmapper.converter.impl.NumberToEnumConverter Maven / Gradle / Ivy
package org.simpleflatmapper.converter.impl;
import org.simpleflatmapper.converter.Converter;
import org.simpleflatmapper.util.EnumHelper;
public class NumberToEnumConverter> implements Converter {
private final Class enumClass;
private final E[] values;
public NumberToEnumConverter(Class enumClass) {
this.enumClass = enumClass;
this.values = EnumHelper.getValues(enumClass);
}
@Override
public E convert(Number in) throws Exception {
if (in == null) return null;
int i = in.intValue();
if (i < 0 || i >= values.length) {
throw new IllegalArgumentException("Invalid ordinal value " + in + " for " + enumClass);
}
return values[i];
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy