org.simpleflatmapper.reflect.ScoredSetter 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.
package org.simpleflatmapper.reflect;
import org.simpleflatmapper.reflect.setter.NullSetter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ScoredSetter {
private final int score;
private final Setter super T, ? super P> setter;
public ScoredSetter(int score, Setter super T, ? super P> setter) {
this.score = score;
this.setter = setter;
}
public Setter super T, ? super P> getSetter() {
return setter;
}
@Override
public String toString() {
return "ScoredSetter{" +
"score=" + score +
", setter=" + setter +
'}';
}
public boolean isBetterThan(ScoredSetter scoredSetter) {
return score > scoredSetter.score;
}
public ScoredSetter best(ScoredSetter setter) {
return isBetterThan(setter) ? this : setter;
}
public static ScoredSetter nullSetter() {
return new ScoredSetter(Integer.MIN_VALUE, NullSetter.NULL_SETTER);
}
public static ScoredSetter ofMethod(Method method, Setter methodSetter) {
int score = 2;
if (method.getName().startsWith("set")) {
score *= 2;
}
return of(methodSetter, score);
}
public static ScoredSetter ofField(Field field, Setter fieldSetter) {
return of(fieldSetter, 1);
}
public static ScoredSetter of(Setter methodSetter, int score) {
return new ScoredSetter(score, methodSetter);
}
}