Last active
February 9, 2019 20:48
-
-
Save cppio/a024d38cfd41e197aafdcc93ffa3b732 to your computer and use it in GitHub Desktop.
Simple wrapper around Java Collectors to use methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.EnumSet; | |
| import java.util.Set; | |
| import java.util.stream.Collector; | |
| public interface CollectorIdentityObject<T, A, R> extends CollectorObject<T, A, R> { | |
| @Override | |
| @SuppressWarnings("unchecked") | |
| default R finish(A a) { | |
| return (R) a; | |
| } | |
| @Override | |
| default Set<Characteristics> characteristics() { | |
| return EnumSet.of(Collector.Characteristics.IDENTITY_FINISH); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Collections; | |
| import java.util.Set; | |
| import java.util.function.BiConsumer; | |
| import java.util.function.BinaryOperator; | |
| import java.util.function.Function; | |
| import java.util.function.Supplier; | |
| import java.util.stream.Collector; | |
| public interface CollectorObject<T, A, R> extends Collector<T, A, R> { | |
| A supply(); | |
| void accumulate(A a, T t); | |
| A combine(A a1, A a2); | |
| R finish(A a); | |
| @Override | |
| default Supplier<A> supplier() { | |
| return this::supply; | |
| } | |
| @Override | |
| default BiConsumer<A, T> accumulator() { | |
| return this::accumulate; | |
| } | |
| @Override | |
| default BinaryOperator<A> combiner() { | |
| return this::combine; | |
| } | |
| @Override | |
| default Function<A, R> finisher() { | |
| return this::finish; | |
| } | |
| @Override | |
| default Set<Characteristics> characteristics() { | |
| return Collections.emptySet(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment