Last active
December 11, 2020 02:57
-
-
Save chtseac/7402da123441f84b18cd79adad1bb9d2 to your computer and use it in GitHub Desktop.
Wrapping stream to improve the .collect experience
This file contains 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.Collection; | |
import java.util.Comparator; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.Spliterator; | |
import java.util.function.BiConsumer; | |
import java.util.function.BiFunction; | |
import java.util.function.BinaryOperator; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.IntFunction; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
import java.util.stream.Collector; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
/** | |
* Util class to delegate stream to reduce clutter (mainly introduced by the `collect(Collectors.xx())` syntax) | |
* | |
* lodash's `Seq` class is the inspiration | |
* | |
* Usage: | |
* | |
* // someList.toStream().map(..).filter(..).collect(Collectors.toList()) | |
* __.of(someList).map(..).filter(..).toList() | |
* | |
* Todo: implement support to other `Collector`s | |
* Todo: figure out what to do with primitive streams (probably add more classes) | |
* | |
* @author Toby Tse | |
*/ | |
public class __<T> { | |
private Stream<T> stm; | |
private __(Stream<T> stm) { this.stm = stm; } | |
private __(Collection<T> wrapped) { this(wrapped.stream()); } | |
public static <T> __<T> of(Collection<T> wrapped) { return new __<T>(wrapped); } | |
public List<T> toList() { return this.collect(Collectors.toList()); } | |
public Stream<T> toStream() { return this.stm; } | |
// wrapped methods | |
public Iterator<T> iterator() { return this.stm.iterator(); } | |
public Spliterator<T> spliterator() { return this.stm.spliterator(); } | |
public boolean isParallel() { return this.stm.isParallel(); } | |
public __<T> sequential() { return new __<T>(this.stm.sequential()); } | |
public __<T> parallel() { return new __<T>(this.stm.parallel()); } | |
public __<T> unordered() { return new __<T>(this.stm.unordered()); } | |
public __<T> onClose(Runnable closeHandler) { return new __<T>(this.stm.onClose(closeHandler)); } | |
public void close() { this.stm.close(); } | |
public __<T> filter(Predicate<? super T> predicate) { return new __<T>(this.stm.filter(predicate)); } | |
public <R> __<R> map(Function<? super T, ? extends R> mapper) { return new __<R>(this.stm.map(mapper)); } | |
// public IntStream mapToInt(ToIntFunction<? super T> mapper) { return this.stm.mapToInt(mapper); } | |
// | |
// public LongStream mapToLong(ToLongFunction<? super T> mapper) { return this.stm.mapToLong(mapper); } | |
// | |
// public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) { return this.stm.mapToDouble(mapper); } | |
public <R> __<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) { | |
return new __<R>(this.stm.flatMap(mapper)); | |
} | |
// public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) { | |
// return this.stm.flatMapToInt(mapper); | |
// } | |
// | |
// public LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) { | |
// return this.stm.flatMapToLong(mapper); | |
// } | |
// | |
// public DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) { | |
// return this.stm.flatMapToDouble(mapper); | |
// } | |
public __<T> distinct() { return new __<T>(this.stm.distinct()); } | |
public __<T> sorted() { return new __<T>(this.stm.sorted()); } | |
public __<T> sorted(Comparator<? super T> comparator) { return new __<T>(this.stm.sorted(comparator)); } | |
public __<T> peek(Consumer<? super T> action) { return new __<T>(this.stm.peek(action)); } | |
public __<T> limit(long maxSize) { return new __<T>(this.stm.limit(maxSize)); } | |
public __<T> skip(long n) { return new __<T>(this.stm.skip(n)); } | |
public __<T> takeWhile(Predicate<? super T> predicate) { return new __<T>(this.stm.takeWhile(predicate)); } | |
public __<T> dropWhile(Predicate<? super T> predicate) { return new __<T>(this.stm.dropWhile(predicate)); } | |
public void forEach(Consumer<? super T> action) { this.stm.forEach(action); } | |
public void forEachOrdered(Consumer<? super T> action) { this.stm.forEachOrdered(action); } | |
public Object[] toArray() { return this.stm.toArray(); } | |
public <A> A[] toArray(IntFunction<A[]> generator) { return this.stm.toArray(generator); } | |
public T reduce(T identity, BinaryOperator<T> accumulator) { return this.stm.reduce(identity, accumulator); } | |
public Optional<T> reduce(BinaryOperator<T> accumulator) { return this.stm.reduce(accumulator); } | |
public <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) { | |
return this.stm.reduce(identity, accumulator, combiner); | |
} | |
public <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) { | |
return this.stm.collect(supplier, accumulator, combiner); | |
} | |
public <R, A> R collect(Collector<? super T, A, R> collector) { return this.stm.collect(collector); } | |
public Optional<T> min(Comparator<? super T> comparator) { return this.stm.min(comparator); } | |
public Optional<T> max(Comparator<? super T> comparator) { return this.stm.max(comparator); } | |
public long count() { return this.stm.count(); } | |
public boolean anyMatch(Predicate<? super T> predicate) { return this.stm.anyMatch(predicate); } | |
public boolean allMatch(Predicate<? super T> predicate) { return this.stm.allMatch(predicate); } | |
public boolean noneMatch(Predicate<? super T> predicate) { return this.stm.noneMatch(predicate); } | |
public Optional<T> findFirst() { return this.stm.findFirst(); } | |
public Optional<T> findAny() { return this.stm.findAny(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment