From b6837340e5eddc53b574ea1102ee8df922e627e8 Mon Sep 17 00:00:00 2001 From: Ulugbek Date: Thu, 21 Jul 2022 14:35:23 +0200 Subject: [PATCH] classlib: add `TCollectors#collectingAndThen` and `TCollectors#toUnmodifiableList` --- .../java/util/stream/TCollectors.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java index 5b356ea32..3056dd5e9 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java @@ -17,6 +17,8 @@ package org.teavm.classlib.java.util.stream; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -43,6 +45,10 @@ public final class TCollectors { return toCollection(ArrayList::new); } + public static TCollector> toUnmodifiableList() { + return collectingAndThen(toList(), Collections::unmodifiableList); + } + public static TCollector> toSet() { return toCollection(HashSet::new); } @@ -117,4 +123,19 @@ public final class TCollectors { }, TCollector.Characteristics.IDENTITY_FINISH); } + + public static TCollector collectingAndThen( + TCollector downstream, + Function finisher) { + + EnumSet 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); + } + }