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

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

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 io.ultreia.java4all.bean.JavaBeanPredicateBuilder.AfterPredicate;
import io.ultreia.java4all.bean.JavaBeanPredicateBuilder.BeforePredicate;
import io.ultreia.java4all.bean.JavaBeanPredicateBuilder.InPredicate;
import io.ultreia.java4all.bean.JavaBeanPredicateBuilder.StringContainsPredicate;
import io.ultreia.java4all.bean.JavaBeanPredicateBuilder.StringMatchesPredicate;

import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
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 JavaBeanStream {

    void addPredicate(Predicate predicate);

    Predicate predicate();

    void addComparator(Comparator predicate);

    Optional> comparator();

    Stream stream();

    default Stream filter() {
        return sortedStream().filter(predicate());
    }

    default boolean anyMatch() {
        return stream().anyMatch(predicate());
    }

    default boolean allMatch() {
        return stream().allMatch(predicate());
    }

    default boolean noneMatch() {
        return stream().noneMatch(predicate());
    }

    default Stream sortedStream() {
        Optional> comparator = comparator();
        return comparator.isPresent() ? stream().sorted(comparator.get()) : stream();
    }

    default Optional min() {
        Optional> comparator = comparator();
        return comparator.isPresent() ? stream().min(comparator.get()) : Optional.empty();
    }

    default Optional max() {
        Optional> comparator = comparator();
        return comparator.isPresent() ? stream().max(comparator.get()) : Optional.empty();
    }

    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);
        }
    }


    interface ComparableQuery, P extends JavaBeanStream> extends Query {

        default P sort() {
            return addComparator(Comparator.comparing(this::getter));
        }

        default P reservedSort() {
            return addComparator(Comparator.comparing(this::getter).reversed());
        }

        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 JavaBeanPredicateBuilder.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)));
        }

        default P addComparator(Comparator comparator) {
            P parent = parent();
            parent.addComparator(comparator);
            return parent;
        }
    }

    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, P extends JavaBeanStream> extends QuerySupport implements ComparableQuery {

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

    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 BooleanQuery> extends SimpleComparableQuery {


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

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

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

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

        public SimpleComparableQuery(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