/** * Create a Stream that infiniteable cycles the provided Streamable * @param s Streamable to cycle * @return New cycling stream */ public static <U> Stream<U> cycle(Streamable<U> s){ return StreamUtils.cycle(s); }
/** * Create a stream from a map * * @param it Iterator to convert to a Stream * @return Stream from a map */ public static <K,V> Stream<Map.Entry<K, V>> stream(Map<K,V> it){ return StreamUtils.stream(it); } /**
/** * Apply multiple Collectors, simultaneously to a Stream * * @param stream Stream to collect * @param collectors Collectors to apply * @return Result as a list */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T,A,R> List<R> collect(Stream<T> stream, Iterable<Collector> collectors){ return StreamUtils.collect(stream, collectors); } /**
/** * Convert to a Stream with the values infinitely cycled * * <pre> * {@code * assertEquals(asList(1, 1, 1, 1, 1,1),of(1).cycle().limit(6).toList()); * } * </pre> * * @return Stream with values repeated */ public final SequenceM<T> cycle() { return StreamUtils.sequenceM(StreamUtils.cycle(stream),reversable); } /**
/** * Repeat in a Stream until specified predicate holds * * <pre> * {@code * count =0; assertThat(StreamUtils.cycleUntil(Stream.of(1,2,2,3) ,next -> count++>10 ) .collect(Collectors.toList()),equalTo(Arrays.asList(1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2))); * } * </pre> * @param predicate * repeat while true * @return Repeating Stream */ public final static <T> Stream<T> cycleUntil(Stream<T> stream,Predicate<? super T> predicate) { return StreamUtils.limitUntil(StreamUtils.cycle(stream),predicate); }
/** * Repeat in a Stream while specified predicate holds * <pre> * {@code * int count =0; * assertThat(StreamUtils.cycleWhile(Stream.of(1,2,2) ,next -> count++<6 ) .collect(Collectors.toList()),equalTo(Arrays.asList(1,2,2,1,2,2))); * } * </pre> * @param predicate * repeat while true * @return Repeating Stream */ public final static <T> Stream<T> cycleWhile(Stream<T> stream,Predicate<? super T> predicate) { return StreamUtils.limitWhile(StreamUtils.cycle(stream),predicate); }
private final static <T> Tuple2<Stream<T>,Stream<T>> duplicatePos(Stream<T> stream,int pos){ Tuple2<Iterator<T>,Iterator<T>> Tuple2 = StreamUtils.toBufferingDuplicator(stream.iterator(),pos); return new Tuple2(StreamUtils.stream(Tuple2.v1()),StreamUtils.stream(Tuple2.v2())); } /**
/** * Triplicates a Stream * Buffers intermediate values, leaders may change positions so a limit * can be safely applied to the leading stream. Not thread-safe. * <pre> * {@code * Tuple3<SequenceM<Tuple3<T1,T2,T3>>,SequenceM<Tuple3<T1,T2,T3>>,SequenceM<Tuple3<T1,T2,T3>>> Tuple3 = sequence.triplicate(); * } * </pre> */ @SuppressWarnings("unchecked") public final static <T> Tuple3<Stream<T>,Stream<T>,Stream<T>> triplicate(Stream<T> stream){ Stream<Stream<T>> its = StreamUtils.toBufferingCopier(stream.iterator(),3) .stream() .map(it -> StreamUtils.stream(it)); Iterator<Stream<T>> it = its.iterator(); return new Tuple3(it.next(),it.next(),it.next()); } /**
public static Stream<Object> stream(Object t){ if(t instanceof Iterable){ return Stream.concat(StreamUtils.stream((Iterable)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Stream){ return Stream.concat( ((Stream)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Iterator){ return Stream.concat( StreamUtils.stream((Iterator)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } if(t instanceof Map){ return Stream.concat(StreamUtils.stream((Map)t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); } return Stream.concat(Stream.of(t).map(SeqUtils::nonNull),(StreamUtils.cycle(Stream.of(EMPTY)))); }
/** * Reduce with multiple reducers in parallel * NB if this Monad is an Optional [Arrays.asList(1,2,3)] reduce will operate on the Optional as if the list was one value * To reduce over the values on the list, called streamedStreamUtils.sequenceM() first. I.e. streamedStreamUtils.sequenceM().reduce(reducer) * * @param reducers * @return */ public final List<T> reduce(Iterable<Monoid<T>> reducers){ return StreamUtils.reduce(stream, reducers); }
/** * Reverse a Stream * * @param stream Stream to reverse * @return Reversed stream */ public static <U> Stream<U> reverse(Stream<U> stream){ return StreamUtils.reverse(stream); } /**
/** * Create a reversed Stream from a List * * @param list List to create a reversed Stream from * @return Reversed Stream */ public static <U> Stream<U> reversedStream(List<U> list){ return StreamUtils.reversedStream(list); } /**
/** * <pre> * {@code * Stream<String> helloWorld = Stream.of(); Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld); assertTrue(!headAndTail.isPresent()); * } * </pre> * @param stream to extract head and tail from * @return */ public final static <T> Optional<HeadAndTail<T>> headAndTailOptional(Stream<T> stream){ Iterator<T> it = stream.iterator(); if(!it.hasNext()) return Optional.empty(); return Optional.of(new HeadAndTail(it.next(),sequenceM(stream(it),Optional.empty()))); }
public <X extends Throwable> void forEachEvent(Consumer<? super T> consumerElement, Consumer<? super Throwable> consumerError, Runnable onComplete){ StreamUtils.forEachEvent(this, consumerElement, consumerError, onComplete); }
public <X extends Throwable> void forEachWithError(Consumer<? super T> consumerElement, Consumer<? super Throwable> consumerError){ StreamUtils.forEachWithError(this, consumerElement, consumerError); } public <X extends Throwable> void forEachEvent(Consumer<? super T> consumerElement,
/** *<pre> * {@code * assertThat(StreamUtils.flatMapCompletableFuture(Stream.of(1,2,3), * i->CompletableFuture.completedFuture(i+2)) * .collect(Collectors.toList()), * equalTo(Arrays.asList(3,4,5))); * } *</pre> */ public final static <T,R> Stream<R> flatMapCompletableFuture(Stream<T> stream,Function<? super T,CompletableFuture<? extends R>> fn) { return stream.flatMap( in->StreamUtils.completableFutureToStream(fn.apply(in))); }
/** * Concat an Object and a Stream * If the Object is a Stream, Streamable or Iterable will be converted (or left) in Stream form and concatonated * Otherwise a new Stream.of(o) is created * * @param o Object to concat * @param stream Stream to concat * @return Concatonated Stream */ public static <U> Stream<U> concat(Object o, Stream<U> stream){ return StreamUtils.concat(o, stream); } /**
/** * Duplicate a Stream, buffers intermediate values, leaders may change positions so a limit * can be safely applied to the leading stream. Not thread-safe. * <pre> * {@code * Tuple2<SequenceM<Integer>, SequenceM<Integer>> copies =of(1,2,3,4,5,6).duplicate(); assertTrue(copies.v1.anyMatch(i->i==2)); assertTrue(copies.v2.anyMatch(i->i==2)); * * } * </pre> * * @return duplicated stream */ public final static <T> Tuple2<Stream<T>,Stream<T>> duplicate(Stream<T> stream){ Tuple2<Iterator<T>,Iterator<T>> Tuple2 = StreamUtils.toBufferingDuplicator(stream.iterator()); return new Tuple2(StreamUtils.stream(Tuple2.v1()),StreamUtils.stream(Tuple2.v2())); } private final static <T> Tuple2<Stream<T>,Stream<T>> duplicatePos(Stream<T> stream,int pos){
/** * Makes four copies of a Stream * Buffers intermediate values, leaders may change positions so a limit * can be safely applied to the leading stream. Not thread-safe. * * <pre> * {@code * * Tuple4<SequenceM<Tuple4<T1,T2,T3,T4>>,SequenceM<Tuple4<T1,T2,T3,T4>>,SequenceM<Tuple4<T1,T2,T3,T4>>,SequenceM<Tuple4<T1,T2,T3,T4>>> quad = sequence.quadruplicate(); * } * </pre> * @return */ @SuppressWarnings("unchecked") public final static <T> Tuple4<Stream<T>,Stream<T>,Stream<T>,Stream<T>> quadruplicate(Stream<T> stream){ Stream<Stream<T>> its = StreamUtils.toBufferingCopier(stream.iterator(),4) .stream() .map(it -> StreamUtils.stream(it)); Iterator<Stream<T>> it = its.iterator(); return new Tuple4(it.next(),it.next(),it.next(),it.next()); } /**