org.sfm.reflect.impl.ArgumentBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.reflect.impl;
import org.sfm.reflect.ConstructorDefinition;
import org.sfm.reflect.ConstructorParameter;
import org.sfm.reflect.Getter;
import org.sfm.reflect.TypeHelper;
import java.util.HashMap;
import java.util.Map;
public final class ArgumentBuilder {
@SuppressWarnings("rawtypes")
private static final Getter NULL_GETTER = new Getter() {
@Override
public Object get(Object target) throws Exception {
return null;
}
};
@SuppressWarnings({ "rawtypes" })
private static final Map, Getter> DEFAULT_GETTERS = new HashMap, Getter>();
static {
DEFAULT_GETTERS.put(boolean.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return Boolean.TRUE;
}
});
DEFAULT_GETTERS.put(byte.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return (byte) 0;
}
});
DEFAULT_GETTERS.put(char.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return (char) 0;
}
});
DEFAULT_GETTERS.put(short.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return (short) 0;
}
});
DEFAULT_GETTERS.put(int.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return 0;
}
});
DEFAULT_GETTERS.put(long.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return (long) 0;
}
});
DEFAULT_GETTERS.put(float.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return 0.0f;
}
});
DEFAULT_GETTERS.put(double.class, new Getter() {
@Override
public Object get(Object target) throws Exception {
return 0.0d;
}
});
}
private final Getter[] getters;
@SuppressWarnings("unchecked")
public ArgumentBuilder(ConstructorDefinition constructorDefinition,
Map> injections) {
ConstructorParameter[] parameters = constructorDefinition.getParameters();
getters = new Getter[parameters.length];
for (int i = 0; i < getters.length; i++) {
ConstructorParameter param = parameters[i];
Getter getter = injections.get(param);
if (getter == null) {
if (TypeHelper.isPrimitive(param.getType())) {
getter = DEFAULT_GETTERS.get(param.getType());
} else {
getter = NULL_GETTER;
}
}
getters[i] = getter;
}
}
public Object[] build(S source) throws Exception {
Object[] args = new Object[getters.length];
for(int i = 0; i < args.length; i++) {
args[i] = getters[i].get(source);
}
return args;
}
}