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

chaschev.lang.Predicates2 Maven / Gradle / Ivy

package chaschev.lang;

import chaschev.lang.reflect.MethodDesc;
import chaschev.util.Exceptions;
import com.google.common.base.Function;
import com.google.common.base.Predicate;

import javax.annotation.Nullable;
import java.lang.reflect.Field;

/**
 * @author Andrey Chaschev [email protected]
 */
public class Predicates2 {

    public static Predicate contains(final CharSequence cs){
        return new Predicate() {
            @Override
            public boolean apply(String input) {
                return input.contains(cs);
            }
        };
    }

    public static  Predicate functionAppliedEquals(final Function fun, final Object to){
        return new Predicate() {
            @Override
            public boolean apply(@Nullable T input) {
                Object r = fun.apply(input);

                return objectsEqual(r, to);
            }
        };
    }

    public static  Predicate functionAppliedNotEqual(final Function fun, final Object to){
        return new Predicate() {
            @Override
            public boolean apply(@Nullable T input) {
                Object r = fun.apply(input);

                return !objectsEqual(r, to);
            }
        };
    }

    public static  Predicate methodEquals(final String method, final Object to){
        return methodEquals(to, method);
    }

    public static  Predicate methodEquals(final Object to, final String method, final Object... params){
        final MethodDesc[] desc = new MethodDesc[1];

        return new Predicate() {
            @Override
            public boolean apply(@Nullable T input) {
                if(input == null) return false;

                if(desc[0] == null){
                    desc[0] = OpenBean.getClassDesc(input.getClass()).getMethodDesc(method, false, params);
                    if(desc[0] == null){
                        throw new IllegalArgumentException("no such method: " + method);
                    }
                }

                Object r = desc[0].invoke(input, params);

                return objectsEqual(r, to);
            }
        };
    }

    public static  Predicate fieldEquals(final String field, final Object to){
        final Field[] desc = new Field[1];

        return new Predicate() {
            @Override
            public boolean apply(@Nullable T input) {
                if(input == null) return false;

                if(desc[0] == null){
                    desc[0] = OpenBean.getClassDesc(input.getClass()).getField(field);
                    if(desc[0] == null){
                        throw new IllegalArgumentException("no such field: " + field);
                    }
                }

                Object r = null;
                try {
                    r = desc[0].get(input);
                } catch (IllegalAccessException e) {
                    throw Exceptions.runtime(e);
                }

                return objectsEqual(r, to);
            }
        };
    }

    private static boolean objectsEqual(Object a, Object b) {
        if(a == null && b == null) return true;
        if(a == null) return false;

        return a.equals(b);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy