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

org.fiolino.common.beans.FieldMatcher Maven / Gradle / Ivy

Go to download

General structure to easily create dynamic logic via MethodHandles and others.

There is a newer version: 1.0.10
Show newest version
package org.fiolino.common.beans;

import java.lang.reflect.Field;

/**
 * Created by kuli on 20.02.16.
 */
public interface FieldMatcher {

    int rank(Field reader, Field writer);

    FieldMatcher SAME_NAME = (r, w) -> r.getName().equals(w.getName()) ? 100 : -1;

    FieldMatcher SAME_OWNER = (r, w) -> r.getDeclaringClass().equals(w.getDeclaringClass()) ? 10 : -1;

    static FieldMatcher combinationOf(FieldMatcher... matchers) {
        if (matchers.length == 0) {
            throw new IllegalArgumentException("Expected at least one matcher.");
        }
        if (matchers.length == 1) {
            return matchers[0];
        }
        return (r, w) -> {
            int rank = 0;
            for (FieldMatcher m : matchers) {
                int inner = m.rank(r, w);
                if (inner < 0) {
                    return inner;
                }
                rank += inner;
            }
            return rank;
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy