Implement some of the missing methods related to streams

This commit is contained in:
Alexey Andreev 2020-04-14 15:06:58 +03:00
parent 2bc9329cf8
commit ddf64870da
3 changed files with 291 additions and 0 deletions

View File

@ -17,8 +17,11 @@ package org.teavm.classlib.java.util;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
public class TSpliterators { public class TSpliterators {
@ -126,4 +129,209 @@ public class TSpliterators {
} }
}; };
} }
public static <T> TSpliterator<T> spliteratorUnknownSize(Iterator<? extends T> iterator, int characteristics) {
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 TSpliterator<T> trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfInt spliterator(PrimitiveIterator.OfInt iterator, long size, int characteristics) {
return new TSpliterator.OfInt() {
@Override
public boolean tryAdvance(IntConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfInt trySplit() {
return null;
}
@Override
public long estimateSize() {
return size;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfInt spliteratorUnknownSize(PrimitiveIterator.OfInt iterator, int characteristics) {
return new TSpliterator.OfInt() {
@Override
public boolean tryAdvance(IntConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfInt trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfLong spliterator(PrimitiveIterator.OfLong iterator, long size, int characteristics) {
return new TSpliterator.OfLong() {
@Override
public boolean tryAdvance(LongConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfLong trySplit() {
return null;
}
@Override
public long estimateSize() {
return size;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfLong spliteratorUnknownSize(PrimitiveIterator.OfLong iterator, int characteristics) {
return new TSpliterator.OfLong() {
@Override
public boolean tryAdvance(LongConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfLong trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfDouble spliterator(PrimitiveIterator.OfDouble iterator, long size,
int characteristics) {
return new TSpliterator.OfDouble() {
@Override
public boolean tryAdvance(DoubleConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfDouble trySplit() {
return null;
}
@Override
public long estimateSize() {
return size;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
public static TSpliterator.OfDouble spliteratorUnknownSize(PrimitiveIterator.OfDouble iterator,
int characteristics) {
return new TSpliterator.OfDouble() {
@Override
public boolean tryAdvance(DoubleConsumer action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public OfDouble trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return characteristics;
}
};
}
} }

View File

@ -16,7 +16,9 @@
package org.teavm.classlib.java.util.stream; package org.teavm.classlib.java.util.stream;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.impl.TStreamOverSpliterator; import org.teavm.classlib.java.util.stream.impl.TStreamOverSpliterator;
import org.teavm.classlib.java.util.stream.impl.TStreamOverSpliteratorSupplier;
public final class TStreamSupport { public final class TStreamSupport {
private TStreamSupport() { private TStreamSupport() {
@ -25,4 +27,8 @@ public final class TStreamSupport {
public static <T> TStream<T> stream(Spliterator<T> spliterator, boolean parallel) { public static <T> TStream<T> stream(Spliterator<T> spliterator, boolean parallel) {
return new TStreamOverSpliterator<>(spliterator); return new TStreamOverSpliterator<>(spliterator);
} }
public static <T> TStream<T> stream(Supplier<Spliterator<T>> spliterator, int characteristics, boolean parallel) {
return new TStreamOverSpliteratorSupplier<>(spliterator, characteristics);
}
} }

View File

@ -0,0 +1,77 @@
/*
* 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;
import java.util.function.Supplier;
public class TStreamOverSpliteratorSupplier<T> extends TSimpleStreamImpl<T> {
private Supplier<Spliterator<T>> supplier;
private int characteristics;
private Spliterator<T> spliterator;
public TStreamOverSpliteratorSupplier(Supplier<Spliterator<T>> supplier, int characteristics) {
this.supplier = supplier;
this.characteristics = characteristics;
}
private void init() {
if (spliterator == null) {
spliterator = supplier.get();
supplier = null;
}
}
@Override
public boolean next(Predicate<? super T> consumer) {
AdapterAction<T> action = new AdapterAction<>(consumer);
init();
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() {
init();
return (int) spliterator.estimateSize();
}
@Override
public long count() {
init();
return spliterator.hasCharacteristics(Spliterator.SIZED) ? (int) spliterator.estimateSize() : super.count();
}
}