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

co.streamx.fluent.JPA.Streams Maven / Gradle / Ivy

The newest version!
package co.streamx.fluent.JPA;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.RandomAccess;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import lombok.RequiredArgsConstructor;

interface Streams {

    static  Stream zip(List lista,
                                          List listb,
                                          BiFunction zipper) {
        int longestLength = Math.max(lista.size(), listb.size());
        return IntStream.range(0, longestLength).mapToObj(i -> {
            return zipper.apply(i < lista.size() ? lista.get(i) : null, i < listb.size() ? listb.get(i) : null);
        });
    }

    static  List map(List source,
                                          Function mapper) {
        List result = new ArrayList<>(source.size());
        source.forEach(f -> result.add(mapper.apply(f)));
        return result;
    }

    static  T find(List list,
                      Predicate predicate) {

        for (T x : list) {
            if (predicate.test(x))
                return x;
        }

        throw new IndexOutOfBoundsException();
    }

    static  List join(List a,
                            List b) {
        return new ListList<>(a, b);
    }

    @SuppressWarnings("serial")
    @RequiredArgsConstructor
    static final class ListList extends AbstractList implements RandomAccess, java.io.Serializable {

        private final List a;
        private final List b;

        @Override
        public int size() {
            return a.size() + b.size();
        }

        @Override
        public E get(int index) {
            int sizeA = a.size();
            return index < sizeA ? a.get(index) : b.get(index - sizeA);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy