com.github.atdixon.vroom.coercion.ToInteger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vroom Show documentation
Show all versions of vroom Show documentation
Work immutably, robustly and in a knowledge-oriented way with Entity maps in Java.
package com.github.atdixon.vroom.coercion;
import java.lang.reflect.Type;
public final class ToInteger implements Coercion {
@Override
public Integer coerce(Type type, Object value) throws FastCannotCoerceException {
if (value == null) {
return null;
} else if (value instanceof Integer) {
return (Integer) value;
} else if (value instanceof Number) {
return ((Number) value).intValue();
}
if (value instanceof String) {
try {
return Integer.valueOf((String) value);
} catch (NumberFormatException e) {/*ok*/}
}
throw new FastCannotCoerceException(type, value);
}
}