All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.reflect.ScoredSetter Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 9.0.2
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy