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 java.util.Comparator;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

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

    void addComparator(Comparator predicate);

    Optional> comparator();

    Stream stream();

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

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

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

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

    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 StreamQuery, Q extends StreamQuery> extends Query {
    }

    interface StreamComparableQuery, P extends JavaBeanStream, Q extends StreamComparableQuery> extends StreamQuery, ComparableQuery {

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

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

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

    class StreamObjectQuery, Q extends StreamObjectQuery> extends ObjectQuery implements StreamQuery {
        public StreamObjectQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StreamPrimitiveObjectQuery, P extends JavaBeanStream, Q extends StreamPrimitiveObjectQuery> extends PrimitiveObjectQuery implements StreamComparableQuery {
        public StreamPrimitiveObjectQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StreamPrimitiveBooleanQuery, Q extends StreamPrimitiveBooleanQuery> extends PrimitiveBooleanQuery implements StreamComparableQuery {
        public StreamPrimitiveBooleanQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StreamObjectBooleanQuery, Q extends StreamObjectBooleanQuery> extends ObjectBooleanQuery implements StreamComparableQuery {
        public StreamObjectBooleanQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StreamSimpleComparableQuery, P extends JavaBeanStream, Q extends StreamSimpleComparableQuery> extends SimpleComparableQuery implements StreamComparableQuery {
        public StreamSimpleComparableQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

    class StreamStringQuery, Q extends StreamStringQuery> extends StringQuery implements StreamComparableQuery {
        public StreamStringQuery(P parent, Function getter) {
            super(parent, getter);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy