org.simpleflatmapper.reflect.ScoredSetter Maven / Gradle / Ivy
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 setter;
public ScoredSetter(int score, Setter setter) {
this.score = score;
this.setter = setter;
}
public Setter 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);
}
}