java parallel stream splitter

public class CacheSplitter<T> implements Spliterator<T> {
// private final Collection<T> collection;
private final T[] values;
private int start;
private int end;
private final int THRESHOLD;
public CacheSplitter(T[] values, int threshold) {
this(values, 0, values.length, threshold);
}
public CacheSplitter(T[] values, int start, int end, int threshold) {
this.values = values;
this.start = start;
this.end = end;
this.THRESHOLD = threshold;
}
@Override
public boolean tryAdvance(Consumer action) {
if(start< end){
action.accept(values[start++]);
return true;
}
return false;
}
@Override
public Spliterator trySplit() {
if(end - start < THRESHOLD){
return null;
}
int mid = (start + end)/2;
return new CacheSplitter(values, start, start= mid+1, THRESHOLD);
}
@Override
public long estimateSize() {
return end - start;
}
@Override
public int characteristics() {
return ORDERED | SIZED | SUBSIZED | NONNULL;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store