classlib: add TCollectors#collectingAndThen and TCollectors#toUnmodifiableList

This commit is contained in:
Ulugbek 2022-07-21 14:35:23 +02:00 committed by Alexey Andreev
parent 81ed5ef829
commit b6837340e5

View File

@ -17,6 +17,8 @@ package org.teavm.classlib.java.util.stream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -43,6 +45,10 @@ public final class TCollectors {
return toCollection(ArrayList::new); return toCollection(ArrayList::new);
} }
public static <T> TCollector<T, ?, List<T>> toUnmodifiableList() {
return collectingAndThen(toList(), Collections::unmodifiableList);
}
public static <T> TCollector<T, ?, Set<T>> toSet() { public static <T> TCollector<T, ?, Set<T>> toSet() {
return toCollection(HashSet::new); return toCollection(HashSet::new);
} }
@ -117,4 +123,19 @@ public final class TCollectors {
}, },
TCollector.Characteristics.IDENTITY_FINISH); TCollector.Characteristics.IDENTITY_FINISH);
} }
public static <T, A, R, K> TCollector<T, A, K> collectingAndThen(
TCollector<T, A, R> downstream,
Function<R, K> finisher) {
EnumSet<TCollector.Characteristics> newCharacteristics = EnumSet.copyOf(downstream.characteristics());
newCharacteristics.remove(TCollector.Characteristics.IDENTITY_FINISH);
return new TCollectorImpl<>(downstream.supplier(),
downstream.accumulator(),
downstream.combiner(),
downstream.finisher().andThen(finisher),
newCharacteristics);
}
} }