org.mongodb.morphia.converters.IntegerConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of morphia Show documentation
Show all versions of morphia Show documentation
Java Object Document Mapper for MongoDB
The newest version!
package org.mongodb.morphia.converters;
import org.mongodb.morphia.mapping.MappedField;
import org.mongodb.morphia.utils.ReflectionUtils;
import java.util.List;
/**
* @author Uwe Schaefer, ([email protected])
* @author scotthernandez
*/
public class IntegerConverter extends TypeConverter implements SimpleValueConverter {
/**
* Creates the Converter.
*/
public IntegerConverter() {
super(int.class, Integer.class, int[].class, Integer[].class);
}
@Override
public Object decode(final Class targetClass, final Object val, final MappedField optionalExtraInfo) {
if (val == null) {
return null;
}
if (val instanceof Integer) {
return val;
}
if (val instanceof Number) {
return ((Number) val).intValue();
}
if (val instanceof List) {
final Class type = targetClass.isArray() ? targetClass.getComponentType() : targetClass;
return ReflectionUtils.convertToArray(type, (List) val);
}
return Integer.parseInt(val.toString());
}
}