public static final <A> Collection<A> toConcurrentLazyCollection(Iterator<A> iterator){ return toLazyCollection(iterator,true); } private static final <A> Collection<A> toLazyCollection(Iterator<A> iterator,boolean concurrent) {
/** * Lazily constructs a Collection from specified Stream. Collections iterator may be safely used * concurrently by multiple threads. * @param stream * @return */ public static final <A> Collection<A> toConcurrentLazyCollection(Stream<A> stream) { return toConcurrentLazyCollection(stream.iterator()); } public static final <A> Collection<A> toConcurrentLazyCollection(Iterator<A> iterator){
public static final <A> Collection<A> toLazyCollection(Iterator<A> iterator){ return toLazyCollection(iterator,false); } /**
/** * Projects an immutable collection of this stream. Initial iteration over the collection is not thread safe * (can't be performed by multiple threads concurrently) subsequent iterations are. * * @return An immutable collection of this stream. */ public static final <A> Collection<A> toLazyCollection(Stream<A> stream) { return toLazyCollection(stream.iterator()); } public static final <A> Collection<A> toLazyCollection(Iterator<A> iterator){
/** * Create a Stream that finitely cycles this Stream, provided number of times * * <pre> * {@code * assertThat(LazyFutureStream.of(1,2,2).cycle(3) .collect(Collectors.toList()), equalTo(Arrays.asList(1,2,2,1,2,2,1,2,2))); * } * </pre> * @return New cycling stream */ default LazyFutureStream<U> cycle(int times){ RepeatableStream s = new RepeatableStream(ToLazyCollection.toLazyCollection(toQueue().stream(getSubscription()).iterator())); return fromStream(Stream.iterate(s.stream(),s1-> s.stream()).limit(times).flatMap(i->i)); } /**
/** * Create a Stream that infinitely cycles this Stream * * <pre> * {@code * assertThat(LazyFutureStream.of(1,2,2).cycle().limit(6) .collect(Collectors.toList()), equalTo(Arrays.asList(1,2,2,1,2,2)); * } * </pre> * @return New cycling stream */ @Override default LazyFutureStream<U> cycle(){ RepeatableStream s = new RepeatableStream(ToLazyCollection.toLazyCollection(toQueue().stream(getSubscription()).iterator())); return fromStream(Stream.iterate(s.stream(),s1-> s.stream()).flatMap(i->i)); }