com.landawn.abacus.util.stream.ParallelArrayStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-util-all-jdk7 Show documentation
Show all versions of abacus-util-all-jdk7 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.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
import com.landawn.abacus.util.ByteIterator;
import com.landawn.abacus.util.CharIterator;
import com.landawn.abacus.util.CompletableFuture;
import com.landawn.abacus.util.DoubleIterator;
import com.landawn.abacus.util.FloatIterator;
import com.landawn.abacus.util.Holder;
import com.landawn.abacus.util.Indexed;
import com.landawn.abacus.util.IntIterator;
import com.landawn.abacus.util.LongIterator;
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.MutableInt;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.Nullable;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.ShortIterator;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
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.IntFunction;
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;
import com.landawn.abacus.util.function.TriFunction;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @param
* @since 0.8
*
* @author Haiyang Li
*/
final class ParallelArrayStream extends ArrayStream {
private final int maxThreadNum;
private final Splitor splitor;
private volatile ArrayStream sequential;
ParallelArrayStream(final T[] values, final int fromIndex, final int toIndex, final boolean sorted, Comparator super T> comparator, int maxThreadNum,
Splitor splitor, final Collection closeHandlers) {
super(values, fromIndex, toIndex, sorted, comparator, closeHandlers);
this.maxThreadNum = checkMaxThreadNum(maxThreadNum);
this.splitor = splitor == null ? DEFAULT_SPLITOR : splitor;
}
@Override
public Stream filter(final Predicate super T> predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().filter(predicate).iterator(), sorted, cmp, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
while (cursor < to) {
next = elements[cursor++];
if (predicate.test(next)) {
hasNext = true;
break;
}
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
while (true) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(next)) {
hasNext = true;
break;
}
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream takeWhile(final Predicate super T> predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().takeWhile(predicate).iterator(), sorted, cmp, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
final MutableBoolean hasMore = MutableBoolean.of(true);
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false && hasMore.isTrue()) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
hasNext = true;
} else {
hasMore.setFalse();
}
}
if (hasNext && predicate.test(next) == false) {
hasNext = false;
hasMore.setFalse();
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream dropWhile(final Predicate super T> predicate) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().dropWhile(predicate).iterator(), sorted, cmp, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
final MutableBoolean dropped = MutableBoolean.of(false);
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
// Only one thread is kept for running after it's dropped.
if (dropped.isTrue()) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
hasNext = true;
}
}
} else {
while (dropped.isFalse()) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(next) == false) {
hasNext = true;
dropped.setTrue();
break;
}
}
if (hasNext == false && dropped.isTrue()) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
hasNext = true;
}
}
}
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream map(final Function super T, ? extends R> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().map(mapper).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public R next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.apply(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public R next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
R result = mapper.apply((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
// @Override
// public Stream biMap(final BiFunction super T, ? super T, ? extends R> mapper, final boolean ignoreNotPaired) {
// if (maxThreadNum <= 1) {
// return new ParallelIteratorStream<>(sequential().biMap(mapper, ignoreNotPaired).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
// }
//
// final int atLeast = ignoreNotPaired ? 2 : 1;
// final int count = (toIndex - fromIndex) / 2 + (ignoreNotPaired || (toIndex - fromIndex) % 2 == 0 ? 0 : 1);
// final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
// final List> iters = new ArrayList<>(threadNum);
//
// if (splitor == Splitor.ARRAY) {
// final int sliceSize = count / maxThreadNum + (count % maxThreadNum == 0 ? 0 : 1);
//
// for (int i = 0; i < threadNum; i++) {
// final int sliceIndex = i;
// iters.add(new ObjIteratorEx() {
// private int cursor = fromIndex + sliceIndex * sliceSize * 2;
// private final int to = toIndex - cursor > sliceSize * 2 ? cursor + sliceSize * 2 : toIndex;
//
// @Override
// public boolean hasNext() {
// return to - cursor >= atLeast;
// }
//
// @Override
// public R next() {
// if (to - cursor < atLeast) {
// throw new NoSuchElementException();
// }
//
// return mapper.apply(elements[cursor++], cursor == toIndex ? null : elements[cursor++]);
// }
// });
// }
// } else {
// final MutableInt cursor = MutableInt.of(fromIndex);
//
// for (int i = 0; i < threadNum; i++) {
// iters.add(new ObjIteratorEx() {
//
// private Object pre = NONE;
// private Object next = NONE;
//
// @Override
// public boolean hasNext() {
// if (pre == NONE) {
// synchronized (elements) {
// if (toIndex - cursor.intValue() >= atLeast) {
// pre = elements[cursor.getAndIncrement()];
// next = cursor.intValue() == toIndex ? null : elements[cursor.getAndIncrement()];
// }
// }
// }
//
// return pre != NONE;
// }
//
// @Override
// public R next() {
// if (pre == NONE && hasNext() == false) {
// throw new NoSuchElementException();
// }
//
// final R result = mapper.apply((T) pre, (T) next);
// pre = NONE;
// next = NONE;
// return result;
// }
//
// });
// }
// }
//
// return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
// }
//
// @Override
// public Stream triMap(final TriFunction super T, ? super T, ? super T, ? extends R> mapper, final boolean ignoreNotPaired) {
// if (maxThreadNum <= 1) {
// return new ParallelIteratorStream<>(sequential().triMap(mapper, ignoreNotPaired).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
// }
//
// final int atLeast = ignoreNotPaired ? 3 : 1;
// final int count = (toIndex - fromIndex) / 3 + (ignoreNotPaired || (toIndex - fromIndex) % 3 == 0 ? 0 : 1);
// final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
// final List> iters = new ArrayList<>(threadNum);
//
// if (splitor == Splitor.ARRAY) {
// final int sliceSize = count / maxThreadNum + (count % maxThreadNum == 0 ? 0 : 1);
//
// for (int i = 0; i < threadNum; i++) {
// final int sliceIndex = i;
// iters.add(new ObjIteratorEx() {
// private int cursor = fromIndex + sliceIndex * sliceSize * 3;
// private final int to = toIndex - cursor > sliceSize * 3 ? cursor + sliceSize * 3 : toIndex;
//
// @Override
// public boolean hasNext() {
// return to - cursor >= atLeast;
// }
//
// @Override
// public R next() {
// if (to - cursor < atLeast) {
// throw new NoSuchElementException();
// }
//
// return mapper.apply(elements[cursor++], cursor == toIndex ? null : elements[cursor++], cursor == toIndex ? null : elements[cursor++]);
// }
// });
// }
// } else {
// final MutableInt cursor = MutableInt.of(fromIndex);
//
// for (int i = 0; i < threadNum; i++) {
// iters.add(new ObjIteratorEx() {
//
// private Object prepre = NONE;
// private Object pre = NONE;
// private Object next = NONE;
//
// @Override
// public boolean hasNext() {
// if (prepre == NONE) {
// synchronized (elements) {
// if (toIndex - cursor.intValue() >= atLeast) {
// prepre = elements[cursor.getAndIncrement()];
// pre = cursor.intValue() == toIndex ? null : elements[cursor.getAndIncrement()];
// next = cursor.intValue() == toIndex ? null : elements[cursor.getAndIncrement()];
// }
// }
// }
//
// return prepre != NONE;
// }
//
// @Override
// public R next() {
// if (prepre == NONE && hasNext() == false) {
// throw new NoSuchElementException();
// }
//
// final R result = mapper.apply((T) prepre, (T) pre, (T) next);
// prepre = NONE;
// pre = NONE;
// next = NONE;
// return result;
// }
//
// });
// }
// }
//
// return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
// }
@Override
public Stream slidingMap(final BiFunction super T, ? super T, R> mapper, final int increment, final boolean ignoreNotPaired) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().slidingMap(mapper, increment).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
final int windowSize = 2;
N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment);
final List> iters = new ArrayList<>(maxThreadNum);
final MutableInt curIndex = MutableInt.of(fromIndex);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private int cursor = -1;
@Override
public boolean hasNext() {
if (cursor == -1) {
synchronized (elements) {
if (ignoreNotPaired ? toIndex - curIndex.intValue() >= windowSize : curIndex.intValue() < toIndex) {
cursor = curIndex.value();
curIndex.setValue(increment < toIndex - cursor && windowSize < toIndex - cursor ? cursor + increment : toIndex);
}
}
}
return cursor != -1;
}
@Override
public R next() {
if (cursor == -1 && hasNext() == false) {
throw new NoSuchElementException();
}
final R result = mapper.apply(elements[cursor], cursor < toIndex - 1 ? elements[cursor + 1] : null);
cursor = -1;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream slidingMap(final TriFunction super T, ? super T, ? super T, R> mapper, final int increment, final boolean ignoreNotPaired) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().slidingMap(mapper, increment).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
final int windowSize = 3;
N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment);
final List> iters = new ArrayList<>(maxThreadNum);
final MutableInt curIndex = MutableInt.of(fromIndex);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private int cursor = -1;
@Override
public boolean hasNext() {
if (cursor == -1) {
synchronized (elements) {
if (ignoreNotPaired ? toIndex - curIndex.intValue() >= windowSize : curIndex.intValue() < toIndex) {
cursor = curIndex.value();
curIndex.setValue(increment < toIndex - cursor && windowSize < toIndex - cursor ? cursor + increment : toIndex);
}
}
}
return cursor != -1;
}
@Override
public R next() {
if (cursor == -1 && hasNext() == false) {
throw new NoSuchElementException();
}
final R result = mapper.apply(elements[cursor], cursor < toIndex - 1 ? elements[cursor + 1] : null,
cursor < toIndex - 2 ? elements[cursor + 2] : null);
cursor = -1;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream mapFirst(final Function super T, ? extends T> mapperForFirst) {
N.requireNonNull(mapperForFirst);
if (fromIndex == toIndex) {
return this;
} else if (toIndex - fromIndex == 1) {
return map(mapperForFirst);
} else {
final Iterator iter = new ObjIteratorEx() {
private int cursor = fromIndex;
@Override
public boolean hasNext() {
return cursor < toIndex;
}
@Override
public T next() {
if (cursor >= toIndex) {
throw new NoSuchElementException();
}
if (cursor == fromIndex) {
return mapperForFirst.apply(elements[cursor++]);
} else {
return elements[cursor++];
}
}
// @Override
// public long count() {
// return toIndex - cursor;
// }
//
// @Override
// public void skip(long n) {
// cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
// }
@Override
public long count() {
if (hasNext()) {
next();
return toIndex - cursor + 1;
}
return 0;
}
@Override
public void skip(long n) {
if (hasNext()) {
next();
n -= 1;
cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
}
}
@Override
public A[] toArray(A[] a) {
a = a.length >= toIndex - cursor ? a : (A[]) N.newArray(a.getClass().getComponentType(), toIndex - cursor);
for (int i = 0, len = toIndex - cursor; i < len; i++) {
if (cursor == fromIndex) {
a[i] = (A) mapperForFirst.apply(elements[cursor++]);
} else {
a[i] = (A) elements[cursor++];
}
}
return a;
}
};
return new ParallelIteratorStream<>(iter, false, null, maxThreadNum, splitor, closeHandlers);
}
}
@SuppressWarnings("resource")
@Override
public Stream mapFirstOrElse(final Function super T, ? extends R> mapperForFirst, final Function super T, ? extends R> mapperForElse) {
N.requireNonNull(mapperForFirst);
N.requireNonNull(mapperForElse);
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().mapFirstOrElse(mapperForFirst, mapperForElse).iterator(), false, null, maxThreadNum, splitor,
closeHandlers);
}
if (fromIndex == toIndex) {
return (Stream) this;
} else if (toIndex - fromIndex == 1) {
return map(mapperForFirst);
} else {
final Function mapperForFirst2 = (Function) mapperForFirst;
final Function mapperForElse2 = (Function) mapperForElse;
return new ParallelArrayStream<>(elements, fromIndex + 1, toIndex, sorted, cmp, maxThreadNum, splitor, closeHandlers).map(mapperForElse2)
.prepend(new ArrayStream<>(elements, fromIndex, fromIndex + 1).map(mapperForFirst2));
}
}
@Override
public Stream mapLast(final Function super T, ? extends T> mapperForLast) {
N.requireNonNull(mapperForLast);
if (fromIndex == toIndex) {
return this;
} else if (toIndex - fromIndex == 1) {
return map(mapperForLast);
} else {
final Iterator iter = new ObjIteratorEx() {
private int last = toIndex - 1;
private int cursor = fromIndex;
@Override
public boolean hasNext() {
return cursor < toIndex;
}
@Override
public T next() {
if (cursor >= toIndex) {
throw new NoSuchElementException();
}
if (cursor == last) {
return mapperForLast.apply(elements[cursor++]);
} else {
return elements[cursor++];
}
}
// @Override
// public long count() {
// return toIndex - cursor;
// }
//
// @Override
// public void skip(long n) {
// cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
// }
@Override
public A[] toArray(A[] a) {
a = a.length >= toIndex - cursor ? a : (A[]) N.newArray(a.getClass().getComponentType(), toIndex - cursor);
for (int i = 0, len = toIndex - cursor; i < len; i++) {
if (cursor == last) {
a[i] = (A) mapperForLast.apply(elements[cursor++]);
} else {
a[i] = (A) elements[cursor++];
}
}
return a;
}
};
return new ParallelIteratorStream<>(iter, false, null, maxThreadNum, splitor, closeHandlers);
}
}
@SuppressWarnings("resource")
@Override
public Stream mapLastOrElse(final Function super T, ? extends R> mapperForLast, final Function super T, ? extends R> mapperForElse) {
N.requireNonNull(mapperForLast);
N.requireNonNull(mapperForElse);
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().mapLastOrElse(mapperForLast, mapperForElse).iterator(), false, null, maxThreadNum, splitor,
closeHandlers);
}
if (fromIndex == toIndex) {
return (Stream) this;
} else if (toIndex - fromIndex == 1) {
return map(mapperForLast);
} else {
final Function mapperForLast2 = (Function) mapperForLast;
final Function mapperForElse2 = (Function) mapperForElse;
return new ParallelArrayStream<>(elements, fromIndex, toIndex - 1, sorted, cmp, maxThreadNum, splitor, closeHandlers).map(mapperForElse2)
.append(new ArrayStream<>(elements, toIndex - 1, toIndex).map(mapperForLast2));
}
}
@Override
public CharStream mapToChar(final ToCharFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorCharStream(sequential().mapToChar(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Character next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsChar(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Character next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Character result = mapper.applyAsChar((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorCharStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public ByteStream mapToByte(final ToByteFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorByteStream(sequential().mapToByte(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Byte next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsByte(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Byte next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Byte result = mapper.applyAsByte((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorByteStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public ShortStream mapToShort(final ToShortFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorShortStream(sequential().mapToShort(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Short next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsShort(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Short next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Short result = mapper.applyAsShort((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorShortStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public IntStream mapToInt(final ToIntFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().mapToInt(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Integer next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsInt(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Integer next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Integer result = mapper.applyAsInt((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorIntStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public LongStream mapToLong(final ToLongFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().mapToLong(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Long next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsLong(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Long next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Long result = mapper.applyAsLong((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorLongStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public FloatStream mapToFloat(final ToFloatFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().mapToFloat(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Float next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsFloat(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Float next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Float result = mapper.applyAsFloat((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorFloatStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public DoubleStream mapToDouble(final ToDoubleFunction super T> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().mapToDouble(mapper).iteratorEx(), false, maxThreadNum, splitor, closeHandlers);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
@Override
public boolean hasNext() {
return cursor < to;
}
@Override
public Double next() {
if (cursor >= to) {
throw new NoSuchElementException();
}
return mapper.applyAsDouble(elements[cursor++]);
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
}
}
}
return next != NONE;
}
@Override
public Double next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Double result = mapper.applyAsDouble((T) next);
next = NONE;
return result;
}
});
}
}
return new ParallelIteratorDoubleStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream flatMap(final Function super T, ? extends Stream extends R>> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().flatMap(mapper), false, null, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private Iterator extends R> cur = null;
private Stream extends R> s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public R next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.next();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private Iterator extends R> cur = null;
private Stream extends R> s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public R next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.next();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public CharStream flatMapToChar(final Function super T, ? extends CharStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorCharStream(sequential().flatMapToChar(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private CharIterator cur = null;
private CharStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Character next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextChar();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private CharIterator cur = null;
private CharStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Character next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextChar();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorCharStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public ByteStream flatMapToByte(final Function super T, ? extends ByteStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorByteStream(sequential().flatMapToByte(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private ByteIterator cur = null;
private ByteStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Byte next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextByte();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private ByteIterator cur = null;
private ByteStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Byte next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextByte();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorByteStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public ShortStream flatMapToShort(final Function super T, ? extends ShortStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorShortStream(sequential().flatMapToShort(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private ShortIterator cur = null;
private ShortStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Short next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextShort();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private ShortIterator cur = null;
private ShortStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Short next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextShort();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorShortStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public IntStream flatMapToInt(final Function super T, ? extends IntStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().flatMapToInt(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private IntIterator cur = null;
private IntStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Integer next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextInt();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private IntIterator cur = null;
private IntStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Integer next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextInt();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorIntStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public LongStream flatMapToLong(final Function super T, ? extends LongStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().flatMapToLong(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private LongIterator cur = null;
private LongStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Long next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextLong();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private LongIterator cur = null;
private LongStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Long next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextLong();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorLongStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public FloatStream flatMapToFloat(final Function super T, ? extends FloatStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().flatMapToFloat(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private FloatIterator cur = null;
private FloatStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Float next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextFloat();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private FloatIterator cur = null;
private FloatStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Float next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextFloat();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorFloatStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public DoubleStream flatMapToDouble(final Function super T, ? extends DoubleStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().flatMapToDouble(mapper), false, maxThreadNum, splitor, null);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> iters = new ArrayList<>(threadNum);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
iters.add(new ObjIteratorEx() {
private int cursor = fromIndex + sliceIndex * sliceSize;
private final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
private DoubleIterator cur = null;
private DoubleStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && cursor < to) {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(elements[cursor++]);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Double next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextDouble();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private DoubleIterator cur = null;
private DoubleStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Set tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Double next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextDouble();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
}
final Set newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalLinkedHashSet(1)
: new LocalLinkedHashSet(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorDoubleStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, newCloseHandlers);
}
@Override
public Stream> split(final int size) {
return new ParallelIteratorStream<>(sequential().split(size).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> splitToList(final int size) {
return new ParallelIteratorStream<>(sequential().splitToList(size).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> splitToSet(final int size) {
return new ParallelIteratorStream<>(sequential().splitToSet(size).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> split(final U seed, final BiPredicate super T, ? super U> predicate, final Consumer super U> seedUpdate) {
return new ParallelIteratorStream<>(sequential().split(seed, predicate, seedUpdate).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> splitToList(final U seed, final BiPredicate super T, ? super U> predicate, final Consumer super U> seedUpdate) {
return new ParallelIteratorStream<>(sequential().splitToList(seed, predicate, seedUpdate).iterator(), false, null, maxThreadNum, splitor,
closeHandlers);
}
@Override
public Stream> splitToSet(final U seed, final BiPredicate super T, ? super U> predicate, final Consumer super U> seedUpdate) {
return new ParallelIteratorStream<>(sequential().splitToSet(seed, predicate, seedUpdate).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> splitAt(final int n) {
N.checkArgument(n >= 0, "'n' can't be negative: %s", n);
final Stream[] a = new Stream[2];
final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex;
a[0] = middleIndex == fromIndex ? (Stream) Stream.empty() : new ArrayStream<>(elements, fromIndex, middleIndex, sorted, cmp, null);
a[1] = middleIndex == toIndex ? (Stream) Stream.empty() : new ArrayStream<>(elements, middleIndex, toIndex, sorted, cmp, null);
return new ParallelArrayStream<>(a, 0, a.length, false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> splitBy(final Predicate super T> where) {
N.requireNonNull(where);
final Nullable> first = indexed().findFirst(new Predicate>() {
@Override
public boolean test(Indexed indexed) {
return !where.test(indexed.value());
}
});
return splitAt(first.isPresent() ? (int) first.get().index() : toIndex - fromIndex);
}
@Override
public Stream> sliding(final int windowSize, final int increment) {
return new ParallelIteratorStream<>(sequential().sliding(windowSize, increment).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream> slidingToList(final int windowSize, final int increment) {
return new ParallelIteratorStream<>(sequential().slidingToList(windowSize, increment).iterator(), false, null, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream distinctBy(final Function super T, ?> keyExtractor) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().distinctBy(keyExtractor).iterator(), sorted, cmp, maxThreadNum, splitor, closeHandlers);
}
final Set