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

io.ultreia.java4all.bean.JavaBeanPredicateBuilder Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package io.ultreia.java4all.bean;

/*-
 * #%L
 * Java Beans extends by Ultreia.io
 * %%
 * Copyright (C) 2018 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * Created by tchemit on 11/01/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public interface JavaBeanPredicateBuilder {

    void addPredicate(Predicate predicate);

    Predicate build();

    default Stream filter(Collection elements) {
        return elements.stream().filter(build());
    }

    default boolean anyMatch(Collection elements) {
        return elements.stream().anyMatch(build());
    }

    default boolean allMatch(Collection elements) {
        return elements.stream().allMatch(build());
    }

    default boolean noneMatch(Collection elements) {
        return elements.stream().noneMatch(build());
    }

    interface Query> {

        P parent();

        V getter(O element);

        default P isEquals(V propertyValue) {
            return addPredicate(new EqualsPredicate<>(propertyValue));
        }

        default P isNotEquals(V propertyValue) {
            return addPredicate(new EqualsPredicate<>(propertyValue).negate());
        }

        default P addPredicate(Predicate predicate) {
            P parent = parent();
            parent.addPredicate(d -> {
                V v = getter(d);
                return v != null && predicate.test(v);
            });
            return parent;
        }

    }

    final class EqualsPredicate implements Predicate {
        private final O value;

        public EqualsPredicate(O value) {
            this.value = Objects.requireNonNull(value);
        }

        @Override
        public boolean test(O o) {
            return Objects.equals(o, value);
        }
    }


    final class InPredicate implements Predicate {
        private final Collection value;

        public InPredicate(Collection value) {
            this.value = Objects.requireNonNull(value);
        }

        @Override
        public boolean test(O o) {
            return value.contains(o);
        }
    }

    final class BeforePredicate> implements Predicate {
        private final O value;
        private final boolean orEquals;

        public BeforePredicate(O value, boolean orEquals) {
            this.value = Objects.requireNonNull(value);
            this.orEquals = orEquals;
        }

        @Override
        public boolean test(O o) {
            int i = o.compareTo(value);
            return i < 0 || i == 0 && orEquals;
        }

    }

    final class AfterPredicate> implements Predicate {
        private final O value;
        private final boolean orEquals;

        public AfterPredicate(O value, boolean orEquals) {
            this.value = Objects.requireNonNull(value);
            this.orEquals = orEquals;
        }

        @Override
        public boolean test(O o) {
            int i = o.compareTo(value);
            return i > 0 || i == 0 && orEquals;
        }

    }

    final class StringContainsPredicate implements Predicate {
        private final String value;

        public StringContainsPredicate(String value) {
            this.value = Objects.requireNonNull(value);
        }

        @Override
        public boolean test(String o) {
            return o.contains(value);
        }

    }

    final class StringMatchesPredicate implements Predicate {
        private final String value;

        public StringMatchesPredicate(String value) {
            this.value = Objects.requireNonNull(value);
        }

        @Override
        public boolean test(String o) {
            return o.matches(value);
        }

    }

    interface ComparableQuery, P extends JavaBeanPredicateBuilder> extends Query {

        default P isBefore(V propertyValue) {
            return addPredicate(new BeforePredicate<>(propertyValue, false));
        }

        default P isBeforeOrEquals(V propertyValue) {
            return addPredicate(new BeforePredicate<>(propertyValue, true));
        }

        default P isAfter(V propertyValue) {
            return addPredicate(new AfterPredicate<>(propertyValue, true));
        }

        default P isAfterOrEquals(V propertyValue) {
            return addPredicate(new AfterPredicate<>(propertyValue, false));
        }

        default P isBetween(V min, V max) {
            return addPredicate(new AfterPredicate<>(min, false).and(new BeforePredicate<>(max, false)));
        }

        default P isBetweenOrEquals(V min, V max) {
            return addPredicate(new AfterPredicate<>(min, true).and(new BeforePredicate<>(max, true)));
        }
    }

    abstract class QuerySupport> implements Query {
        private final P parent;
        private final Function getter;

        protected QuerySupport(P parent, Function getter) {
            this.parent = parent;
            this.getter = getter;
        }

        @Override
        public P parent() {
            return parent;
        }

        @Override
        public V getter(O element) {
            return element == null ? null : getter.apply(element);
        }

    }

    class ObjectQuery> extends QuerySupport {

        public ObjectQuery(P parent, Function getter) {
            super(parent, getter);
        }

        public P isIn(Collection propertyValue) {
            return addPredicate(new InPredicate<>(propertyValue));
        }

        public P isNotIn(Collection propertyValue) {
            return addPredicate(new InPredicate<>(propertyValue).negate());
        }

        public P isNull() {
            P parent = parent();
            parent.addPredicate(d -> {
                V v = getter(d);
                return v == null;
            });
            return parent;
        }

        public P isNotNull() {
            P parent = parent();
            parent.addPredicate(d -> {
                V v = getter(d);
                return v != null;
            });
            return parent;
        }

    }

    class PrimitiveObjectQuery> extends QuerySupport {

        public PrimitiveObjectQuery(P parent, Function getter) {
            super(parent, getter);
        }

    }

    class BooleanQuery> extends ObjectQuery {

        public BooleanQuery(P parent, Function getter) {
            super(parent, getter);
        }

        public P isTrue() {
            return isEquals(true);
        }

        public P isFalse() {
            return isEquals(false);
        }

    }

    class PrimitiveBooleanQuery> extends PrimitiveObjectQuery {

        public PrimitiveBooleanQuery(P parent, Function getter) {
            super(parent, getter);
        }

        public P isTrue() {
            return isEquals(true);
        }

        public P isFalse() {
            return isEquals(false);
        }
    }

    class SimpleComparableQuery, P extends JavaBeanPredicateBuilder> extends ObjectQuery implements ComparableQuery {

        public SimpleComparableQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class PrimitiveComparableQuery, P extends JavaBeanPredicateBuilder> extends PrimitiveObjectQuery implements ComparableQuery {

        public PrimitiveComparableQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StringQuery> extends SimpleComparableQuery {

        public StringQuery(P parent, Function getter) {
            super(parent, getter);
        }

        public P contains(String propertyValue) {
            return addPredicate(new StringContainsPredicate(propertyValue));
        }

        public P notContains(String propertyValue) {
            return addPredicate(new StringContainsPredicate(propertyValue).negate());
        }

        public P matches(String propertyValue) {
            return addPredicate(new StringMatchesPredicate(propertyValue));
        }

        public P notMatches(String propertyValue) {
            return addPredicate(new StringMatchesPredicate(propertyValue).negate());
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy