org.simpleflatmapper.reflect.ScoredGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-reflect Show documentation
Show all versions of sfm-reflect Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
The newest version!
package org.simpleflatmapper.reflect;
import org.simpleflatmapper.reflect.getter.NullGetter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ScoredGetter {
@SuppressWarnings("unchecked")
public static final ScoredGetter NULL = new ScoredGetter(Integer.MIN_VALUE, NullGetter.getter());
private final int score;
private final Getter super T, ? extends P> getter;
public ScoredGetter(int score, Getter super T, ? extends P> getter) {
this.score = score;
this.getter = getter;
}
public Getter super T, ? extends P> getGetter() {
return getter;
}
@Override
public String toString() {
return "ScoredGetter{" +
"score=" + score +
", getter=" + getter +
'}';
}
@SuppressWarnings("unchecked")
public static ScoredGetter nullGetter() {
return NULL;
}
public boolean isBetterThan(ScoredGetter scoredGetter) {
return score > scoredGetter.score;
}
public ScoredGetter best(ScoredGetter getter) {
return isBetterThan(getter) ? this : getter;
}
public static ScoredGetter of(Getter getter, int score) {
return new ScoredGetter(score, getter);
}
public static ScoredGetter ofMethod(Method method, Getter methodGetter) {
int score = 2;
if (method.getName().startsWith("get") || method.getName().startsWith("is")) {
score *= 2;
}
return of(methodGetter, score);
}
public static ScoredGetter ofField(Field field, Getter fieldGetter) {
return of(fieldGetter, 1);
}
}