com.landawn.abacus.util.stream.ParallelIteratorIntStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general programming library in Java
/*
* Copyright (C) 2016 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.Callable;
import com.landawn.abacus.util.CompletableFuture;
import com.landawn.abacus.util.IndexedInt;
import com.landawn.abacus.util.IntIterator;
import com.landawn.abacus.util.IntList;
import com.landawn.abacus.util.IntSummaryStatistics;
import com.landawn.abacus.util.LongMultiset;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.NullabLe;
import com.landawn.abacus.util.OptionalDouble;
import com.landawn.abacus.util.OptionalInt;
import com.landawn.abacus.util.Holder;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntBiFunction;
import com.landawn.abacus.util.function.IntBinaryOperator;
import com.landawn.abacus.util.function.IntConsumer;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.IntPredicate;
import com.landawn.abacus.util.function.IntToByteFunction;
import com.landawn.abacus.util.function.IntToCharFunction;
import com.landawn.abacus.util.function.IntToDoubleFunction;
import com.landawn.abacus.util.function.IntToFloatFunction;
import com.landawn.abacus.util.function.IntToLongFunction;
import com.landawn.abacus.util.function.IntToShortFunction;
import com.landawn.abacus.util.function.IntTriFunction;
import com.landawn.abacus.util.function.IntUnaryOperator;
import com.landawn.abacus.util.function.ObjIntConsumer;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @since 0.8
*
* @author Haiyang Li
*/
final class ParallelIteratorIntStream extends IteratorIntStream {
private final int maxThreadNum;
private final Splitor splitor;
private volatile IteratorIntStream sequential;
private volatile Stream boxed;
ParallelIteratorIntStream(final IntIterator values, final Collection closeHandlers, final boolean sorted, final int maxThreadNum,
final Splitor splitor) {
super(values, closeHandlers, sorted);
this.maxThreadNum = N.min(maxThreadNum, MAX_THREAD_NUM_PER_OPERATION);
this.splitor = splitor == null ? DEFAULT_SPLITOR : splitor;
}
ParallelIteratorIntStream(final IntStream stream, final Set closeHandlers, final boolean sorted, final int maxThreadNum, final Splitor splitor) {
this(stream.exIterator(), mergeCloseHandlers(stream, closeHandlers), sorted, maxThreadNum, splitor);
}
ParallelIteratorIntStream(final Stream stream, final Set closeHandlers, final boolean sorted, final int maxThreadNum,
final Splitor splitor) {
this(intIterator(stream.exIterator()), mergeCloseHandlers(stream, closeHandlers), sorted, maxThreadNum, splitor);
}
@Override
public IntStream filter(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().filter(predicate).exIterator(), closeHandlers, sorted, maxThreadNum, splitor);
}
final Stream stream = boxed().filter(new Predicate() {
@Override
public boolean test(Integer value) {
return predicate.test(value);
}
});
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public IntStream takeWhile(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().takeWhile(predicate).exIterator(), closeHandlers, sorted, maxThreadNum, splitor);
}
final Stream stream = boxed().takeWhile(new Predicate() {
@Override
public boolean test(Integer value) {
return predicate.test(value);
}
});
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public IntStream dropWhile(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().dropWhile(predicate).exIterator(), closeHandlers, sorted, maxThreadNum, splitor);
}
final Stream stream = boxed().dropWhile(new Predicate() {
@Override
public boolean test(Integer value) {
return predicate.test(value);
}
});
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public IntStream map(final IntUnaryOperator mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().map(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final IntStream stream = boxed().mapToInt(new ToIntFunction() {
@Override
public int applyAsInt(Integer value) {
return mapper.applyAsInt(value);
}
});
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public CharStream mapToChar(final IntToCharFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorCharStream(sequential().mapToChar(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final CharStream stream = boxed().mapToChar(new ToCharFunction() {
@Override
public char applyAsChar(Integer value) {
return mapper.applyAsChar(value);
}
});
return new ParallelIteratorCharStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public ByteStream mapToByte(final IntToByteFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorByteStream(sequential().mapToByte(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final ByteStream stream = boxed().mapToByte(new ToByteFunction() {
@Override
public byte applyAsByte(Integer value) {
return mapper.applyAsByte(value);
}
});
return new ParallelIteratorByteStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public ShortStream mapToShort(final IntToShortFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorShortStream(sequential().mapToShort(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final ShortStream stream = boxed().mapToShort(new ToShortFunction() {
@Override
public short applyAsShort(Integer value) {
return mapper.applyAsShort(value);
}
});
return new ParallelIteratorShortStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public LongStream mapToLong(final IntToLongFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().mapToLong(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final LongStream stream = boxed().mapToLong(new ToLongFunction() {
@Override
public long applyAsLong(Integer value) {
return mapper.applyAsLong(value);
}
});
return new ParallelIteratorLongStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public FloatStream mapToFloat(final IntToFloatFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().mapToFloat(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final FloatStream stream = boxed().mapToFloat(new ToFloatFunction() {
@Override
public float applyAsFloat(Integer value) {
return mapper.applyAsFloat(value);
}
});
return new ParallelIteratorFloatStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public DoubleStream mapToDouble(final IntToDoubleFunction mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().mapToDouble(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final DoubleStream stream = boxed().mapToDouble(new ToDoubleFunction() {
@Override
public double applyAsDouble(Integer value) {
return mapper.applyAsDouble(value);
}
});
return new ParallelIteratorDoubleStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public Stream mapToObj(final IntFunction extends U> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().mapToObj(mapper).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
return boxed().map(new Function() {
@Override
public U apply(Integer value) {
return mapper.apply(value);
}
});
}
@Override
public IntStream flatMap(final IntFunction extends IntStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().flatMap(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final IntStream stream = boxed().flatMapToInt(new Function() {
@Override
public IntStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public CharStream flatMapToChar(final IntFunction extends CharStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorCharStream(sequential().flatMapToChar(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final CharStream stream = boxed().flatMapToChar(new Function() {
@Override
public CharStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorCharStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public ByteStream flatMapToByte(final IntFunction extends ByteStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorByteStream(sequential().flatMapToByte(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final ByteStream stream = boxed().flatMapToByte(new Function() {
@Override
public ByteStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorByteStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public ShortStream flatMapToShort(final IntFunction extends ShortStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorShortStream(sequential().flatMapToShort(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final ShortStream stream = boxed().flatMapToShort(new Function() {
@Override
public ShortStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorShortStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public LongStream flatMapToLong(final IntFunction extends LongStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().flatMapToLong(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final LongStream stream = boxed().flatMapToLong(new Function() {
@Override
public LongStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorLongStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public FloatStream flatMapToFloat(final IntFunction extends FloatStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().flatMapToFloat(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final FloatStream stream = boxed().flatMapToFloat(new Function() {
@Override
public FloatStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorFloatStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public DoubleStream flatMapToDouble(final IntFunction extends DoubleStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().flatMapToDouble(mapper).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final DoubleStream stream = boxed().flatMapToDouble(new Function() {
@Override
public DoubleStream apply(Integer value) {
return mapper.apply(value);
}
});
return new ParallelIteratorDoubleStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public Stream flatMapToObj(final IntFunction extends Stream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().flatMapToObj(mapper).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
return boxed().flatMap(new Function>() {
@Override
public Stream apply(Integer value) {
return mapper.apply(value);
}
});
}
@Override
public Stream split(final int size) {
return new ParallelIteratorStream<>(sequential().split(size).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
@Override
public Stream splitToList(final int size) {
return new ParallelIteratorStream<>(sequential().splitToList(size).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
@Override
public Stream split(final U identity, final BiFunction super Integer, ? super U, Boolean> predicate,
final Consumer super U> identityUpdate) {
return new ParallelIteratorStream<>(sequential().split(identity, predicate, identityUpdate).iterator(), closeHandlers, false, null, maxThreadNum,
splitor);
}
@Override
public Stream splitToList(final U identity, final BiFunction super Integer, ? super U, Boolean> predicate,
final Consumer super U> identityUpdate) {
return new ParallelIteratorStream<>(sequential().splitToList(identity, predicate, identityUpdate).iterator(), closeHandlers, false, null, maxThreadNum,
splitor);
}
@Override
public Stream splitBy(final IntPredicate where) {
N.requireNonNull(where);
final List testedElements = new ArrayList<>();
final NullabLe first = indexed().findFirst(new Predicate() {
@Override
public boolean test(IndexedInt indexed) {
synchronized (testedElements) {
testedElements.add(indexed);
}
return !where.test(indexed.value());
}
});
N.sort(testedElements, INDEXED_INT_COMPARATOR);
final int n = first.isPresent() ? (int) first.get().index() : testedElements.size();
final IntList list1 = new IntList(n);
final IntList list2 = new IntList(testedElements.size() - n);
for (int i = 0; i < n; i++) {
list1.add(testedElements.get(i).value());
}
for (int i = n, size = testedElements.size(); i < size; i++) {
list2.add(testedElements.get(i).value());
}
final IntStream[] a = new IntStream[2];
a[0] = new ArrayIntStream(list1.array(), null, sorted);
a[1] = new IteratorIntStream(elements, null, sorted);
if (N.notNullOrEmpty(list2)) {
if (sorted) {
a[1] = new IteratorIntStream(a[1].prepend(list2.stream()).exIterator(), null, sorted);
} else {
a[1] = a[1].prepend(list2.stream());
}
}
return new ParallelArrayStream<>(a, 0, a.length, closeHandlers, false, null, maxThreadNum, splitor);
}
@Override
public Stream sliding(final int windowSize, final int increment) {
return new ParallelIteratorStream<>(sequential().sliding(windowSize, increment).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
@Override
public Stream slidingToList(final int windowSize, final int increment) {
return new ParallelIteratorStream<>(sequential().slidingToList(windowSize, increment).iterator(), closeHandlers, false, null, maxThreadNum, splitor);
}
@Override
public IntStream top(int n) {
return top(n, INT_COMPARATOR);
}
@Override
public IntStream top(int n, Comparator super Integer> comparator) {
return new ParallelIteratorIntStream(this.sequential().top(n, comparator).exIterator(), closeHandlers, sorted, maxThreadNum, splitor);
}
@Override
public IntStream sorted() {
if (sorted) {
return this;
}
return new ParallelIteratorIntStream(new ExIntIterator() {
int[] a = null;
int toIndex = 0;
int cursor = 0;
@Override
public boolean hasNext() {
if (a == null) {
sort();
}
return cursor < toIndex;
}
@Override
public int nextInt() {
if (a == null) {
sort();
}
if (cursor >= toIndex) {
throw new NoSuchElementException();
}
return a[cursor++];
}
@Override
public long count() {
if (a == null) {
sort();
}
return toIndex - cursor;
}
@Override
public void skip(long n) {
if (a == null) {
sort();
}
cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
}
@Override
public int[] toArray() {
if (a == null) {
sort();
}
if (cursor == 0) {
return a;
} else {
return N.copyOfRange(a, cursor, toIndex);
}
}
private void sort() {
a = elements.toArray();
toIndex = a.length;
N.parallelSort(a);
}
}, closeHandlers, true, maxThreadNum, splitor);
}
@Override
public IntStream peek(final IntConsumer action) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().peek(action).exIterator(), closeHandlers, false, maxThreadNum, splitor);
}
final IntStream stream = boxed().peek(new Consumer() {
@Override
public void accept(Integer t) {
action.accept(t);
}
}).sequential().mapToInt(ToIntFunction.UNBOX);
return new ParallelIteratorIntStream(stream, closeHandlers, false, maxThreadNum, splitor);
}
@Override
public IntStream limit(final long maxSize) {
if (maxSize < 0) {
throw new IllegalArgumentException("'maxSize' can't be negative: " + maxSize);
}
return new ParallelIteratorIntStream(new ExIntIterator() {
private long cnt = 0;
@Override
public boolean hasNext() {
return cnt < maxSize && elements.hasNext();
}
@Override
public int nextInt() {
if (cnt >= maxSize) {
throw new NoSuchElementException();
}
cnt++;
return elements.nextInt();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers, sorted, maxThreadNum, splitor);
}
@Override
public IntStream skip(final long n) {
if (n < 0) {
throw new IllegalArgumentException("The skipped number can't be negative: " + n);
} else if (n == 0) {
return this;
}
return new ParallelIteratorIntStream(new ExIntIterator() {
private boolean skipped = false;
@Override
public boolean hasNext() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.hasNext();
}
@Override
public int nextInt() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.nextInt();
}
@Override
public long count() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.count();
}
@Override
public void skip(long n2) {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
elements.skip(n2);
}
@Override
public int[] toArray() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.toArray();
}
}, closeHandlers, sorted, maxThreadNum, splitor);
}
@Override
public void forEach(final IntConsumer action) {
if (maxThreadNum <= 1) {
sequential().forEach(action);
return;
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
action.accept(next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
}
@Override
public int[] toArray() {
return elements.toArray();
}
@Override
public IntList toIntList() {
return IntList.of(toArray());
}
@Override
public List toList() {
final List result = new ArrayList<>();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public > R toList(Supplier supplier) {
final R result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public Set toSet() {
final Set result = new HashSet<>();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public > R toSet(Supplier supplier) {
final R result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public Multiset toMultiset() {
final Multiset result = new Multiset<>();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public Multiset toMultiset(Supplier extends Multiset> supplier) {
final Multiset result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public LongMultiset toLongMultiset() {
final LongMultiset result = new LongMultiset<>();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public LongMultiset toLongMultiset(Supplier extends LongMultiset> supplier) {
final LongMultiset result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextInt());
}
return result;
}
@Override
public > M toMap(final IntFunction extends K> keyExtractor, final IntFunction extends U> valueMapper,
final BinaryOperator mergeFunction, final Supplier mapFactory) {
if (maxThreadNum <= 1) {
return sequential().toMap(keyExtractor, valueMapper, mergeFunction, mapFactory);
}
final Function super Integer, ? extends K> keyExtractor2 = new Function() {
@Override
public K apply(Integer value) {
return keyExtractor.apply(value);
}
};
final Function super Integer, ? extends U> valueMapper2 = new Function() {
@Override
public U apply(Integer value) {
return valueMapper.apply(value);
}
};
return boxed().toMap(keyExtractor2, valueMapper2, mergeFunction, mapFactory);
}
@Override
public > M toMap(final IntFunction extends K> classifier, final Collector downstream,
final Supplier mapFactory) {
if (maxThreadNum <= 1) {
return sequential().toMap(classifier, downstream, mapFactory);
}
final Function super Integer, ? extends K> classifier2 = new Function() {
@Override
public K apply(Integer value) {
return classifier.apply(value);
}
};
return boxed().toMap(classifier2, downstream, mapFactory);
}
@Override
public > Multimap toMultimap(final IntFunction extends K> keyExtractor, final IntFunction extends U> valueMapper,
final Supplier> mapFactory) {
if (maxThreadNum <= 1) {
return sequential().toMultimap(keyExtractor, valueMapper, mapFactory);
}
final Function super Integer, ? extends K> keyExtractor2 = new Function() {
@Override
public K apply(Integer value) {
return keyExtractor.apply(value);
}
};
final Function super Integer, ? extends U> valueMapper2 = new Function() {
@Override
public U apply(Integer value) {
return valueMapper.apply(value);
}
};
return boxed().toMultimap(keyExtractor2, valueMapper2, mapFactory);
}
@Override
public int reduce(final int identity, final IntBinaryOperator op) {
if (maxThreadNum <= 1) {
return sequential().reduce(identity, op);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Integer call() {
int result = identity;
int next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
result = op.applyAsInt(result, next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
return result;
}
}));
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Integer result = null;
try {
for (CompletableFuture future : futureList) {
if (result == null) {
result = future.get();
} else {
result = op.applyAsInt(result, future.get());
}
}
} catch (Exception e) {
throw N.toRuntimeException(e);
}
return result == null ? identity : result;
}
@Override
public OptionalInt reduce(final IntBinaryOperator accumulator) {
if (maxThreadNum <= 1) {
return sequential().reduce(accumulator);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Integer call() {
int result = 0;
synchronized (elements) {
if (elements.hasNext()) {
result = elements.nextInt();
} else {
return null;
}
}
int next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
result = accumulator.applyAsInt(result, next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
return result;
}
}));
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Integer result = null;
try {
for (CompletableFuture future : futureList) {
final Integer tmp = future.get();
if (tmp == null) {
continue;
} else if (result == null) {
result = tmp;
} else {
result = accumulator.applyAsInt(result, tmp);
}
}
} catch (Exception e) {
throw N.toRuntimeException(e);
}
return result == null ? OptionalInt.empty() : OptionalInt.of(result);
}
@Override
public R collect(final Supplier supplier, final ObjIntConsumer accumulator, final BiConsumer combiner) {
if (maxThreadNum <= 1) {
return sequential().collect(supplier, accumulator, combiner);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public R call() {
final R container = supplier.get();
int next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
accumulator.accept(container, next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
return container;
}
}));
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
R container = (R) NONE;
try {
for (CompletableFuture future : futureList) {
if (container == NONE) {
container = future.get();
} else {
combiner.accept(container, future.get());
}
}
} catch (Exception e) {
throw N.toRuntimeException(e);
}
return container == NONE ? supplier.get() : container;
}
@Override
public OptionalInt head() {
if (head == null) {
head = elements.hasNext() ? OptionalInt.of(elements.nextInt()) : OptionalInt.empty();
tail = new ParallelIteratorIntStream(elements, closeHandlers, sorted, maxThreadNum, splitor);
}
return head;
}
@Override
public IntStream tail() {
if (tail == null) {
head = elements.hasNext() ? OptionalInt.of(elements.nextInt()) : OptionalInt.empty();
tail = new ParallelIteratorIntStream(elements, closeHandlers, sorted, maxThreadNum, splitor);
}
return tail;
}
@Override
public IntStream head2() {
if (head2 == null) {
final int[] a = elements.toArray();
head2 = new ParallelArrayIntStream(a, 0, a.length == 0 ? 0 : a.length - 1, closeHandlers, sorted, maxThreadNum, splitor);
tail2 = a.length == 0 ? OptionalInt.empty() : OptionalInt.of(a[a.length - 1]);
}
return head2;
}
@Override
public OptionalInt tail2() {
if (tail2 == null) {
final int[] a = elements.toArray();
head2 = new ParallelArrayIntStream(a, 0, a.length == 0 ? 0 : a.length - 1, closeHandlers, sorted, maxThreadNum, splitor);
tail2 = a.length == 0 ? OptionalInt.empty() : OptionalInt.of(a[a.length - 1]);
}
return tail2;
}
@Override
public OptionalInt min() {
if (elements.hasNext() == false) {
return OptionalInt.empty();
} else if (sorted) {
return OptionalInt.of(elements.nextInt());
}
int candidate = elements.nextInt();
int next = 0;
while (elements.hasNext()) {
next = elements.nextInt();
if (N.compare(next, candidate) < 0) {
candidate = next;
}
}
return OptionalInt.of(candidate);
}
@Override
public OptionalInt max() {
if (elements.hasNext() == false) {
return OptionalInt.empty();
} else if (sorted) {
int next = 0;
while (elements.hasNext()) {
next = elements.nextInt();
}
return OptionalInt.of(next);
}
int candidate = elements.nextInt();
int next = 0;
while (elements.hasNext()) {
next = elements.nextInt();
if (N.compare(next, candidate) > 0) {
candidate = next;
}
}
return OptionalInt.of(candidate);
}
@Override
public OptionalInt kthLargest(int k) {
N.checkArgument(k > 0, "'k' must be bigger than 0");
if (elements.hasNext() == false) {
return OptionalInt.empty();
}
final NullabLe optional = boxed().kthLargest(k, INT_COMPARATOR);
return optional.isPresent() ? OptionalInt.of(optional.get()) : OptionalInt.empty();
}
@Override
public long sum() {
long result = 0;
while (elements.hasNext()) {
result += elements.nextInt();
}
return result;
}
@Override
public OptionalDouble average() {
if (elements.hasNext() == false) {
return OptionalDouble.empty();
}
return sequential().average();
}
@Override
public long count() {
return elements.count();
}
@Override
public IntSummaryStatistics summarize() {
final IntSummaryStatistics result = new IntSummaryStatistics();
while (elements.hasNext()) {
result.accept(elements.nextInt());
}
return result;
}
@Override
public boolean anyMatch(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().anyMatch(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(false);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int next = 0;
try {
while (result.isFalse() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
if (predicate.test(next)) {
result.setTrue();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
return result.value();
}
@Override
public boolean allMatch(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().allMatch(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(true);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
if (predicate.test(next) == false) {
result.setFalse();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
return result.value();
}
@Override
public boolean noneMatch(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().noneMatch(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(true);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextInt();
} else {
break;
}
}
if (predicate.test(next)) {
result.setFalse();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
return result.value();
}
@Override
public OptionalInt findFirst(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().findFirst(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder> resultHolder = new Holder<>();
final MutableLong index = MutableLong.of(0);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
final Pair pair = new Pair<>();
try {
while (resultHolder.value() == null && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
pair.left = index.getAndIncrement();
pair.right = elements.nextInt();
} else {
break;
}
}
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left < resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
return resultHolder.value() == null ? OptionalInt.empty() : OptionalInt.of(resultHolder.value().right);
}
@Override
public OptionalInt findLast(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().findLast(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder> resultHolder = new Holder<>();
final MutableLong index = MutableLong.of(0);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
final Pair pair = new Pair<>();
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
pair.left = index.getAndIncrement();
pair.right = elements.nextInt();
} else {
break;
}
}
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left > resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complete(futureList, eHolder);
return resultHolder.value() == null ? OptionalInt.empty() : OptionalInt.of(resultHolder.value().right);
}
@Override
public OptionalInt findAny(final IntPredicate predicate) {
if (maxThreadNum <= 1) {
return sequential().findAny(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder
© 2015 - 2025 Weber Informatics LLC | Privacy Policy