co.streamx.fluent.JPA.Streams Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-jpa Show documentation
Show all versions of fluent-jpa Show documentation
Fluent query implementation for JPA repositories
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 super A, ? super B, ? extends C> 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 super FROM, ? extends TO> mapper) {
List result = new ArrayList<>(source.size());
source.forEach(f -> result.add(mapper.apply(f)));
return result;
}
static T find(List list,
Predicate super T> 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);
}
}
}