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