com.landawn.abacus.util.stream.ParallelIteratorDoubleStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android-jdk7 Show documentation
Show all versions of abacus-android-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.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import com.landawn.abacus.util.CompletableFuture;
import com.landawn.abacus.util.DoubleIterator;
import com.landawn.abacus.util.Holder;
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.OptionalDouble;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.DoubleBiFunction;
import com.landawn.abacus.util.function.DoubleBinaryOperator;
import com.landawn.abacus.util.function.DoubleConsumer;
import com.landawn.abacus.util.function.DoubleFunction;
import com.landawn.abacus.util.function.DoublePredicate;
import com.landawn.abacus.util.function.DoubleToFloatFunction;
import com.landawn.abacus.util.function.DoubleToIntFunction;
import com.landawn.abacus.util.function.DoubleToLongFunction;
import com.landawn.abacus.util.function.DoubleTriFunction;
import com.landawn.abacus.util.function.DoubleUnaryOperator;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ObjDoubleConsumer;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
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;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @since 0.8
*
* @author Haiyang Li
*/
final class ParallelIteratorDoubleStream extends IteratorDoubleStream {
private final int maxThreadNum;
private final Splitor splitor;
private volatile IteratorDoubleStream sequential;
private volatile Stream boxed;
ParallelIteratorDoubleStream(final DoubleIterator values, final boolean sorted, final int maxThreadNum, final Splitor splitor,
final Collection closeHandlers) {
super(values, sorted, closeHandlers);
this.maxThreadNum = checkMaxThreadNum(maxThreadNum);
this.splitor = splitor == null ? DEFAULT_SPLITOR : splitor;
}
ParallelIteratorDoubleStream(final DoubleStream stream, final boolean sorted, final int maxThreadNum, final Splitor splitor,
final Set closeHandlers) {
this(stream.iteratorEx(), sorted, maxThreadNum, splitor, mergeCloseHandlers(stream, closeHandlers));
}
ParallelIteratorDoubleStream(final Stream stream, final boolean sorted, final int maxThreadNum, final Splitor splitor,
final Set closeHandlers) {
this(doubleIterator(stream.iteratorEx()), sorted, maxThreadNum, splitor, mergeCloseHandlers(stream, closeHandlers));
}
@Override
public DoubleStream filter(final DoublePredicate predicate) {
if (maxThreadNum <= 1) {
return super.filter(predicate);
}
final Stream stream = boxed().filter(new Predicate() {
@Override
public boolean test(Double value) {
return predicate.test(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public DoubleStream takeWhile(final DoublePredicate predicate) {
if (maxThreadNum <= 1) {
return super.takeWhile(predicate);
}
final Stream stream = boxed().takeWhile(new Predicate() {
@Override
public boolean test(Double value) {
return predicate.test(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public DoubleStream dropWhile(final DoublePredicate predicate) {
if (maxThreadNum <= 1) {
return super.dropWhile(predicate);
}
final Stream stream = boxed().dropWhile(new Predicate() {
@Override
public boolean test(Double value) {
return predicate.test(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public DoubleStream map(final DoubleUnaryOperator mapper) {
if (maxThreadNum <= 1) {
return super.map(mapper);
}
final DoubleStream stream = boxed().mapToDouble(new ToDoubleFunction() {
@Override
public double applyAsDouble(Double value) {
return mapper.applyAsDouble(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public IntStream mapToInt(final DoubleToIntFunction mapper) {
if (maxThreadNum <= 1) {
return super.mapToInt(mapper);
}
final IntStream stream = boxed().mapToInt(new ToIntFunction() {
@Override
public int applyAsInt(Double value) {
return mapper.applyAsInt(value);
}
});
return new ParallelIteratorIntStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public LongStream mapToLong(final DoubleToLongFunction mapper) {
if (maxThreadNum <= 1) {
return super.mapToLong(mapper);
}
final LongStream stream = boxed().mapToLong(new ToLongFunction() {
@Override
public long applyAsLong(Double value) {
return mapper.applyAsLong(value);
}
});
return new ParallelIteratorLongStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public FloatStream mapToFloat(final DoubleToFloatFunction mapper) {
if (maxThreadNum <= 1) {
return super.mapToFloat(mapper);
}
final FloatStream stream = boxed().mapToFloat(new ToFloatFunction() {
@Override
public float applyAsFloat(Double value) {
return mapper.applyAsFloat(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream mapToObj(final DoubleFunction extends U> mapper) {
if (maxThreadNum <= 1) {
return super.mapToObj(mapper);
}
return boxed().map(new Function() {
@Override
public U apply(Double value) {
return mapper.apply(value);
}
});
}
@Override
public DoubleStream flatMap(final DoubleFunction extends DoubleStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().flatMap(mapper), false, maxThreadNum, splitor, null);
}
final DoubleStream stream = boxed().flatMapToDouble(new Function() {
@Override
public DoubleStream apply(Double value) {
return mapper.apply(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public IntStream flatMapToInt(final DoubleFunction extends IntStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().flatMapToInt(mapper), false, maxThreadNum, splitor, null);
}
final IntStream stream = boxed().flatMapToInt(new Function() {
@Override
public IntStream apply(Double value) {
return mapper.apply(value);
}
});
return new ParallelIteratorIntStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public LongStream flatMapToLong(final DoubleFunction extends LongStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().flatMapToLong(mapper), false, maxThreadNum, splitor, null);
}
final LongStream stream = boxed().flatMapToLong(new Function() {
@Override
public LongStream apply(Double value) {
return mapper.apply(value);
}
});
return new ParallelIteratorLongStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public FloatStream flatMapToFloat(final DoubleFunction extends FloatStream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().flatMapToFloat(mapper), false, maxThreadNum, splitor, null);
}
final FloatStream stream = boxed().flatMapToFloat(new Function() {
@Override
public FloatStream apply(Double value) {
return mapper.apply(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public Stream flatMapToObj(final DoubleFunction extends Stream> mapper) {
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().flatMapToObj(mapper), false, null, maxThreadNum, splitor, null);
}
return boxed().flatMap(new Function>() {
@Override
public Stream apply(Double value) {
return mapper.apply(value);
}
});
}
@Override
public DoubleStream peek(final DoubleConsumer action) {
if (maxThreadNum <= 1) {
return super.peek(action);
}
final DoubleStream stream = boxed().peek(new Consumer() {
@Override
public void accept(Double t) {
action.accept(t);
}
}).sequential().mapToDouble(ToDoubleFunction.UNBOX);
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public void forEach(final Try.DoubleConsumer action) throws E {
if (maxThreadNum <= 1) {
super.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() {
double next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
action.accept(next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complette(futureList, eHolder, (E) null);
}
@Override
public > M toMap(final DoubleFunction extends K> keyExtractor, final DoubleFunction extends U> valueMapper,
final BinaryOperator mergeFunction, final Supplier mapFactory) {
if (maxThreadNum <= 1) {
return super.toMap(keyExtractor, valueMapper, mergeFunction, mapFactory);
}
final Function super Double, ? extends K> keyExtractor2 = new Function() {
@Override
public K apply(Double value) {
return keyExtractor.apply(value);
}
};
final Function super Double, ? extends U> valueMapper2 = new Function() {
@Override
public U apply(Double value) {
return valueMapper.apply(value);
}
};
return boxed().toMap(keyExtractor2, valueMapper2, mergeFunction, mapFactory);
}
@Override
public > M toMap(final DoubleFunction extends K> classifier, final Collector downstream,
final Supplier mapFactory) {
if (maxThreadNum <= 1) {
return super.toMap(classifier, downstream, mapFactory);
}
final Function super Double, ? extends K> classifier2 = new Function() {
@Override
public K apply(Double value) {
return classifier.apply(value);
}
};
return boxed().toMap(classifier2, downstream, mapFactory);
}
@Override
public double reduce(final double identity, final DoubleBinaryOperator op) {
if (maxThreadNum <= 1) {
return super.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 Double call() {
double result = identity;
double next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
result = op.applyAsDouble(result, next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
return result;
}
}));
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Double result = null;
try {
for (CompletableFuture future : futureList) {
if (result == null) {
result = future.get();
} else {
result = op.applyAsDouble(result, future.get());
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return result == null ? identity : result;
}
@Override
public OptionalDouble reduce(final DoubleBinaryOperator accumulator) {
if (maxThreadNum <= 1) {
return super.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 Double call() {
double result = 0;
synchronized (elements) {
if (elements.hasNext()) {
result = elements.nextDouble();
} else {
return null;
}
}
double next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
result = accumulator.applyAsDouble(result, next);
}
} catch (Throwable e) {
setError(eHolder, e);
}
return result;
}
}));
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Double result = null;
try {
for (CompletableFuture future : futureList) {
final Double tmp = future.get();
if (tmp == null) {
continue;
} else if (result == null) {
result = tmp;
} else {
result = accumulator.applyAsDouble(result, tmp);
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return result == null ? OptionalDouble.empty() : OptionalDouble.of(result);
}
@Override
public R collect(final Supplier supplier, final ObjDoubleConsumer accumulator, final BiConsumer combiner) {
if (maxThreadNum <= 1) {
return super.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();
double next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} 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 (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return container == NONE ? supplier.get() : container;
};
@Override
public boolean anyMatch(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.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() {
double next = 0;
try {
while (result.isFalse() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
if (predicate.test(next)) {
result.setTrue();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public boolean allMatch(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.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() {
double next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
if (predicate.test(next) == false) {
result.setFalse();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public boolean noneMatch(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.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() {
double next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.nextDouble();
} else {
break;
}
}
if (predicate.test(next)) {
result.setFalse();
break;
}
}
} catch (Throwable e) {
setError(eHolder, e);
}
}
}));
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public OptionalDouble findFirst(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.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.nextDouble();
} 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);
}
}
}));
}
complette(futureList, eHolder, (E) null);
return resultHolder.value() == null ? OptionalDouble.empty() : OptionalDouble.of(resultHolder.value().right);
}
@Override
public OptionalDouble findLast(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.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.nextDouble();
} 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);
}
}
}));
}
complette(futureList, eHolder, (E) null);
return resultHolder.value() == null ? OptionalDouble.empty() : OptionalDouble.of(resultHolder.value().right);
}
@Override
public OptionalDouble findAny(final Try.DoublePredicate predicate) throws E {
if (maxThreadNum <= 1) {
return super.findAny(predicate);
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder
© 2015 - 2025 Weber Informatics LLC | Privacy Policy