com.google.gwt.emul.java.util.stream.StreamImpl Maven / Gradle / Ivy
/*
* Copyright 2016 Google Inc.
*
* 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 java.util.stream;
import static javaemul.internal.InternalPreconditions.checkNotNull;
import static javaemul.internal.InternalPreconditions.checkState;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.Spliterators.AbstractDoubleSpliterator;
import java.util.Spliterators.AbstractIntSpliterator;
import java.util.Spliterators.AbstractLongSpliterator;
import java.util.Spliterators.AbstractSpliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.LongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
/**
* Main implementation of Stream, wrapping a single spliterator and an optional parent stream.
*/
final class StreamImpl extends TerminatableStream> implements Stream {
/**
* Represents an empty stream, doing nothing for all methods.
*/
static class Empty extends TerminatableStream> implements Stream {
public Empty(TerminatableStream> previous) {
super(previous);
}
@Override
public Stream filter(Predicate super T> predicate) {
throwIfTerminated();
return this;
}
@Override
public Stream map(Function super T, ? extends R> mapper) {
throwIfTerminated();
return (Stream) this;
}
@Override
public IntStream mapToInt(ToIntFunction super T> mapper) {
throwIfTerminated();
return new IntStreamImpl.Empty(this);
}
@Override
public LongStream mapToLong(ToLongFunction super T> mapper) {
throwIfTerminated();
return new LongStreamImpl.Empty(this);
}
@Override
public DoubleStream mapToDouble(ToDoubleFunction super T> mapper) {
throwIfTerminated();
return new DoubleStreamImpl.Empty(this);
}
@Override
public Stream flatMap(Function super T, ? extends Stream extends R>> mapper) {
throwIfTerminated();
return (Stream) this;
}
@Override
public IntStream flatMapToInt(Function super T, ? extends IntStream> mapper) {
throwIfTerminated();
return new IntStreamImpl.Empty(this);
}
@Override
public LongStream flatMapToLong(Function super T, ? extends LongStream> mapper) {
throwIfTerminated();
return new LongStreamImpl.Empty(this);
}
@Override
public DoubleStream flatMapToDouble(Function super T, ? extends DoubleStream> mapper) {
throwIfTerminated();
return new DoubleStreamImpl.Empty(this);
}
@Override
public Stream distinct() {
throwIfTerminated();
return this;
}
@Override
public Stream sorted() {
throwIfTerminated();
return this;
}
@Override
public Stream sorted(Comparator super T> comparator) {
throwIfTerminated();
return this;
}
@Override
public Stream peek(Consumer super T> action) {
throwIfTerminated();
return this;
}
@Override
public Stream limit(long maxSize) {
throwIfTerminated();
checkState(maxSize >= 0, "maxSize may not be negative");
return this;
}
@Override
public Stream skip(long n) {
throwIfTerminated();
checkState(n >= 0, "n may not be negative");
return this;
}
@Override
public void forEach(Consumer super T> action) {
terminate();
// nothing to do
}
@Override
public void forEachOrdered(Consumer super T> action) {
terminate();
// nothing to do
}
@Override
public Object[] toArray() {
terminate();
return new Object[0];
}
@Override
public A[] toArray(IntFunction generator) {
terminate();
return generator.apply(0);
}
@Override
public T reduce(T identity, BinaryOperator accumulator) {
terminate();
return identity;
}
@Override
public Optional reduce(BinaryOperator accumulator) {
terminate();
return Optional.empty();
}
@Override
public U reduce(
U identity, BiFunction accumulator, BinaryOperator combiner) {
terminate();
return identity;
}
@Override
public R collect(
Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
terminate();
return supplier.get();
}
@Override
public R collect(Collector super T, A, R> collector) {
terminate();
return collector.finisher().apply(collector.supplier().get());
}
@Override
public Optional min(Comparator super T> comparator) {
terminate();
return Optional.empty();
}
@Override
public Optional max(Comparator super T> comparator) {
terminate();
return Optional.empty();
}
@Override
public long count() {
terminate();
return 0;
}
@Override
public boolean anyMatch(Predicate super T> predicate) {
terminate();
return false;
}
@Override
public boolean allMatch(Predicate super T> predicate) {
terminate();
return true;
}
@Override
public boolean noneMatch(Predicate super T> predicate) {
terminate();
return true;
}
@Override
public Optional findFirst() {
terminate();
return Optional.empty();
}
@Override
public Optional findAny() {
terminate();
return Optional.empty();
}
@Override
public Iterator iterator() {
terminate();
return Collections.emptyIterator();
}
@Override
public Spliterator spliterator() {
terminate();
return Spliterators.emptySpliterator();
}
@Override
public boolean isParallel() {
throwIfTerminated();
return false;
}
@Override
public Stream sequential() {
throwIfTerminated();
return this;
}
@Override
public Stream parallel() {
throwIfTerminated();
return this;
}
@Override
public Stream unordered() {
throwIfTerminated();
return this;
}
}
/**
* Object to Object map spliterator.
*
* @param the input type
* @param the output type
*/
private static final class MapToObjSpliterator extends Spliterators.AbstractSpliterator {
private final Function super U, ? extends T> map;
private final Spliterator original;
public MapToObjSpliterator(Function super U, ? extends T> map, Spliterator original) {
super(
original.estimateSize(),
original.characteristics() & ~(Spliterator.SORTED | Spliterator.DISTINCT));
checkNotNull(map);
this.map = map;
this.original = original;
}
@Override
public boolean tryAdvance(final Consumer super T> action) {
return original.tryAdvance(u -> action.accept(map.apply(u)));
}
}
/**
* Object to Int map spliterator.
*
* @param the input type
*/
private static final class MapToIntSpliterator extends Spliterators.AbstractIntSpliterator {
private final ToIntFunction super T> map;
private final Spliterator original;
public MapToIntSpliterator(ToIntFunction super T> map, Spliterator original) {
super(
original.estimateSize(),
original.characteristics() & ~(Spliterator.SORTED | Spliterator.DISTINCT));
checkNotNull(map);
this.map = map;
this.original = original;
}
@Override
public boolean tryAdvance(final IntConsumer action) {
return original.tryAdvance(u -> action.accept(map.applyAsInt(u)));
}
}
/**
* Object to Long map spliterator.
*
* @param the input type
*/
private static final class MapToLongSpliterator extends Spliterators.AbstractLongSpliterator {
private final ToLongFunction super T> map;
private final Spliterator original;
public MapToLongSpliterator(ToLongFunction super T> map, Spliterator original) {
super(
original.estimateSize(),
original.characteristics() & ~(Spliterator.SORTED | Spliterator.DISTINCT));
checkNotNull(map);
this.map = map;
this.original = original;
}
@Override
public boolean tryAdvance(final LongConsumer action) {
return original.tryAdvance(u -> action.accept(map.applyAsLong(u)));
}
}
/**
* Object to Double map spliterator.
*
* @param the input type
*/
private static final class MapToDoubleSpliterator
extends Spliterators.AbstractDoubleSpliterator {
private final ToDoubleFunction super T> map;
private final Spliterator original;
public MapToDoubleSpliterator(ToDoubleFunction super T> map, Spliterator original) {
super(
original.estimateSize(),
original.characteristics() & ~(Spliterator.SORTED | Spliterator.DISTINCT));
checkNotNull(map);
this.map = map;
this.original = original;
}
@Override
public boolean tryAdvance(final DoubleConsumer action) {
return original.tryAdvance(u -> action.accept(map.applyAsDouble(u)));
}
}
/**
* Object filter spliterator.
*
* @param the type of data to iterate over
*/
private static final class FilterSpliterator extends Spliterators.AbstractSpliterator {
private final Predicate super T> filter;
private final Spliterator original;
private boolean found;
public FilterSpliterator(Predicate super T> filter, Spliterator original) {
super(
original.estimateSize(),
original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED));
checkNotNull(filter);
this.filter = filter;
this.original = original;
}
@Override
public Comparator super T> getComparator() {
return original.getComparator();
}
@Override
public boolean tryAdvance(final Consumer super T> action) {
found = false;
while (!found
&& original.tryAdvance(
item -> {
if (filter.test(item)) {
found = true;
action.accept(item);
}
})) {
// do nothing, work is done in tryAdvance
}
return found;
}
}
/**
* Object skip spliterator.
*
* @param the type of data to iterate over
*/
private static final class SkipSpliterator extends Spliterators.AbstractSpliterator {
private long skip;
private final Spliterator original;
public SkipSpliterator(long skip, Spliterator original) {
super(
original.hasCharacteristics(Spliterator.SIZED)
? Math.max(0, original.estimateSize() - skip)
: Long.MAX_VALUE,
original.characteristics());
this.skip = skip;
this.original = original;
}
@Override
public Comparator super T> getComparator() {
return original.getComparator();
}
@Override
public boolean tryAdvance(Consumer super T> action) {
while (skip > 0) {
if (!original.tryAdvance(ignore -> { })) {
return false;
}
skip--;
}
return original.tryAdvance(action);
}
}
/**
* Object limit spliterator.
*
* @param the type of data to iterate over
*/
private static final class LimitSpliterator extends Spliterators.AbstractSpliterator {
private final long limit;
private final Spliterator original;
private int position = 0;
public LimitSpliterator(long limit, Spliterator original) {
super(
original.hasCharacteristics(Spliterator.SIZED)
? Math.min(original.estimateSize(), limit)
: Long.MAX_VALUE,
original.characteristics());
this.limit = limit;
this.original = original;
}
@Override
public Comparator super T> getComparator() {
return original.getComparator();
}
@Override
public boolean tryAdvance(Consumer super T> action) {
if (position >= limit) {
return false;
}
boolean result = original.tryAdvance(action);
position++;
return result;
}
}
/**
* Value holder for various stream operations.
*/
private static final class ValueConsumer implements Consumer {
T value;
@Override
public void accept(T value) {
this.value = value;
}
}
private final Spliterator spliterator;
public StreamImpl(TerminatableStream> prev, Spliterator spliterator) {
super(prev);
this.spliterator = spliterator;
}
// terminal
@Override
public Spliterator spliterator() {
terminate();
return spliterator;
}
@Override
public Iterator iterator() {
return Spliterators.iterator(spliterator());
}
@Override
public long count() {
terminate();
long count = 0;
while (spliterator.tryAdvance(a -> { })) {
count++;
}
return count;
}
@Override
public void forEach(Consumer super T> action) {
forEachOrdered(action);
}
@Override
public void forEachOrdered(Consumer super T> action) {
terminate();
spliterator.forEachRemaining(action);
}
@Override
public Object[] toArray() {
return toArray(Object[]::new);
}
@Override
public A[] toArray(IntFunction generator) {
List collected = collect(Collectors.toList());
return collected.toArray(generator.apply(collected.size()));
}
@Override
public R collect(
Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
return collect(
Collector.of(
supplier,
accumulator,
(a, b) -> {
combiner.accept(a, b);
return a;
}));
}
@Override
public R collect(final Collector super T, A, R> collector) {
return collector
.finisher()
.apply(
reduce(
collector.supplier().get(),
(a, t) -> {
collector.accumulator().accept(a, t);
return a;
},
collector.combiner()));
}
@Override
public Optional findFirst() {
terminate();
ValueConsumer holder = new ValueConsumer();
if (spliterator.tryAdvance(holder)) {
return Optional.of(holder.value);
}
return Optional.empty();
}
@Override
public Optional findAny() {
return findFirst();
}
private static final Consumer