Fix Stream.collect. Add some of the missing functional interfaces

This commit is contained in:
Alexey Andreev 2019-12-04 15:50:10 +03:00
parent 037acc613c
commit 477d8b2d69
8 changed files with 173 additions and 4 deletions

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TObjDoubleConsumer<T> {
void accept(T t, double u);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TObjIntConsumer<T> {
void accept(T t, int u);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TObjLongConsumer<T> {
void accept(T t, long u);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TToDoubleBiFunction<T, U> {
double applyAsDouble(T t, U u);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TToIntBiFunction<T, U> {
int applyAsInt(T t, U u);
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2015 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 TToLongBiFunction<T, U> {
long applyAsLong(T t, U u);
}

View File

@ -212,10 +212,15 @@ public abstract class TSimpleStreamImpl<T> implements TStream<T> {
public <R, A> R collect(TCollector<? super T, A, R> collector) { public <R, A> R collect(TCollector<? super T, A, R> collector) {
A collection = collector.supplier().get(); A collection = collector.supplier().get();
BiConsumer<A, ? super T> accumulator = collector.accumulator(); BiConsumer<A, ? super T> accumulator = collector.accumulator();
next(e -> { while (true) {
boolean hasMore = next(e -> {
accumulator.accept(collection, e); accumulator.accept(collection, e);
return true; return true;
}); });
if (!hasMore) {
break;
}
}
return collector.finisher().apply(collection); return collector.finisher().apply(collection);
} }

View File

@ -18,13 +18,21 @@ package org.teavm.classlib.java.util.stream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import com.carrotsearch.hppc.DoubleArrayList;
import com.carrotsearch.hppc.IntArrayList;
import com.carrotsearch.hppc.LongArrayList;
import com.carrotsearch.hppc.cursors.DoubleCursor;
import com.carrotsearch.hppc.cursors.IntCursor;
import com.carrotsearch.hppc.cursors.LongCursor;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.PrimitiveIterator; import java.util.PrimitiveIterator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.DoubleConsumer; import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
import java.util.function.LongConsumer; import java.util.function.LongConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream; import java.util.stream.DoubleStream;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.LongStream; import java.util.stream.LongStream;
@ -52,6 +60,13 @@ final class Helper {
} }
assertEquals(expectedText, sb.toString()); assertEquals(expectedText, sb.toString());
sb.setLength(0);
List<Integer> list = streamSupplier.get().collect(Collectors.toList());
for (Integer e : list) {
sb.append(e).append(';');
}
assertEquals(expectedText, sb.toString());
assertEquals(expected.length, streamSupplier.get().count()); assertEquals(expected.length, streamSupplier.get().count());
if (expected.length > 0) { if (expected.length > 0) {
@ -91,6 +106,13 @@ final class Helper {
} }
assertEquals(expectedText, sb.toString()); assertEquals(expectedText, sb.toString());
sb.setLength(0);
IntArrayList list = streamSupplier.get().collect(IntArrayList::new, IntArrayList::add, IntArrayList::addAll);
for (IntCursor cursor : list) {
sb.append(cursor.value).append(';');
}
assertEquals(expectedText, sb.toString());
assertEquals(expected.length, streamSupplier.get().count()); assertEquals(expected.length, streamSupplier.get().count());
if (expected.length > 0) { if (expected.length > 0) {
@ -130,6 +152,14 @@ final class Helper {
} }
assertEquals(expectedText, sb.toString()); assertEquals(expectedText, sb.toString());
sb.setLength(0);
LongArrayList list = streamSupplier.get().collect(LongArrayList::new, LongArrayList::add,
LongArrayList::addAll);
for (LongCursor cursor : list) {
sb.append(cursor.value).append(';');
}
assertEquals(expectedText, sb.toString());
assertEquals(expected.length, streamSupplier.get().count()); assertEquals(expected.length, streamSupplier.get().count());
if (expected.length > 0) { if (expected.length > 0) {
@ -169,6 +199,14 @@ final class Helper {
} }
assertEquals(expectedText, sb.toString()); assertEquals(expectedText, sb.toString());
sb.setLength(0);
DoubleArrayList list = streamSupplier.get().collect(DoubleArrayList::new, DoubleArrayList::add,
DoubleArrayList::addAll);
for (DoubleCursor cursor : list) {
sb.append(cursor.value).append(';');
}
assertEquals(expectedText, sb.toString());
assertEquals(expected.length, streamSupplier.get().count()); assertEquals(expected.length, streamSupplier.get().count());
if (expected.length > 0) { if (expected.length > 0) {