Add support of stream API

This commit is contained in:
Alexey Andreev 2017-11-19 19:47:09 +03:00
parent 2fb6ca7001
commit 608e62ae3b
160 changed files with 10176 additions and 31 deletions

View File

@ -15,13 +15,47 @@
*/
package org.teavm.classlib.java.lang;
import java.util.function.Consumer;
import org.teavm.classlib.java.util.TIterator;
import org.teavm.classlib.java.util.TSpliterator;
/**
*
* @author Alexey Andreev
* @param <T> type this collection returns.
*/
public interface TIterable<T> {
TIterator<T> iterator();
default TSpliterator<T> spliterator() {
TIterator<T> iterator = iterator();
return new TSpliterator<T>() {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public void forEachRemaining(Consumer<? super T> action) {
while (iterator.hasNext()) {
action.accept(iterator.next());
}
}
@Override
public TSpliterator<T> trySplit() {
return null;
}
@Override
public long estimateSize() {
return -1;
}
@Override
public int characteristics() {
return 0;
}
};
}
}

View File

@ -19,11 +19,6 @@ import java.lang.reflect.Array;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.lang.TUnsupportedOperationException;
/**
*
* @author Alexey Andreev
* @param <E>
*/
public abstract class TAbstractCollection<E> extends TObject implements TCollection<E> {
protected TAbstractCollection() {
}

View File

@ -15,11 +15,8 @@
*/
package org.teavm.classlib.java.util;
/**
*
* @author Alexey Andreev
* @param <E>
*/
import java.util.Arrays;
public abstract class TAbstractSet<E> extends TAbstractCollection<E> implements TSet<E> {
public TAbstractSet() {
super();
@ -45,4 +42,31 @@ public abstract class TAbstractSet<E> extends TAbstractCollection<E> implements
}
return modified;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TSet)) {
return false;
}
TSet<?> other = (TSet<?>) obj;
if (size() != other.size()) {
return false;
}
for (TIterator<?> iter = other.iterator(); iter.hasNext();) {
if (!contains(iter.next())) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return Arrays.hashCode(toArray());
}
}

View File

@ -29,6 +29,14 @@ import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.lang.TString;
import org.teavm.classlib.java.lang.TStringBuilder;
import org.teavm.classlib.java.lang.reflect.TArray;
import org.teavm.classlib.java.util.stream.TDoubleStream;
import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.TLongStream;
import org.teavm.classlib.java.util.stream.TStream;
import org.teavm.classlib.java.util.stream.doubleimpl.TArrayDoubleStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TArrayStreamImpl;
import org.teavm.classlib.java.util.stream.intimpl.TArrayIntStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TArrayLongStreamImpl;
public class TArrays extends TObject {
public static char[] copyOf(char[] array, int length) {
@ -1519,7 +1527,7 @@ public class TArrays extends TObject {
public static TString deepToString(Object[] a) {
TStringBuilder sb = new TStringBuilder();
deepToString(a, sb, new TArrayList<Object[]>());
deepToString(a, sb, new TArrayList<>());
return TString.wrap(sb.toString());
}
@ -1548,4 +1556,48 @@ public class TArrays extends TObject {
out.append(TObject.wrap(a));
}
}
public static <T> TStream<T> stream(T[] array) {
return new TArrayStreamImpl<>(array, 0, array.length);
}
public static <T> TStream<T> stream(T[] array, int startInclusive, int endExclusive) {
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > array.length) {
throw new ArrayIndexOutOfBoundsException();
}
return new TArrayStreamImpl<>(array, startInclusive, endExclusive);
}
public static TIntStream stream(int[] array) {
return new TArrayIntStreamImpl(array, 0, array.length);
}
public static TIntStream stream(int[] array, int startInclusive, int endExclusive) {
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > array.length) {
throw new ArrayIndexOutOfBoundsException();
}
return new TArrayIntStreamImpl(array, startInclusive, endExclusive);
}
public static TLongStream stream(long[] array) {
return new TArrayLongStreamImpl(array, 0, array.length);
}
public static TLongStream stream(long[] array, int startInclusive, int endExclusive) {
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > array.length) {
throw new ArrayIndexOutOfBoundsException();
}
return new TArrayLongStreamImpl(array, startInclusive, endExclusive);
}
public static TDoubleStream stream(double[] array) {
return new TArrayDoubleStreamImpl(array, 0, array.length);
}
public static TDoubleStream stream(double[] array, int startInclusive, int endExclusive) {
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > array.length) {
throw new ArrayIndexOutOfBoundsException();
}
return new TArrayDoubleStreamImpl(array, startInclusive, endExclusive);
}
}

View File

@ -15,13 +15,12 @@
*/
package org.teavm.classlib.java.util;
import java.util.Spliterator;
import org.teavm.classlib.java.lang.TIterable;
import org.teavm.classlib.java.util.stream.TStream;
import org.teavm.classlib.java.util.stream.impl.TSpliteratorOverCollection;
import org.teavm.classlib.java.util.stream.impl.TStreamOverSpliterator;
/**
*
* @author Alexey Andreev
* @param <E>
*/
public interface TCollection<E> extends TIterable<E> {
int size();
@ -46,4 +45,14 @@ public interface TCollection<E> extends TIterable<E> {
boolean retainAll(TCollection<?> c);
void clear();
@Override
default TSpliterator<E> spliterator() {
return new TSpliteratorOverCollection<>(this);
}
@SuppressWarnings("unchecked")
default TStream<E> stream() {
return new TStreamOverSpliterator<>((Spliterator<E>) spliterator());
}
}

View File

@ -15,15 +15,18 @@
*/
package org.teavm.classlib.java.util;
/**
*
* @author Alexey Andreev
* @param <E>
*/
import java.util.function.Consumer;
public interface TIterator<E> {
boolean hasNext();
E next();
void remove();
default void forEachRemaining(Consumer<? super E> action) {
while (hasNext()) {
action.accept(next());
}
}
}

View File

@ -15,11 +15,6 @@
*/
package org.teavm.classlib.java.util;
/**
*
* @author Alexey Andreev
* @param <E>
*/
public interface TList<E> extends TCollection<E> {
boolean addAll(int index, TCollection<? extends E> c);
@ -40,4 +35,8 @@ public interface TList<E> extends TCollection<E> {
TListIterator<E> listIterator(int index);
TList<E> subList(int fromIndex, int toIndex);
default void sort(TComparator<? super E> c) {
TCollections.sort(this, c);
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.util.NoSuchElementException;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
public class TOptionalDouble {
private static TOptionalDouble emptyInstance;
private final double value;
private TOptionalDouble(double value) {
this.value = value;
}
@SuppressWarnings("unchecked")
public static TOptionalDouble empty() {
if (emptyInstance == null) {
emptyInstance = new TOptionalDouble(0);
}
return emptyInstance;
}
public static TOptionalDouble of(double value) {
return new TOptionalDouble(value);
}
public double getAsDouble() {
if (this == emptyInstance) {
throw new NoSuchElementException();
}
return value;
}
public boolean isPresent() {
return this != emptyInstance;
}
public void ifPresent(DoubleConsumer consumer) {
if (this != emptyInstance) {
consumer.accept(value);
}
}
public double orElse(double other) {
return this != emptyInstance ? value : other;
}
public double orElseGet(DoubleSupplier other) {
return this != emptyInstance ? value : other.getAsDouble();
}
public <X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (!isPresent()) {
throw exceptionSupplier.get();
}
return value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TOptionalDouble)) {
return false;
}
return ((TOptionalDouble) obj).value == value;
}
@Override
public int hashCode() {
return Double.hashCode(value);
}
@Override
public String toString() {
return isPresent() ? "OptionalDouble.of(" + value + ")" : "OptionalDouble.empty()";
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.util.NoSuchElementException;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
public class TOptionalInt {
private static TOptionalInt emptyInstance;
private final int value;
private TOptionalInt(int value) {
this.value = value;
}
@SuppressWarnings("unchecked")
public static TOptionalInt empty() {
if (emptyInstance == null) {
emptyInstance = new TOptionalInt(0);
}
return emptyInstance;
}
public static TOptionalInt of(int value) {
return new TOptionalInt(value);
}
public int getAsInt() {
if (this == emptyInstance) {
throw new NoSuchElementException();
}
return value;
}
public boolean isPresent() {
return this != emptyInstance;
}
public void ifPresent(IntConsumer consumer) {
if (this != emptyInstance) {
consumer.accept(value);
}
}
public int orElse(int other) {
return this != emptyInstance ? value : other;
}
public int orElseGet(IntSupplier other) {
return this != emptyInstance ? value : other.getAsInt();
}
public <X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (!isPresent()) {
throw exceptionSupplier.get();
}
return value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TOptionalInt)) {
return false;
}
return ((TOptionalInt) obj).value == value;
}
@Override
public int hashCode() {
return value;
}
@Override
public String toString() {
return isPresent() ? "OptionalInt.of(" + value + ")" : "OptionalInt.empty()";
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.util.NoSuchElementException;
import java.util.function.LongConsumer;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
public class TOptionalLong {
private static TOptionalLong emptyInstance;
private final long value;
private TOptionalLong(long value) {
this.value = value;
}
@SuppressWarnings("unchecked")
public static TOptionalLong empty() {
if (emptyInstance == null) {
emptyInstance = new TOptionalLong(0);
}
return emptyInstance;
}
public static TOptionalLong of(long value) {
return new TOptionalLong(value);
}
public long getAsLong() {
if (this == emptyInstance) {
throw new NoSuchElementException();
}
return value;
}
public boolean isPresent() {
return this != emptyInstance;
}
public void ifPresent(LongConsumer consumer) {
if (this != emptyInstance) {
consumer.accept(value);
}
}
public long orElse(long other) {
return this != emptyInstance ? value : other;
}
public long orElseGet(LongSupplier other) {
return this != emptyInstance ? value : other.getAsLong();
}
public <X extends Throwable> long orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (!isPresent()) {
throw exceptionSupplier.get();
}
return value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TOptionalLong)) {
return false;
}
return ((TOptionalLong) obj).value == value;
}
@Override
public int hashCode() {
return Long.hashCode(value);
}
@Override
public String toString() {
return isPresent() ? "OptionalLong.of(" + value + ")" : "OptionalLong.empty()";
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
public interface TPrimitiveIterator<T, S> extends Iterator<T> {
void forEachRemaining(S action);
interface OfInt extends TPrimitiveIterator<Integer, IntConsumer> {
@Override
default void forEachRemaining(Consumer<? super Integer> action) {
while (hasNext()) {
action.accept(nextInt());
}
}
@Override
default void forEachRemaining(IntConsumer action) {
while (hasNext()) {
action.accept(nextInt());
}
}
int nextInt();
@Override
default Integer next() {
return nextInt();
}
}
interface OfLong extends TPrimitiveIterator<Long, LongConsumer> {
@Override
default void forEachRemaining(Consumer<? super Long> action) {
while (hasNext()) {
action.accept(nextLong());
}
}
@Override
default void forEachRemaining(LongConsumer action) {
while (hasNext()) {
action.accept(nextLong());
}
}
long nextLong();
@Override
default Long next() {
return nextLong();
}
}
interface OfDouble extends TPrimitiveIterator<Double, DoubleConsumer> {
@Override
default void forEachRemaining(Consumer<? super Double> action) {
while (hasNext()) {
action.accept(nextDouble());
}
}
@Override
default void forEachRemaining(DoubleConsumer action) {
while (hasNext()) {
action.accept(nextDouble());
}
}
double nextDouble();
@Override
default Double next() {
return nextDouble();
}
}
}

View File

@ -0,0 +1,107 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
public interface TSpliterator<T> {
int ORDERED = 16;
int DISTINCT = 1;
int SORTED = 4;
int SIZED = 64;
int NONNULL = 256;
int IMMUTABLE = 1024;
int CONCURRENT = 4096;
int SUBSIZED = 16384;
boolean tryAdvance(Consumer<? super T> action);
default void forEachRemaining(Consumer<? super T> action) {
while (tryAdvance(action)) {
// just repeat
}
}
TSpliterator<T> trySplit();
long estimateSize();
default long getExactSizeIfKnown() {
return (characteristics() & SIZED) != 0 ? estimateSize() : -1;
}
int characteristics();
default boolean hasCharacteristics(int characteristics) {
return (characteristics() & characteristics) == characteristics;
}
default Comparator<? super T> getComparator() {
throw new IllegalStateException();
}
interface OfPrimitive<T, C, S extends OfPrimitive<T, C, S>> {
S trySplit();
boolean tryAdvance(C action);
default void forEachRemaining(C action) {
while (tryAdvance(action)) {
// continue
}
}
}
interface OfInt extends OfPrimitive<Integer, IntConsumer, OfInt> {
default boolean tryAdvance(Consumer<? super Integer> action) {
return tryAdvance((IntConsumer) action::accept);
}
default void forEachRemaining(Consumer<? super Integer> action) {
while (tryAdvance(action)) {
// continue
}
}
}
interface OfLong extends OfPrimitive<Long, LongConsumer, OfLong> {
default boolean tryAdvance(Consumer<? super Long> action) {
return tryAdvance((LongConsumer) action::accept);
}
default void forEachRemaining(Consumer<? super Long> action) {
while (tryAdvance(action)) {
// continue
}
}
}
interface OfDouble extends OfPrimitive<Double, DoubleConsumer, OfDouble> {
default boolean tryAdvance(Consumer<? super Double> action) {
return tryAdvance((DoubleConsumer) action::accept);
}
default void forEachRemaining(Consumer<? super Double> action) {
while (tryAdvance(action)) {
// continue
}
}
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongBinaryOperator {
long applyAsLong(long left, long right);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongConsumer {
void accept(long value);
default TLongConsumer andThen(TLongConsumer after) {
return v -> {
accept(v);
after.accept(v);
};
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongFunction<R> {
R apply(long value);
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongPredicate {
boolean test(long value);
default TLongPredicate and(TLongPredicate other) {
return v -> test(v) && other.test(v);
}
default TLongPredicate negate() {
return v -> !test(v);
}
default TLongPredicate or(TLongPredicate other) {
return v -> test(v) || other.test(v);
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongSupplier {
long getAsLong();
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongToDoubleFunction {
double applyAsDouble(long value);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongToIntFunction {
int applyAsInt(long value);
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.function;
@FunctionalInterface
public interface TLongUnaryOperator {
long applyAsLong(long operand);
default TLongUnaryOperator compose(TLongUnaryOperator before) {
return v -> applyAsLong(before.applyAsLong(v));
}
default TLongUnaryOperator andThen(TLongUnaryOperator after) {
return v -> after.applyAsLong(applyAsLong(v));
}
static TLongUnaryOperator identity() {
return v -> v;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.Iterator;
import java.util.Spliterator;
public interface TBaseStream<T, S extends TBaseStream<T, S>> extends AutoCloseable {
Iterator<T> iterator();
Spliterator<T> spliterator();
boolean isParallel();
S sequential();
S parallel();
S unordered();
S onClose(Runnable closeHandler);
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
public interface TCollector<T, A, R> {
enum Characteristics {
CONCURRENT,
UNORDERED,
IDENTITY_FINISH
}
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
BinaryOperator<A> combiner();
Function<A, R> finisher();
Set<Characteristics> characteristics();
static <T, R> TCollector<T, R, R> of(Supplier<R> supplier, BiConsumer<R, T> accumulator, BinaryOperator<R> combiner,
Characteristics... characteristics) {
return of(supplier, accumulator, combiner, x -> x, characteristics);
}
static <T, A, R> TCollector<T, A, R> of(Supplier<A> supplier, BiConsumer<A, T> accumulator,
BinaryOperator<A> combiner, Function<A, R> finisher, Characteristics... characteristics) {
EnumSet<Characteristics> characteristicsSet = EnumSet.noneOf(Characteristics.class);
characteristicsSet.addAll(Arrays.asList(characteristics));
return new TCollectorImpl<>(supplier, accumulator, combiner, finisher, characteristicsSet);
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
class TCollectorImpl<T, A, R> implements TCollector<T, A, R> {
private Supplier<A> supplier;
private BiConsumer<A, T> accumulator;
private BinaryOperator<A> combiner;
private Function<A, R> finisher;
private EnumSet<Characteristics> characteristics;
TCollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Function<A, R> finisher, EnumSet<Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
@Override
public Supplier<A> supplier() {
return supplier;
}
@Override
public BiConsumer<A, T> accumulator() {
return accumulator;
}
@Override
public BinaryOperator<A> combiner() {
return combiner;
}
@Override
public Function<A, R> finisher() {
return finisher;
}
@Override
public Set<Characteristics> characteristics() {
return characteristics;
}
}

View File

@ -0,0 +1,72 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
public final class TCollectors {
private TCollectors() {
}
public static <T, C extends Collection<T>> TCollector<T, ?, C> toCollection(Supplier<C> collectionFactory) {
return TCollector.of(collectionFactory, Collection::add, (a, b) -> {
a.addAll(b);
return a;
});
}
public static <T> TCollector<T, ?, List<T>> toList() {
return toCollection(ArrayList::new);
}
public static <T> TCollector<T, ?, Set<T>> toSet() {
return toCollection(HashSet::new);
}
public static TCollector<CharSequence, ?, String> joining() {
return TCollector.of(StringBuilder::new, StringBuilder::append, StringBuilder::append,
StringBuilder::toString);
}
public static TCollector<CharSequence, ?, String> joining(CharSequence delimiter) {
return joining(delimiter, "", "");
}
public static TCollector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix,
CharSequence suffix) {
BiConsumer<StringBuilder, CharSequence> accumulator = (sb, item) -> {
if (sb.length() > 0) {
sb.append(delimiter);
}
sb.append(item);
};
BinaryOperator<StringBuilder> combiner = (a, b) -> {
if (a.length() > 0) {
a.append(delimiter);
}
return a.append(b);
};
return TCollector.of(StringBuilder::new, accumulator, combiner,
sb -> sb.insert(0, prefix).append(suffix).toString());
}
}

View File

@ -0,0 +1,147 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.OptionalDouble;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.ObjDoubleConsumer;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.doubleimpl.TArrayDoubleStreamImpl;
import org.teavm.classlib.java.util.stream.doubleimpl.TDoubleStreamBuilder;
import org.teavm.classlib.java.util.stream.doubleimpl.TEmptyDoubleStreamImpl;
import org.teavm.classlib.java.util.stream.doubleimpl.TGenerateDoubleStream;
import org.teavm.classlib.java.util.stream.doubleimpl.TGenericConcatDoubleStream;
import org.teavm.classlib.java.util.stream.doubleimpl.TIterateDoubleStream;
import org.teavm.classlib.java.util.stream.doubleimpl.TSimpleDoubleStreamImpl;
import org.teavm.classlib.java.util.stream.doubleimpl.TSingleDoubleStreamImpl;
import org.teavm.classlib.java.util.stream.doubleimpl.TSpecializedConcatDoubleStream;
public interface TDoubleStream extends TBaseStream<Double, TDoubleStream> {
interface Builder {
void accept(double t);
default Builder add(double t) {
accept(t);
return this;
}
TDoubleStream build();
}
TDoubleStream filter(DoublePredicate predicate);
TDoubleStream map(DoubleUnaryOperator mapper);
<U> TStream<U> mapToObj(DoubleFunction<? extends U> mapper);
TIntStream mapToInt(DoubleToIntFunction mapper);
TLongStream mapToLong(DoubleToLongFunction mapper);
TDoubleStream flatMap(DoubleFunction<? extends TDoubleStream> mapper);
TDoubleStream distinct();
TDoubleStream sorted();
TDoubleStream peek(DoubleConsumer action);
TDoubleStream limit(long maxSize);
TDoubleStream skip(long n);
void forEach(DoubleConsumer action);
void forEachOrdered(DoubleConsumer action);
double[] toArray();
double reduce(double identity, DoubleBinaryOperator accumulator);
OptionalDouble reduce(DoubleBinaryOperator op);
<R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner);
double sum();
OptionalDouble min();
OptionalDouble max();
long count();
OptionalDouble average();
boolean anyMatch(DoublePredicate predicate);
boolean allMatch(DoublePredicate predicate);
boolean noneMatch(DoublePredicate predicate);
OptionalDouble findFirst();
OptionalDouble findAny();
TStream<Double> boxed();
@Override
PrimitiveIterator.OfDouble iterator();
@Override
Spliterator.OfDouble spliterator();
static TDoubleStream.Builder builder() {
return new TDoubleStreamBuilder();
}
static TDoubleStream empty() {
return new TEmptyDoubleStreamImpl();
}
static TDoubleStream of(double t) {
return new TSingleDoubleStreamImpl(t);
}
static TDoubleStream of(double... values) {
return new TArrayDoubleStreamImpl(values, 0, values.length);
}
static TDoubleStream iterate(double seed, DoubleUnaryOperator f) {
return new TIterateDoubleStream(seed, f);
}
static TDoubleStream generate(DoubleSupplier s) {
return new TGenerateDoubleStream(s);
}
static TDoubleStream concat(TDoubleStream a, TDoubleStream b) {
if (a instanceof TSimpleDoubleStreamImpl && b instanceof TSimpleDoubleStreamImpl) {
return new TSpecializedConcatDoubleStream((TSimpleDoubleStreamImpl) a, (TSimpleDoubleStreamImpl) b);
} else {
return new TGenericConcatDoubleStream(a, b);
}
}
}

View File

@ -0,0 +1,161 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.intimpl.TArrayIntStreamImpl;
import org.teavm.classlib.java.util.stream.intimpl.TEmptyIntStreamImpl;
import org.teavm.classlib.java.util.stream.intimpl.TGenerateIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TGenericConcatIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TIntStreamBuilder;
import org.teavm.classlib.java.util.stream.intimpl.TIterateIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TRangeIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl;
import org.teavm.classlib.java.util.stream.intimpl.TSingleIntStreamImpl;
import org.teavm.classlib.java.util.stream.intimpl.TSpecializedConcatIntStream;
public interface TIntStream extends TBaseStream<Integer, TIntStream> {
interface Builder {
void accept(int t);
default Builder add(int t) {
accept(t);
return this;
}
TIntStream build();
}
TIntStream filter(IntPredicate predicate);
TIntStream map(IntUnaryOperator mapper);
<U> TStream<U> mapToObj(IntFunction<? extends U> mapper);
TLongStream mapToLong(IntToLongFunction mapper);
TDoubleStream mapToDouble(IntToDoubleFunction mapper);
TIntStream flatMap(IntFunction<? extends TIntStream> mapper);
TIntStream distinct();
TIntStream sorted();
TIntStream peek(IntConsumer action);
TIntStream limit(long maxSize);
TIntStream skip(long n);
void forEach(IntConsumer action);
void forEachOrdered(IntConsumer action);
int[] toArray();
int reduce(int identity, IntBinaryOperator accumulator);
OptionalInt reduce(IntBinaryOperator op);
<R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner);
int sum();
OptionalInt min();
OptionalInt max();
long count();
OptionalDouble average();
boolean anyMatch(IntPredicate predicate);
boolean allMatch(IntPredicate predicate);
boolean noneMatch(IntPredicate predicate);
OptionalInt findFirst();
OptionalInt findAny();
TLongStream asLongStream();
TDoubleStream asDoubleStream();
TStream<Integer> boxed();
@Override
PrimitiveIterator.OfInt iterator();
@Override
Spliterator.OfInt spliterator();
static Builder builder() {
return new TIntStreamBuilder();
}
static TIntStream empty() {
return new TEmptyIntStreamImpl();
}
static TIntStream of(int t) {
return new TSingleIntStreamImpl(t);
}
static TIntStream of(int... values) {
return new TArrayIntStreamImpl(values, 0, values.length);
}
static TIntStream iterate(int seed, IntUnaryOperator f) {
return new TIterateIntStream(seed, f);
}
static TIntStream generate(IntSupplier s) {
return new TGenerateIntStream(s);
}
static TIntStream range(int startInclusive, int endExclusive) {
return new TRangeIntStream(startInclusive, endExclusive);
}
static TIntStream rangeClosed(int startInclusive, int endInclusive) {
return new TRangeIntStream(startInclusive, endInclusive + 1);
}
static TIntStream concat(TIntStream a, TIntStream b) {
if (a instanceof TSimpleIntStreamImpl && b instanceof TSimpleIntStreamImpl) {
return new TSpecializedConcatIntStream((TSimpleIntStreamImpl) a, (TSimpleIntStreamImpl) b);
} else {
return new TGenericConcatIntStream(a, b);
}
}
}

View File

@ -0,0 +1,159 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.OptionalDouble;
import java.util.OptionalLong;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.LongBinaryOperator;
import java.util.function.LongConsumer;
import java.util.function.LongFunction;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.longimpl.TArrayLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TEmptyLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TGenerateLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TGenericConcatLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TIterateLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TLongStreamBuilder;
import org.teavm.classlib.java.util.stream.longimpl.TRangeLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TSingleLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TSpecializedConcatLongStream;
public interface TLongStream extends TBaseStream<Long, TLongStream> {
interface Builder {
void accept(long t);
default Builder add(long t) {
accept(t);
return this;
}
TLongStream build();
}
TLongStream filter(LongPredicate predicate);
TLongStream map(LongUnaryOperator mapper);
<U> TStream<U> mapToObj(LongFunction<? extends U> mapper);
TIntStream mapToInt(LongToIntFunction mapper);
TDoubleStream mapToDouble(LongToDoubleFunction mapper);
TLongStream flatMap(LongFunction<? extends TLongStream> mapper);
TLongStream distinct();
TLongStream sorted();
TLongStream peek(LongConsumer action);
TLongStream limit(long maxSize);
TLongStream skip(long n);
void forEach(LongConsumer action);
void forEachOrdered(LongConsumer action);
long[] toArray();
long reduce(long identity, LongBinaryOperator accumulator);
OptionalLong reduce(LongBinaryOperator op);
<R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner);
long sum();
OptionalLong min();
OptionalLong max();
long count();
OptionalDouble average();
boolean anyMatch(LongPredicate predicate);
boolean allMatch(LongPredicate predicate);
boolean noneMatch(LongPredicate predicate);
OptionalLong findFirst();
OptionalLong findAny();
TDoubleStream asDoubleStream();
TStream<Long> boxed();
@Override
PrimitiveIterator.OfLong iterator();
@Override
Spliterator.OfLong spliterator();
static Builder builder() {
return new TLongStreamBuilder();
}
static TLongStream empty() {
return new TEmptyLongStreamImpl();
}
static TLongStream of(long t) {
return new TSingleLongStreamImpl(t);
}
static TLongStream of(long... values) {
return new TArrayLongStreamImpl(values, 0, values.length);
}
static TLongStream iterate(long seed, LongUnaryOperator f) {
return new TIterateLongStream(seed, f);
}
static TLongStream generate(LongSupplier s) {
return new TGenerateLongStream(s);
}
static TLongStream range(long startInclusive, long endExclusive) {
return new TRangeLongStream(startInclusive, endExclusive);
}
static TLongStream rangeClosed(long startInclusive, long endInclusive) {
return new TRangeLongStream(startInclusive, endInclusive + 1);
}
static TLongStream concat(TLongStream a, TLongStream b) {
if (a instanceof TSimpleLongStreamImpl && b instanceof TSimpleLongStreamImpl) {
return new TSpecializedConcatLongStream((TSimpleLongStreamImpl) a, (TSimpleLongStreamImpl) b);
} else {
return new TGenericConcatLongStream(a, b);
}
}
}

View File

@ -0,0 +1,151 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
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;
import java.util.function.UnaryOperator;
import org.teavm.classlib.java.util.stream.impl.TArrayStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TEmptyStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TGenerateStream;
import org.teavm.classlib.java.util.stream.impl.TGenericConcatStream;
import org.teavm.classlib.java.util.stream.impl.TIterateStream;
import org.teavm.classlib.java.util.stream.impl.TSimpleStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TSingleStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TSpecializedConcatStream;
import org.teavm.classlib.java.util.stream.impl.TStreamBuilder;
public interface TStream<T> extends TBaseStream<T, TStream<T>> {
interface Builder<T> {
void accept(T t);
default Builder<T> add(T t) {
accept(t);
return this;
}
TStream<T> build();
}
TStream<T> filter(Predicate<? super T> predicate);
<R> TStream<R> map(Function<? super T, ? extends R> mapper);
TIntStream mapToInt(ToIntFunction<? super T> mapper);
TLongStream mapToLong(ToLongFunction<? super T> mapper);
TDoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
<R> TStream<R> flatMap(Function<? super T, ? extends TStream<? extends R>> mapper);
TIntStream flatMapToInt(Function<? super T, ? extends TIntStream> mapper);
TLongStream flatMapToLong(Function<? super T, ? extends TLongStream> mapper);
TDoubleStream flatMapToDouble(Function<? super T, ? extends TDoubleStream> mapper);
TStream<T> distinct();
TStream<T> sorted();
TStream<T> sorted(Comparator<? super T> comparator);
TStream<T> peek(Consumer<? super T> action);
TStream<T> limit(long maxSize);
TStream<T> skip(long n);
void forEach(Consumer<? super T> action);
void forEachOrdered(Consumer<? super T> action);
Object[] toArray();
<A> A[] toArray(IntFunction<A[]> generator);
T reduce(T identity, BinaryOperator<T> accumulator);
Optional<T> reduce(BinaryOperator<T> accumulator);
<U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner);
<R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner);
<R, A> R collect(TCollector<? super T, A, R> collector);
Optional<T> min(Comparator<? super T> comparator);
Optional<T> max(Comparator<? super T> comparator);
long count();
boolean anyMatch(Predicate<? super T> predicate);
boolean allMatch(Predicate<? super T> predicate);
boolean noneMatch(Predicate<? super T> predicate);
Optional<T> findFirst();
Optional<T> findAny();
static <T> TStream.Builder<T> builder() {
return new TStreamBuilder<>();
}
static <T> TStream<T> empty() {
return new TEmptyStreamImpl<>();
}
static <T> TStream<T> of(T t) {
return new TSingleStreamImpl<>(t);
}
@SafeVarargs
static <T> TStream<T> of(T... values) {
return new TArrayStreamImpl<>(values, 0, values.length);
}
static <T> TStream<T> iterate(T seed, UnaryOperator<T> f) {
return new TIterateStream<>(seed, f);
}
static <T> TStream<T> generate(Supplier<T> s) {
return new TGenerateStream<>(s);
}
@SuppressWarnings("unchecked")
static <T> TStream<T> concat(TStream<? extends T> a, TStream<? extends T> b) {
if (a instanceof TSimpleStreamImpl && b instanceof TSimpleStreamImpl) {
return new TSpecializedConcatStream<>((TSimpleStreamImpl<T>) a, (TSimpleStreamImpl<T>) b);
} else {
return new TGenericConcatStream<>(a, b);
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream;
import java.util.Spliterator;
import org.teavm.classlib.java.util.stream.impl.TStreamOverSpliterator;
public final class TStreamSupport {
private TStreamSupport() {
}
public static <T> TStream<T> stream(Spliterator<T> spliterator, boolean parallel) {
return new TStreamOverSpliterator<>(spliterator);
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TArrayDoubleStreamImpl extends TSimpleDoubleStreamImpl {
private double[] array;
private int index;
private int end;
private int size;
public TArrayDoubleStreamImpl(double[] array, int start, int end) {
this.array = array;
index = start;
this.end = end;
size = end - start;
}
@Override
public boolean next(DoublePredicate consumer) {
while (index < end) {
if (!consumer.test(array[index++])) {
break;
}
}
return index < end;
}
@Override
protected int estimateSize() {
return size;
}
@Override
public long count() {
return size;
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TAverageDoubleConsumer implements DoublePredicate {
double sum;
int count;
@Override
public boolean test(double t) {
sum += t;
count++;
return true;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.Predicate;
import org.teavm.classlib.java.util.stream.impl.TSimpleStreamImpl;
public class TBoxedDoubleStream extends TSimpleStreamImpl<Double> {
private TSimpleDoubleStreamImpl source;
public TBoxedDoubleStream(TSimpleDoubleStreamImpl source) {
this.source = source;
}
@Override
public boolean next(Predicate<? super Double> consumer) {
return source.next(consumer::test);
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TCloseHandlingDoubleStream extends TSimpleDoubleStreamImpl {
private TSimpleDoubleStreamImpl innerStream;
private Runnable closeHandler;
public TCloseHandlingDoubleStream(TSimpleDoubleStreamImpl innerStream, Runnable closeHandler) {
this.innerStream = innerStream;
this.closeHandler = closeHandler;
}
@Override
public boolean next(DoublePredicate consumer) {
return innerStream.next(consumer);
}
@Override
public void close() throws Exception {
RuntimeException previousException = null;
try {
closeHandler.run();
} catch (RuntimeException e) {
previousException = e;
}
try {
innerStream.close();
} catch (RuntimeException e) {
if (previousException != null) {
e.addSuppressed(previousException);
}
throw e;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TCountingDoubleConsumer implements DoublePredicate {
int count;
@Override
public boolean test(double t) {
count++;
return true;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.HashSet;
import java.util.Set;
import java.util.function.DoublePredicate;
public class TDistinctDoubleStreamImpl extends TWrappingDoubleStreamImpl {
public TDistinctDoubleStreamImpl(TSimpleDoubleStreamImpl innerStream) {
super(innerStream);
}
@Override
protected DoublePredicate wrap(DoublePredicate consumer) {
Set<Double> visited = new HashSet<>();
return e -> {
if (!visited.add(e)) {
return true;
}
return consumer.test(e);
};
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.Arrays;
import org.teavm.classlib.java.util.stream.TDoubleStream;
public class TDoubleStreamBuilder implements TDoubleStream.Builder {
private double[] elements = new double[4];
private int size;
@Override
public void accept(double t) {
if (size == elements.length) {
elements = Arrays.copyOf(elements, elements.length * 2);
}
elements[size++] = t;
}
@Override
public TDoubleStream build() {
return new TArrayDoubleStreamImpl(elements, 0, size);
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TEmptyDoubleStreamImpl extends TSimpleDoubleStreamImpl {
@Override
public boolean next(DoublePredicate consumer) {
return false;
}
@Override
protected int estimateSize() {
return 0;
}
@Override
public long count() {
return 1;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TFilteringDoubleStreamImpl extends TWrappingDoubleStreamImpl {
private DoublePredicate filter;
public TFilteringDoubleStreamImpl(TSimpleDoubleStreamImpl innerStream, DoublePredicate filter) {
super(innerStream);
this.filter = filter;
}
@Override
protected DoublePredicate wrap(DoublePredicate consumer) {
return t -> {
if (!filter.test(t)) {
return true;
}
return consumer.test(t);
};
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TFindFirstDoubleConsumer implements DoublePredicate {
public double result;
boolean hasAny;
@Override
public boolean test(double t) {
hasAny = true;
result = t;
return false;
}
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.PrimitiveIterator;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import org.teavm.classlib.java.util.stream.TDoubleStream;
public class TFlatMappingDoubleStreamImpl extends TSimpleDoubleStreamImpl {
private TSimpleDoubleStreamImpl sourceStream;
private TDoubleStream current;
private PrimitiveIterator.OfDouble iterator;
private DoubleFunction<? extends TDoubleStream> mapper;
private boolean done;
public TFlatMappingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream,
DoubleFunction<? extends TDoubleStream> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(DoublePredicate consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleDoubleStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleDoubleStreamImpl castCurrent = (TSimpleDoubleStreamImpl) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
double e = iterator.nextDouble();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
public class TGenerateDoubleStream extends TSimpleDoubleStreamImpl {
private DoubleSupplier s;
public TGenerateDoubleStream(DoubleSupplier s) {
this.s = s;
}
@Override
public boolean next(DoublePredicate consumer) {
while (consumer.test(s.getAsDouble())) {
// go on
}
return true;
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.PrimitiveIterator;
import java.util.function.DoublePredicate;
import org.teavm.classlib.java.util.stream.TDoubleStream;
public class TGenericConcatDoubleStream extends TSimpleDoubleStreamImpl {
TDoubleStream first;
TDoubleStream second;
PrimitiveIterator.OfDouble iterator;
boolean isSecond;
public TGenericConcatDoubleStream(TDoubleStream first, TDoubleStream second) {
this.first = first;
this.second = second;
}
@Override
public boolean next(DoublePredicate consumer) {
if (iterator == null) {
iterator = first.iterator();
}
while (true) {
while (iterator.hasNext()) {
if (!consumer.test(iterator.nextDouble())) {
return true;
}
}
if (!isSecond) {
isSecond = true;
iterator = second.iterator();
} else {
return false;
}
}
}
@Override
public long count() {
return first.count() + second.count();
}
@Override
public void close() throws Exception {
Exception suppressed = null;
try {
first.close();
} catch (Exception e) {
suppressed = e;
}
try {
second.close();
} catch (Exception e) {
if (suppressed != null) {
e.addSuppressed(suppressed);
}
throw e;
}
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
import java.util.function.DoubleUnaryOperator;
public class TIterateDoubleStream extends TSimpleDoubleStreamImpl {
private double value;
private DoubleUnaryOperator f;
public TIterateDoubleStream(double value, DoubleUnaryOperator f) {
this.value = value;
this.f = f;
}
@Override
public boolean next(DoublePredicate consumer) {
while (true) {
double valueToReport = value;
value = f.applyAsDouble(value);
if (!consumer.test(valueToReport)) {
return true;
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TLimitingDoubleStreamImpl extends TSimpleDoubleStreamImpl {
private TSimpleDoubleStreamImpl sourceStream;
private int limit;
private int remaining;
public TLimitingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream, int limit) {
this.sourceStream = sourceStream;
this.limit = limit;
remaining = limit;
}
@Override
public boolean next(DoublePredicate consumer) {
if (remaining == 0) {
return false;
}
boolean result = sourceStream.next(e -> {
if (remaining-- == 0) {
return false;
}
return consumer.test(e);
});
if (!result) {
remaining = 0;
}
return remaining > 0;
}
@Override
protected int estimateSize() {
int sourceEstimation = sourceStream.estimateSize();
return sourceEstimation < 0 ? limit : Math.min(limit, sourceEstimation);
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
import java.util.function.DoubleUnaryOperator;
public class TMappingDoubleStreamImpl extends TWrappingDoubleStreamImpl {
private DoubleUnaryOperator mapper;
public TMappingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream, DoubleUnaryOperator mapper) {
super(sourceStream);
this.mapper = mapper;
}
@Override
protected DoublePredicate wrap(DoublePredicate consumer) {
return t -> consumer.test(mapper.applyAsDouble(t));
}
@Override
public long count() {
return sourceStream.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoubleToIntFunction;
import java.util.function.IntPredicate;
import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl;
public class TMappingToIntStreamImpl extends TSimpleIntStreamImpl {
private TSimpleDoubleStreamImpl source;
private DoubleToIntFunction mapper;
public TMappingToIntStreamImpl(TSimpleDoubleStreamImpl source, DoubleToIntFunction mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(IntPredicate consumer) {
return source.next(e -> consumer.test(mapper.applyAsInt(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoubleToLongFunction;
import java.util.function.LongPredicate;
import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl;
public class TMappingToLongStreamImpl extends TSimpleLongStreamImpl {
private TSimpleDoubleStreamImpl source;
private DoubleToLongFunction mapper;
public TMappingToLongStreamImpl(TSimpleDoubleStreamImpl source, DoubleToLongFunction mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(LongPredicate consumer) {
return source.next(e -> consumer.test(mapper.applyAsLong(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoubleFunction;
import java.util.function.Predicate;
import org.teavm.classlib.java.util.stream.impl.TSimpleStreamImpl;
public class TMappingToObjStreamImpl<T> extends TSimpleStreamImpl<T> {
private TSimpleDoubleStreamImpl source;
private DoubleFunction<? extends T> mapper;
public TMappingToObjStreamImpl(TSimpleDoubleStreamImpl source, DoubleFunction<? extends T> mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(Predicate<? super T> consumer) {
return source.next(e -> consumer.test(mapper.apply(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoubleConsumer;
import java.util.function.DoublePredicate;
public class TPeekingDoubleStreamImpl extends TWrappingDoubleStreamImpl {
private DoubleConsumer elementConsumer;
public TPeekingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream, DoubleConsumer elementConsumer) {
super(sourceStream);
this.elementConsumer = elementConsumer;
}
@Override
protected DoublePredicate wrap(DoublePredicate consumer) {
return e -> {
elementConsumer.accept(e);
return consumer.test(e);
};
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoublePredicate;
class TReducingDoubleConsumer implements DoublePredicate {
private DoubleBinaryOperator accumulator;
double result;
boolean initialized;
TReducingDoubleConsumer(DoubleBinaryOperator accumulator, double result, boolean initialized) {
this.accumulator = accumulator;
this.result = result;
this.initialized = initialized;
}
@Override
public boolean test(double t) {
if (!initialized) {
result = t;
initialized = true;
} else {
result = accumulator.applyAsDouble(result, t);
}
return true;
}
}

View File

@ -0,0 +1,281 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.ObjDoubleConsumer;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.TDoubleStream;
import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.TLongStream;
import org.teavm.classlib.java.util.stream.TStream;
public abstract class TSimpleDoubleStreamImpl implements TDoubleStream {
@Override
public TDoubleStream filter(DoublePredicate predicate) {
return new TFilteringDoubleStreamImpl(this, predicate);
}
@Override
public TDoubleStream map(DoubleUnaryOperator mapper) {
return new TMappingDoubleStreamImpl(this, mapper);
}
@Override
public <U> TStream<U> mapToObj(DoubleFunction<? extends U> mapper) {
return new TMappingToObjStreamImpl<>(this, mapper);
}
@Override
public TIntStream mapToInt(DoubleToIntFunction mapper) {
return new TMappingToIntStreamImpl(this, mapper);
}
@Override
public TLongStream mapToLong(DoubleToLongFunction mapper) {
return new TMappingToLongStreamImpl(this, mapper);
}
@Override
public TDoubleStream flatMap(DoubleFunction<? extends TDoubleStream> mapper) {
return new TFlatMappingDoubleStreamImpl(this, mapper);
}
@Override
public TDoubleStream distinct() {
return new TDistinctDoubleStreamImpl(this);
}
@Override
public TDoubleStream sorted() {
double[] array = toArray();
Arrays.sort(array);
return TDoubleStream.of(array);
}
@Override
public TDoubleStream peek(DoubleConsumer action) {
return new TPeekingDoubleStreamImpl(this, action);
}
@Override
public TDoubleStream limit(long maxSize) {
return new TLimitingDoubleStreamImpl(this, (int) maxSize);
}
@Override
public TDoubleStream skip(long n) {
return new TSkippingDoubleStreamImpl(this, (int) n);
}
@Override
public void forEach(DoubleConsumer action) {
forEachOrdered(action);
}
@Override
public void forEachOrdered(DoubleConsumer action) {
next(e -> {
action.accept(e);
return true;
});
}
@Override
public double[] toArray() {
int estimatedSize = estimateSize();
if (estimatedSize < 0) {
List<Double> list = new ArrayList<>();
next(list::add);
double[] array = new double[list.size()];
for (int i = 0; i < array.length; ++i) {
array[i] = list.get(i);
}
return array;
} else {
double[] array = new double[estimatedSize];
ArrayFillingConsumer consumer = new ArrayFillingConsumer(array);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have reported done status";
if (consumer.index < array.length) {
array = Arrays.copyOf(array, consumer.index);
}
return array;
}
}
@Override
public double reduce(double identity, DoubleBinaryOperator accumulator) {
TReducingDoubleConsumer consumer = new TReducingDoubleConsumer(accumulator, identity, true);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have returned true";
return consumer.result;
}
@Override
public OptionalDouble reduce(DoubleBinaryOperator accumulator) {
TReducingDoubleConsumer consumer = new TReducingDoubleConsumer(accumulator, 0, false);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have returned true";
return consumer.initialized ? OptionalDouble.of(consumer.result) : OptionalDouble.empty();
}
@Override
public <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) {
R collection = supplier.get();
next(e -> {
accumulator.accept(collection, e);
return true;
});
return collection;
}
@Override
public OptionalDouble min() {
return reduce(Math::min);
}
@Override
public OptionalDouble max() {
return reduce(Math::max);
}
@Override
public long count() {
TCountingDoubleConsumer consumer = new TCountingDoubleConsumer();
next(consumer);
return consumer.count;
}
@Override
public double sum() {
TSumDoubleConsumer consumer = new TSumDoubleConsumer();
next(consumer);
return consumer.sum;
}
@Override
public OptionalDouble average() {
TAverageDoubleConsumer consumer = new TAverageDoubleConsumer();
next(consumer);
return consumer.count > 0 ? OptionalDouble.of(consumer.sum / consumer.count) : OptionalDouble.empty();
}
@Override
public boolean anyMatch(DoublePredicate predicate) {
return next(predicate.negate());
}
@Override
public boolean allMatch(DoublePredicate predicate) {
return !next(predicate);
}
@Override
public boolean noneMatch(DoublePredicate predicate) {
return !anyMatch(predicate);
}
@Override
public OptionalDouble findFirst() {
TFindFirstDoubleConsumer consumer = new TFindFirstDoubleConsumer();
next(consumer);
return consumer.hasAny ? OptionalDouble.of(consumer.result) : OptionalDouble.empty();
}
@Override
public OptionalDouble findAny() {
return findFirst();
}
@Override
public PrimitiveIterator.OfDouble iterator() {
return new TSimpleDoubleStreamIterator(this);
}
@Override
public Spliterator.OfDouble spliterator() {
return new TSimpleDoubleStreamSpliterator(this);
}
@Override
public TStream<Double> boxed() {
return new TBoxedDoubleStream(this);
}
@Override
public boolean isParallel() {
return false;
}
@Override
public TDoubleStream sequential() {
return this;
}
@Override
public TDoubleStream parallel() {
return this;
}
@Override
public TDoubleStream unordered() {
return this;
}
@Override
public TDoubleStream onClose(Runnable closeHandler) {
return new TCloseHandlingDoubleStream(this, closeHandler);
}
@Override
public void close() throws Exception {
}
protected int estimateSize() {
return -1;
}
public abstract boolean next(DoublePredicate consumer);
class ArrayFillingConsumer implements DoublePredicate {
double[] array;
int index;
ArrayFillingConsumer(double[] array) {
this.array = array;
}
@Override
public boolean test(double t) {
array[index++] = t;
return true;
}
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;
public class TSimpleDoubleStreamIterator implements PrimitiveIterator.OfDouble {
private static final byte NEEDS_MORE = 0;
private static final byte HAS_DATA = 1;
private static final byte LAST_ELEMENT = 2;
private static final byte DONE = 3;
private TSimpleDoubleStreamImpl stream;
private double lastElement;
private byte state;
public TSimpleDoubleStreamIterator(TSimpleDoubleStreamImpl stream) {
this.stream = stream;
}
@Override
public boolean hasNext() {
fetchIfNeeded();
return state != DONE;
}
@Override
public double nextDouble() {
fetchIfNeeded();
if (state == DONE) {
throw new NoSuchElementException();
}
double result = lastElement;
state = state == LAST_ELEMENT ? DONE : NEEDS_MORE;
return result;
}
private void fetchIfNeeded() {
if (state != NEEDS_MORE) {
return;
}
boolean hasMore = stream.next(e -> {
lastElement = e;
return false;
});
state = hasMore ? HAS_DATA : LAST_ELEMENT;
if (state == LAST_ELEMENT) {
stream = null;
}
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.Spliterator;
import java.util.function.DoubleConsumer;
public class TSimpleDoubleStreamSpliterator implements Spliterator.OfDouble {
private TSimpleDoubleStreamImpl stream;
private boolean done;
public TSimpleDoubleStreamSpliterator(TSimpleDoubleStreamImpl stream) {
this.stream = stream;
}
@Override
public void forEachRemaining(DoubleConsumer action) {
stream.next(x -> {
action.accept(x);
return true;
});
}
@Override
public boolean tryAdvance(DoubleConsumer action) {
if (done) {
return false;
}
done = !stream.next(x -> {
action.accept(x);
return false;
});
return true;
}
@Override
public OfDouble trySplit() {
return null;
}
@Override
public long estimateSize() {
return stream.estimateSize();
}
@Override
public int characteristics() {
return 0;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TSingleDoubleStreamImpl extends TSimpleDoubleStreamImpl {
private double element;
public TSingleDoubleStreamImpl(double element) {
this.element = element;
}
@Override
public boolean next(DoublePredicate consumer) {
consumer.test(element);
return false;
}
@Override
protected int estimateSize() {
return 1;
}
@Override
public long count() {
return 1;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TSkippingDoubleStreamImpl extends TSimpleDoubleStreamImpl {
private TSimpleDoubleStreamImpl sourceStream;
private int skip;
private int remaining;
public TSkippingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream, int skip) {
this.sourceStream = sourceStream;
this.skip = skip;
remaining = skip;
}
@Override
public boolean next(DoublePredicate consumer) {
if (remaining > 0) {
if (!sourceStream.next(e -> --remaining > 0)) {
return false;
}
}
return sourceStream.next(consumer);
}
@Override
protected int estimateSize() {
int sourceSize = sourceStream.estimateSize();
return sourceSize >= 0 ? Math.max(0, sourceSize - skip) : -1;
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,78 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TSpecializedConcatDoubleStream extends TSimpleDoubleStreamImpl {
TSimpleDoubleStreamImpl first;
TSimpleDoubleStreamImpl second;
TSimpleDoubleStreamImpl current;
public TSpecializedConcatDoubleStream(TSimpleDoubleStreamImpl first, TSimpleDoubleStreamImpl second) {
this.first = first;
this.second = second;
current = first;
}
@Override
public boolean next(DoublePredicate consumer) {
if (current == null) {
return false;
}
while (true) {
if (current.next(consumer)) {
return true;
}
if (current == first) {
current = second;
} else {
current = null;
return false;
}
}
}
@Override
protected int estimateSize() {
int firstSize = first.estimateSize();
int secondSize = second.estimateSize();
return firstSize >= 0 && secondSize >= 0 ? firstSize + secondSize : -1;
}
@Override
public long count() {
return first.count() + second.count();
}
@Override
public void close() throws Exception {
RuntimeException suppressed = null;
try {
first.close();
} catch (RuntimeException e) {
suppressed = e;
}
try {
second.close();
} catch (RuntimeException e) {
if (suppressed != null) {
e.addSuppressed(suppressed);
}
throw e;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public class TSumDoubleConsumer implements DoublePredicate {
double sum;
@Override
public boolean test(double t) {
sum += t;
return true;
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate;
public abstract class TWrappingDoubleStreamImpl extends TSimpleDoubleStreamImpl {
TSimpleDoubleStreamImpl sourceStream;
public TWrappingDoubleStreamImpl(TSimpleDoubleStreamImpl sourceStream) {
this.sourceStream = sourceStream;
}
@Override
public boolean next(DoublePredicate consumer) {
return sourceStream.next(wrap(consumer));
}
protected abstract DoublePredicate wrap(DoublePredicate consumer);
@Override
protected int estimateSize() {
return sourceStream.estimateSize();
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TArrayStreamImpl<T> extends TSimpleStreamImpl<T> {
private T[] array;
private int index;
private int end;
private int size;
public TArrayStreamImpl(T[] array, int start, int end) {
this.array = array;
index = start;
this.end = end;
size = end - start;
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (index < end) {
if (!consumer.test(array[index++])) {
break;
}
}
return index < end;
}
@Override
protected int estimateSize() {
return size;
}
@Override
public long count() {
return size;
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TCloseHandlingStream<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> innerStream;
private Runnable closeHandler;
public TCloseHandlingStream(TSimpleStreamImpl<T> innerStream, Runnable closeHandler) {
this.innerStream = innerStream;
this.closeHandler = closeHandler;
}
@Override
public boolean next(Predicate<? super T> consumer) {
return innerStream.next(consumer);
}
@Override
public void close() throws Exception {
RuntimeException previousException = null;
try {
closeHandler.run();
} catch (RuntimeException e) {
previousException = e;
}
try {
innerStream.close();
} catch (RuntimeException e) {
if (previousException != null) {
e.addSuppressed(previousException);
}
throw e;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TCountingConsumer<T> implements Predicate<T> {
int count;
@Override
public boolean test(T t) {
count++;
return true;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
public class TDistinctStreamImpl<T> extends TWrappingStreamImpl<T, T> {
public TDistinctStreamImpl(TSimpleStreamImpl<T> innerStream) {
super(innerStream);
}
@Override
protected Predicate<T> wrap(Predicate<? super T> consumer) {
Set<T> visited = new HashSet<>();
return e -> {
if (!visited.add(e)) {
return true;
}
return consumer.test(e);
};
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TEmptyStreamImpl<T> extends TSimpleStreamImpl<T> {
@Override
public boolean next(Predicate<? super T> consumer) {
return false;
}
@Override
protected int estimateSize() {
return 0;
}
@Override
public long count() {
return 1;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TFilteringStreamImpl<T> extends TWrappingStreamImpl<T, T> {
private Predicate<? super T> filter;
public TFilteringStreamImpl(TSimpleStreamImpl<T> innerStream, Predicate<? super T> filter) {
super(innerStream);
this.filter = filter;
}
@Override
protected Predicate<T> wrap(Predicate<? super T> consumer) {
return t -> {
if (!filter.test(t)) {
return true;
}
return consumer.test(t);
};
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TFindFirstConsumer<T> implements Predicate<T> {
public T result;
@Override
public boolean test(T t) {
result = t;
return false;
}
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Predicate;
import org.teavm.classlib.java.util.stream.TStream;
public class TFlatMappingStreamImpl<T, S> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<S> sourceStream;
private TStream<? extends T> current;
private Iterator<? extends T> iterator;
private Function<? super S, ? extends TStream<? extends T>> mapper;
private boolean done;
public TFlatMappingStreamImpl(TSimpleStreamImpl<S> sourceStream,
Function<? super S, ? extends TStream<? extends T>> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleStreamImpl<? extends T> castCurrent = (TSimpleStreamImpl<? extends T>) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
T e = iterator.next();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.PrimitiveIterator;
import java.util.function.DoublePredicate;
import java.util.function.Function;
import org.teavm.classlib.java.util.stream.TDoubleStream;
import org.teavm.classlib.java.util.stream.doubleimpl.TSimpleDoubleStreamImpl;
public class TFlatMappingToDoubleStreamImpl<T> extends TSimpleDoubleStreamImpl {
private TSimpleStreamImpl<T> sourceStream;
private TDoubleStream current;
private PrimitiveIterator.OfDouble iterator;
private Function<? super T, ? extends TDoubleStream> mapper;
private boolean done;
public TFlatMappingToDoubleStreamImpl(TSimpleStreamImpl<T> sourceStream,
Function<? super T, ? extends TDoubleStream> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(DoublePredicate consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleDoubleStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleDoubleStreamImpl castCurrent = (TSimpleDoubleStreamImpl) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
double e = iterator.nextDouble();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.PrimitiveIterator;
import java.util.function.Function;
import java.util.function.IntPredicate;
import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl;
public class TFlatMappingToIntStreamImpl<T> extends TSimpleIntStreamImpl {
private TSimpleStreamImpl<T> sourceStream;
private TIntStream current;
private PrimitiveIterator.OfInt iterator;
private Function<? super T, ? extends TIntStream> mapper;
private boolean done;
public TFlatMappingToIntStreamImpl(TSimpleStreamImpl<T> sourceStream,
Function<? super T, ? extends TIntStream> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(IntPredicate consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleIntStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleIntStreamImpl castCurrent = (TSimpleIntStreamImpl) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
int e = iterator.nextInt();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.PrimitiveIterator;
import java.util.function.Function;
import java.util.function.LongPredicate;
import org.teavm.classlib.java.util.stream.TLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl;
public class TFlatMappingToLongStreamImpl<T> extends TSimpleLongStreamImpl {
private TSimpleStreamImpl<T> sourceStream;
private TLongStream current;
private PrimitiveIterator.OfLong iterator;
private Function<? super T, ? extends TLongStream> mapper;
private boolean done;
public TFlatMappingToLongStreamImpl(TSimpleStreamImpl<T> sourceStream,
Function<? super T, ? extends TLongStream> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(LongPredicate consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleLongStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleLongStreamImpl castCurrent = (TSimpleLongStreamImpl) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
long e = iterator.nextLong();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class TGenerateStream<T> extends TSimpleStreamImpl<T> {
private Supplier<T> s;
public TGenerateStream(Supplier<T> s) {
this.s = s;
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (consumer.test(s.get())) {
// go on
}
return true;
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Iterator;
import java.util.function.Predicate;
import org.teavm.classlib.java.util.stream.TStream;
public class TGenericConcatStream<T> extends TSimpleStreamImpl<T> {
TStream<? extends T> first;
TStream<? extends T> second;
Iterator<? extends T> iterator;
boolean isSecond;
public TGenericConcatStream(TStream<? extends T> first, TStream<? extends T> second) {
this.first = first;
this.second = second;
}
@Override
public boolean next(Predicate<? super T> consumer) {
if (iterator == null) {
iterator = first.iterator();
}
while (true) {
while (iterator.hasNext()) {
if (!consumer.test(iterator.next())) {
return true;
}
}
if (!isSecond) {
isSecond = true;
iterator = second.iterator();
} else {
return false;
}
}
}
@Override
public long count() {
return first.count() + second.count();
}
@Override
public void close() throws Exception {
Exception suppressed = null;
try {
first.close();
} catch (Exception e) {
suppressed = e;
}
try {
second.close();
} catch (Exception e) {
if (suppressed != null) {
e.addSuppressed(suppressed);
}
throw e;
}
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
public class TIterateStream<T> extends TSimpleStreamImpl<T> {
private T value;
private UnaryOperator<T> f;
public TIterateStream(T value, UnaryOperator<T> f) {
this.value = value;
this.f = f;
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (true) {
T valueToReport = value;
value = f.apply(value);
if (!consumer.test(valueToReport)) {
return true;
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TLimitingStreamImpl<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> sourceStream;
private int limit;
private int remaining;
public TLimitingStreamImpl(TSimpleStreamImpl<T> sourceStream, int limit) {
this.sourceStream = sourceStream;
this.limit = limit;
remaining = limit;
}
@Override
public boolean next(Predicate<? super T> consumer) {
if (remaining == 0) {
return false;
}
boolean result = sourceStream.next(e -> {
if (remaining-- == 0) {
return false;
}
return consumer.test(e);
});
if (!result) {
remaining = 0;
}
return remaining > 0;
}
@Override
protected int estimateSize() {
int sourceEstimation = sourceStream.estimateSize();
return sourceEstimation < 0 ? limit : Math.min(limit, sourceEstimation);
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Function;
import java.util.function.Predicate;
public class TMappingStreamImpl<T, S> extends TWrappingStreamImpl<T, S> {
private Function<? super S, ? extends T> mapper;
public TMappingStreamImpl(TSimpleStreamImpl<S> sourceStream, Function<? super S, ? extends T> mapper) {
super(sourceStream);
this.mapper = mapper;
}
@Override
protected Predicate<S> wrap(Predicate<? super T> consumer) {
return t -> consumer.test(mapper.apply(t));
}
@Override
public long count() {
return sourceStream.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.DoublePredicate;
import java.util.function.ToDoubleFunction;
import org.teavm.classlib.java.util.stream.doubleimpl.TSimpleDoubleStreamImpl;
public class TMappingToDoubleStreamImpl<T> extends TSimpleDoubleStreamImpl {
private TSimpleStreamImpl<T> source;
private ToDoubleFunction<? super T> mapper;
public TMappingToDoubleStreamImpl(TSimpleStreamImpl<T> source, ToDoubleFunction<? super T> mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(DoublePredicate consumer) {
return source.next(e -> consumer.test(mapper.applyAsDouble(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.IntPredicate;
import java.util.function.ToIntFunction;
import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl;
public class TMappingToIntStreamImpl<T> extends TSimpleIntStreamImpl {
private TSimpleStreamImpl<T> source;
private ToIntFunction<? super T> mapper;
public TMappingToIntStreamImpl(TSimpleStreamImpl<T> source, ToIntFunction<? super T> mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(IntPredicate consumer) {
return source.next(e -> consumer.test(mapper.applyAsInt(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.LongPredicate;
import java.util.function.ToLongFunction;
import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl;
public class TMappingToLongStreamImpl<T> extends TSimpleLongStreamImpl {
private TSimpleStreamImpl<T> source;
private ToLongFunction<? super T> mapper;
public TMappingToLongStreamImpl(TSimpleStreamImpl<T> source, ToLongFunction<? super T> mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
public boolean next(LongPredicate consumer) {
return source.next(e -> consumer.test(mapper.applyAsLong(e)));
}
@Override
public void close() throws Exception {
source.close();
}
@Override
protected int estimateSize() {
return source.estimateSize();
}
@Override
public long count() {
return source.count();
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class TPeekingStreamImpl<T> extends TWrappingStreamImpl<T, T> {
private Consumer<? super T> elementConsumer;
public TPeekingStreamImpl(TSimpleStreamImpl<T> sourceStream, Consumer<? super T> elementConsumer) {
super(sourceStream);
this.elementConsumer = elementConsumer;
}
@Override
protected Predicate<T> wrap(Predicate<? super T> consumer) {
return e -> {
elementConsumer.accept(e);
return consumer.test(e);
};
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
class TReducingConsumer<T> implements Predicate<T> {
private BinaryOperator<T> accumulator;
T result;
boolean initialized;
TReducingConsumer(BinaryOperator<T> accumulator, T result, boolean initialized) {
this.accumulator = accumulator;
this.result = result;
this.initialized = initialized;
}
@Override
public boolean test(T t) {
if (!initialized) {
result = t;
initialized = true;
} else {
result = accumulator.apply(result, t);
}
return true;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.BiFunction;
import java.util.function.Predicate;
class TReducingConsumer2<T, U> implements Predicate<T> {
private BiFunction<U, ? super T, U> accumulator;
U result;
TReducingConsumer2(BiFunction<U, ? super T, U> accumulator, U result) {
this.accumulator = accumulator;
this.result = result;
}
@Override
public boolean test(T t) {
result = accumulator.apply(result, t);
return true;
}
}

View File

@ -0,0 +1,310 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
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;
import org.teavm.classlib.java.util.stream.TCollector;
import org.teavm.classlib.java.util.stream.TDoubleStream;
import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.TLongStream;
import org.teavm.classlib.java.util.stream.TStream;
public abstract class TSimpleStreamImpl<T> implements TStream<T> {
@Override
public TStream<T> filter(Predicate<? super T> predicate) {
return new TFilteringStreamImpl<>(this, predicate);
}
@Override
public <R> TStream<R> map(Function<? super T, ? extends R> mapper) {
return new TMappingStreamImpl<>(this, mapper);
}
@Override
public TIntStream mapToInt(ToIntFunction<? super T> mapper) {
return new TMappingToIntStreamImpl<>(this, mapper);
}
@Override
public TLongStream mapToLong(ToLongFunction<? super T> mapper) {
return new TMappingToLongStreamImpl<>(this, mapper);
}
@Override
public TDoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) {
return new TMappingToDoubleStreamImpl<>(this, mapper);
}
@Override
public <R> TStream<R> flatMap(Function<? super T, ? extends TStream<? extends R>> mapper) {
return new TFlatMappingStreamImpl<>(this, mapper);
}
@Override
public TIntStream flatMapToInt(Function<? super T, ? extends TIntStream> mapper) {
return new TFlatMappingToIntStreamImpl<>(this, mapper);
}
@Override
public TLongStream flatMapToLong(Function<? super T, ? extends TLongStream> mapper) {
return new TFlatMappingToLongStreamImpl<>(this, mapper);
}
@Override
public TDoubleStream flatMapToDouble(Function<? super T, ? extends TDoubleStream> mapper) {
return new TFlatMappingToDoubleStreamImpl<>(this, mapper);
}
@Override
public TStream<T> distinct() {
return new TDistinctStreamImpl<>(this);
}
@Override
@SuppressWarnings("unchecked")
public TStream<T> sorted() {
return new TSortedStreamImpl<>(this, (a, b) -> ((Comparable<Object>) a).compareTo(b));
}
@Override
public TStream<T> sorted(Comparator<? super T> comparator) {
return new TSortedStreamImpl<>(this, comparator);
}
@Override
public TStream<T> peek(Consumer<? super T> action) {
return new TPeekingStreamImpl<>(this, action);
}
@Override
public TStream<T> limit(long maxSize) {
return new TLimitingStreamImpl<>(this, (int) maxSize);
}
@Override
public TStream<T> skip(long n) {
return new TSkippingStreamImpl<>(this, (int) n);
}
@Override
public void forEach(Consumer<? super T> action) {
forEachOrdered(action);
}
@Override
public void forEachOrdered(Consumer<? super T> action) {
next(e -> {
action.accept(e);
return true;
});
}
@Override
public Object[] toArray() {
return toArray(Object[]::new);
}
@Override
@SuppressWarnings("unchecked")
public <A> A[] toArray(IntFunction<A[]> generator) {
int estimatedSize = estimateSize();
if (estimatedSize < 0) {
List<T> list = new ArrayList<>();
next(list::add);
A[] array = generator.apply(list.size());
for (int i = 0; i < array.length; ++i) {
array[i] = (A) list.get(i);
}
return array;
} else {
A[] array = generator.apply(estimatedSize);
ArrayFillingConsumer<A> consumer = new ArrayFillingConsumer<>(array);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have reported done status";
if (consumer.index < array.length) {
array = Arrays.copyOf(array, consumer.index);
}
return array;
}
}
@Override
public T reduce(T identity, BinaryOperator<T> accumulator) {
TReducingConsumer<T> consumer = new TReducingConsumer<>(accumulator, identity, true);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have returned true";
return consumer.result;
}
@Override
public Optional<T> reduce(BinaryOperator<T> accumulator) {
TReducingConsumer<T> consumer = new TReducingConsumer<>(accumulator, null, false);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have returned true";
return Optional.ofNullable(consumer.result);
}
@Override
public <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) {
TReducingConsumer2<T, U> consumer = new TReducingConsumer2<>(accumulator, identity);
boolean wantsMore = next(consumer);
assert !wantsMore : "next() should have returned true";
return consumer.result;
}
@Override
public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) {
R collection = supplier.get();
next(e -> {
accumulator.accept(collection, e);
return true;
});
return collection;
}
@Override
public <R, A> R collect(TCollector<? super T, A, R> collector) {
A collection = collector.supplier().get();
BiConsumer<A, ? super T> accumulator = collector.accumulator();
next(e -> {
accumulator.accept(collection, e);
return true;
});
return collector.finisher().apply(collection);
}
@Override
public Optional<T> min(Comparator<? super T> comparator) {
return reduce((a, b) -> comparator.compare(a, b) < 0 ? a : b);
}
@Override
public Optional<T> max(Comparator<? super T> comparator) {
return reduce((a, b) -> comparator.compare(a, b) > 0 ? a : b);
}
@Override
public long count() {
TCountingConsumer<T> consumer = new TCountingConsumer<>();
next(consumer);
return consumer.count;
}
@Override
public boolean anyMatch(Predicate<? super T> predicate) {
return next(predicate.negate());
}
@Override
public boolean allMatch(Predicate<? super T> predicate) {
return !next(predicate);
}
@Override
public boolean noneMatch(Predicate<? super T> predicate) {
return !anyMatch(predicate);
}
@Override
public Optional<T> findFirst() {
TFindFirstConsumer<T> consumer = new TFindFirstConsumer<>();
next(consumer);
return Optional.ofNullable(consumer.result);
}
@Override
public Optional<T> findAny() {
return findFirst();
}
@Override
public Iterator<T> iterator() {
return new TSimpleStreamIterator<>(this);
}
@Override
public Spliterator<T> spliterator() {
return new TSimpleStreamSpliterator<>(this);
}
@Override
public boolean isParallel() {
return false;
}
@Override
public TStream<T> sequential() {
return this;
}
@Override
public TStream<T> parallel() {
return this;
}
@Override
public TStream<T> unordered() {
return this;
}
@Override
public TStream<T> onClose(Runnable closeHandler) {
return new TCloseHandlingStream<>(this, closeHandler);
}
@Override
public void close() throws Exception {
}
protected int estimateSize() {
return -1;
}
public abstract boolean next(Predicate<? super T> consumer);
class ArrayFillingConsumer<A> implements Predicate<T> {
A[] array;
int index;
ArrayFillingConsumer(A[] array) {
this.array = array;
}
@Override
@SuppressWarnings("unchecked")
public boolean test(T t) {
array[index++] = (A) t;
return true;
}
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
public class TSimpleStreamIterator<T> implements Iterator<T> {
private static final byte NEEDS_MORE = 0;
private static final byte HAS_DATA = 1;
private static final byte LAST_ELEMENT = 2;
private static final byte DONE = 3;
private TSimpleStreamImpl<T> stream;
private T lastElement;
private byte state;
public TSimpleStreamIterator(TSimpleStreamImpl<T> stream) {
this.stream = stream;
}
@Override
public void forEachRemaining(Consumer<? super T> action) {
stream.next(x -> {
action.accept(x);
return true;
});
}
@Override
public boolean hasNext() {
fetchIfNeeded();
return state != DONE;
}
@Override
public T next() {
fetchIfNeeded();
if (state == DONE) {
throw new NoSuchElementException();
}
T result = lastElement;
lastElement = null;
state = state == LAST_ELEMENT ? DONE : NEEDS_MORE;
return result;
}
private void fetchIfNeeded() {
if (state != NEEDS_MORE) {
return;
}
boolean hasMore = stream.next(e -> {
lastElement = e;
return false;
});
state = hasMore ? HAS_DATA : LAST_ELEMENT;
if (state == LAST_ELEMENT) {
stream = null;
}
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Spliterator;
import java.util.function.Consumer;
public class TSimpleStreamSpliterator<T> implements Spliterator<T> {
private TSimpleStreamImpl<T> stream;
private boolean done;
public TSimpleStreamSpliterator(TSimpleStreamImpl<T> stream) {
this.stream = stream;
}
@Override
public void forEachRemaining(Consumer<? super T> action) {
stream.next(x -> {
action.accept(x);
return true;
});
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (done) {
return false;
}
done = !stream.next(x -> {
action.accept(x);
return false;
});
return true;
}
@Override
public Spliterator<T> trySplit() {
return null;
}
@Override
public long estimateSize() {
return stream.estimateSize();
}
@Override
public int characteristics() {
return 0;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TSingleStreamImpl<T> extends TSimpleStreamImpl<T> {
private T element;
public TSingleStreamImpl(T element) {
this.element = element;
}
@Override
public boolean next(Predicate<? super T> consumer) {
consumer.test(element);
return false;
}
@Override
protected int estimateSize() {
return 1;
}
@Override
public long count() {
return 1;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TSkippingStreamImpl<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> sourceStream;
private int skip;
private int remaining;
public TSkippingStreamImpl(TSimpleStreamImpl<T> sourceStream, int skip) {
this.sourceStream = sourceStream;
this.skip = skip;
remaining = skip;
}
@Override
public boolean next(Predicate<? super T> consumer) {
if (remaining > 0) {
if (!sourceStream.next(e -> --remaining > 0)) {
return false;
}
}
return sourceStream.next(consumer);
}
@Override
protected int estimateSize() {
int sourceSize = sourceStream.estimateSize();
return sourceSize >= 0 ? Math.max(0, sourceSize - skip) : -1;
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Predicate;
public class TSortedStreamImpl<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> sourceStream;
private T[] array;
private int index;
@SuppressWarnings("unchecked")
public TSortedStreamImpl(TSimpleStreamImpl<T> sourceStream, Comparator<? super T> comparator) {
this.sourceStream = sourceStream;
array = (T[]) sourceStream.toArray(Object[]::new);
Arrays.sort(array, comparator);
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (index < array.length) {
if (!consumer.test(array[index++])) {
break;
}
}
return index < array.length;
}
@Override
protected int estimateSize() {
return sourceStream.estimateSize();
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,78 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public class TSpecializedConcatStream<T> extends TSimpleStreamImpl<T> {
TSimpleStreamImpl<T> first;
TSimpleStreamImpl<T> second;
TSimpleStreamImpl<T> current;
public TSpecializedConcatStream(TSimpleStreamImpl<T> first, TSimpleStreamImpl<T> second) {
this.first = first;
this.second = second;
current = first;
}
@Override
public boolean next(Predicate<? super T> consumer) {
if (current == null) {
return false;
}
while (true) {
if (current.next(consumer)) {
return true;
}
if (current == first) {
current = second;
} else {
current = null;
return false;
}
}
}
@Override
protected int estimateSize() {
int firstSize = first.estimateSize();
int secondSize = second.estimateSize();
return firstSize >= 0 && secondSize >= 0 ? firstSize + secondSize : -1;
}
@Override
public long count() {
return first.count() + second.count();
}
@Override
public void close() throws Exception {
RuntimeException suppressed = null;
try {
first.close();
} catch (RuntimeException e) {
suppressed = e;
}
try {
second.close();
} catch (RuntimeException e) {
if (suppressed != null) {
e.addSuppressed(suppressed);
}
throw e;
}
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Consumer;
import org.teavm.classlib.java.util.TCollection;
import org.teavm.classlib.java.util.TIterator;
import org.teavm.classlib.java.util.TSpliterator;
public class TSpliteratorOverCollection<T> implements TSpliterator<T> {
private TCollection<T> collection;
private TIterator<T> iterator;
public TSpliteratorOverCollection(TCollection<T> collection) {
this.collection = collection;
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
ensureIterator();
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public void forEachRemaining(Consumer<? super T> action) {
ensureIterator();
while (iterator.hasNext()) {
action.accept(iterator.next());
}
}
private void ensureIterator() {
if (iterator == null) {
iterator = collection.iterator();
}
}
@Override
public TSpliterator<T> trySplit() {
return null;
}
@Override
public long estimateSize() {
return collection.size();
}
@Override
public int characteristics() {
return TSpliterator.SIZED;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import org.teavm.classlib.java.util.TArrayList;
import org.teavm.classlib.java.util.TList;
import org.teavm.classlib.java.util.stream.TStream;
public class TStreamBuilder<T> implements TStream.Builder<T> {
private TList<T> elements = new TArrayList<>();
@Override
public void accept(T t) {
elements.add(t);
}
@Override
public TStream<T> build() {
return elements.stream();
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class TStreamOverSpliterator<T> extends TSimpleStreamImpl<T> {
private Spliterator<T> spliterator;
public TStreamOverSpliterator(Spliterator<T> spliterator) {
this.spliterator = spliterator;
}
@Override
public boolean next(Predicate<? super T> consumer) {
AdapterAction<T> action = new AdapterAction<>(consumer);
while (spliterator.tryAdvance(action)) {
if (!action.wantsMore) {
return true;
}
}
return false;
}
static class AdapterAction<T> implements Consumer<T> {
private Predicate<? super T> consumer;
boolean wantsMore;
AdapterAction(Predicate<? super T> consumer) {
this.consumer = consumer;
}
@Override
public void accept(T t) {
wantsMore = consumer.test(t);
}
}
@Override
protected int estimateSize() {
return (int) spliterator.estimateSize();
}
@Override
public long count() {
return spliterator.hasCharacteristics(Spliterator.SIZED) ? (int) spliterator.estimateSize() : super.count();
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate;
public abstract class TWrappingStreamImpl<T, S> extends TSimpleStreamImpl<T> {
TSimpleStreamImpl<S> sourceStream;
public TWrappingStreamImpl(TSimpleStreamImpl<S> sourceStream) {
this.sourceStream = sourceStream;
}
@Override
public boolean next(Predicate<? super T> consumer) {
return sourceStream.next(wrap(consumer));
}
protected abstract Predicate<S> wrap(Predicate<? super T> consumer);
@Override
protected int estimateSize() {
return sourceStream.estimateSize();
}
@Override
public void close() throws Exception {
sourceStream.close();
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TArrayIntStreamImpl extends TSimpleIntStreamImpl {
private int[] array;
private int index;
private int end;
private int size;
public TArrayIntStreamImpl(int[] array, int start, int end) {
this.array = array;
index = start;
this.end = end;
size = end - start;
}
@Override
public boolean next(IntPredicate consumer) {
while (index < end) {
if (!consumer.test(array[index++])) {
break;
}
}
return index < end;
}
@Override
protected int estimateSize() {
return size;
}
@Override
public long count() {
return size;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.Predicate;
import org.teavm.classlib.java.util.stream.impl.TSimpleStreamImpl;
public class TBoxedIntStream extends TSimpleStreamImpl<Integer> {
private TSimpleIntStreamImpl source;
public TBoxedIntStream(TSimpleIntStreamImpl source) {
this.source = source;
}
@Override
public boolean next(Predicate<? super Integer> consumer) {
return source.next(consumer::test);
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TCloseHandlingIntStream extends TSimpleIntStreamImpl {
private TSimpleIntStreamImpl innerStream;
private Runnable closeHandler;
public TCloseHandlingIntStream(TSimpleIntStreamImpl innerStream, Runnable closeHandler) {
this.innerStream = innerStream;
this.closeHandler = closeHandler;
}
@Override
public boolean next(IntPredicate consumer) {
return innerStream.next(consumer);
}
@Override
public void close() throws Exception {
RuntimeException previousException = null;
try {
closeHandler.run();
} catch (RuntimeException e) {
previousException = e;
}
try {
innerStream.close();
} catch (RuntimeException e) {
if (previousException != null) {
e.addSuppressed(previousException);
}
throw e;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TCountingIntConsumer implements IntPredicate {
int count;
@Override
public boolean test(int t) {
count++;
return true;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.HashSet;
import java.util.Set;
import java.util.function.IntPredicate;
public class TDistinctIntStreamImpl extends TWrappingIntStreamImpl {
public TDistinctIntStreamImpl(TSimpleIntStreamImpl innerStream) {
super(innerStream);
}
@Override
protected IntPredicate wrap(IntPredicate consumer) {
Set<Integer> visited = new HashSet<>();
return e -> {
if (!visited.add(e)) {
return true;
}
return consumer.test(e);
};
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TEmptyIntStreamImpl extends TSimpleIntStreamImpl {
@Override
public boolean next(IntPredicate consumer) {
return false;
}
@Override
protected int estimateSize() {
return 0;
}
@Override
public long count() {
return 1;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TFilteringIntStreamImpl extends TWrappingIntStreamImpl {
private IntPredicate filter;
public TFilteringIntStreamImpl(TSimpleIntStreamImpl innerStream, IntPredicate filter) {
super(innerStream);
this.filter = filter;
}
@Override
protected IntPredicate wrap(IntPredicate consumer) {
return t -> {
if (!filter.test(t)) {
return true;
}
return consumer.test(t);
};
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate;
public class TFindFirstIntConsumer implements IntPredicate {
public int result;
boolean hasAny;
@Override
public boolean test(int t) {
hasAny = true;
result = t;
return false;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util.stream.intimpl;
import java.util.PrimitiveIterator;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import org.teavm.classlib.java.util.stream.TIntStream;
public class TFlatMappingIntStreamImpl extends TSimpleIntStreamImpl {
private TSimpleIntStreamImpl sourceStream;
private TIntStream current;
private PrimitiveIterator.OfInt iterator;
private IntFunction<? extends TIntStream> mapper;
private boolean done;
public TFlatMappingIntStreamImpl(TSimpleIntStreamImpl sourceStream, IntFunction<? extends TIntStream> mapper) {
this.sourceStream = sourceStream;
this.mapper = mapper;
}
@Override
public boolean next(IntPredicate consumer) {
while (true) {
if (current == null) {
if (done) {
return false;
}
boolean hasMore = sourceStream.next(e -> {
current = mapper.apply(e);
return false;
});
if (!hasMore) {
done = true;
}
if (current == null) {
done = true;
return false;
}
}
if (current instanceof TSimpleIntStreamImpl) {
@SuppressWarnings("unchecked")
TSimpleIntStreamImpl castCurrent = (TSimpleIntStreamImpl) current;
if (castCurrent.next(consumer)) {
return true;
}
current = null;
} else {
iterator = current.iterator();
while (iterator.hasNext()) {
int e = iterator.nextInt();
if (!consumer.test(e)) {
return true;
}
}
iterator = null;
current = null;
}
}
}
@Override
public void close() throws Exception {
current = null;
iterator = null;
sourceStream.close();
}
}

Some files were not shown because too many files have changed in this diff Show More