public static <T> ReactiveSeq<T> fromSpliterator(Spliterator<T> spliterator){ return syncStream(new SpliteratorToOperator<T>(spliterator)); } /**
@Override protected <R> ReactiveSeq<R> createSeq(Stream<R> stream) { if (stream instanceof ReactiveSeq) return (ReactiveSeq) stream; if (stream instanceof Iterable) return new ReactiveStreamX<>(new IterableSourceOperator<>((Iterable<R>) stream)); return new ReactiveStreamX<>(new SpliteratorToOperator<>(stream.spliterator())); }
@Override public <U, R> ReactiveSeq<R> zipWithStream(Stream<? extends U> other, BiFunction<? super T, ? super U, ? extends R> zipper) { Operator<U> right; if (other instanceof ReactiveStreamX) { right = ((ReactiveStreamX<U>) other).source; } else { right = new SpliteratorToOperator<U>(((Stream<U>) other).spliterator()); } return createSeq(new ZippingOperator<>(source, right, zipper), async); }
/** * Unfold a function into a ReactiveSeq * * <pre> * {@code * ReactiveSeq.unfold(1,i->i<=6 ? Optional.of(Tuple.tuple(i,i+1)) : Optional.empty()); * * //(1,2,3,4,5) * * }</code> * * @param seed Initial value * @param unfolder Iteratively applied function, terminated by an empty Optional * @return ReactiveSeq generated by unfolder function */ static <U, T> ReactiveSeq<T> unfold(final U seed, final Function<? super U, Option<Tuple2<T, U>>> unfolder) { return reactiveStream(new SpliteratorToOperator<T>(new UnfoldSpliterator<>(seed, unfolder))); } public static <T> ReactiveSeq<T> concat(Publisher<Publisher<T>> pubs){
public Operator<Integer> createTwoAndError(){ return new ArrayConcatonatingOperator<>(new ArrayOfValuesOperator<>(), new SpliteratorToOperator<>(Stream.of(1).spliterator()), Fixtures.oneAndErrorSource); } public Operator<Integer> createThreeErrors(){
public Operator<Integer> createThreeErrors(){ return new ArrayConcatonatingOperator<>(new ArrayOfValuesOperator<>(), new SpliteratorToOperator<>(Stream.of().spliterator()), Fixtures.threeErrorsSource); }
@Test public void streamOf2(){ new ReactiveStreamX<>(new SpliteratorToOperator<>(Stream.of(2,3).spliterator())) .subscribe(new Subscriber<Integer>() { @Override
@Test public void requestOne(){ Subscription sub = new FilterOperator<>(new SpliteratorToOperator<Integer>(ReactiveSeq.fill(10).limit(100).spliterator()), i->true) .subscribe(values::add,errors::add,()->onComplete=true); sub.request(1l); assertThat(values.size(),equalTo(1)); assertFalse(onComplete); sub.cancel(); sub.request(1l); assertThat(values.size(),equalTo(1)); assertFalse(onComplete); } @Test
@Test public void request1(){ new SpliteratorToOperator<Integer>(ReactiveSeq.of(1,2,3).spliterator()) .subscribe(values::add,errors::add,()->onComplete=true) .request(1l); assertThat(values.size(),equalTo(1)); }
public Operator<Integer> createThree(){ return new ArrayConcatonatingOperator<>(new ArrayOfValuesOperator<>(1), new ArrayOfValuesOperator<>(), new SpliteratorToOperator<>(Stream.of(2,3).spliterator())); /** return new ArrayConcatonatingOperator<>(new SpliteratorToOperator<>(Stream.of(1).spliterator()), new ArrayOfValuesOperator<>(), new ArrayOfValuesOperator<>(2,3));**/ } public Operator<Integer> createTwoAndError(){
@Override public <U> ReactiveSeq<Tuple2<T, U>> zipWithStream(Stream<? extends U> other) { Operator<U> right; if (other instanceof ReactiveStreamX) { right = ((ReactiveStreamX<U>) other).source; } else if (other instanceof Iterable) { right = new IterableSourceOperator<U>(((Iterable<U>) other)); } else { //not replayable right = new SpliteratorToOperator<U>(((Stream<U>) other).spliterator()); } ReactiveStreamX<Tuple2<T, U>> res = createSeq(new ZippingOperator<>(source, right, Tuple::tuple)); if (this.async == SYNC) { //zip could recieve an asyncrhonous Stream so we force onto the async path return res.withAsync(BACKPRESSURE); } return res; }
@Test public void requestTwo(){ new SpliteratorToOperator<Integer>(ReactiveSeq.fill(10).limit(100).spliterator()) .subscribe(values::add,errors::add,()->onComplete=true) .request(2l); assertThat(values.size(),equalTo(2)); }
public static <T> ReactiveSeq<T> concat(Stream<? extends T>... streams){ Operator<T>[] operators = new Operator[streams.length]; int index = 0; Type async = Type.SYNC; for(Stream<T> next : (Stream<T>[])streams){ if(next instanceof ReactiveStreamX){ ReactiveStreamX rsx = ((ReactiveStreamX) next); operators[index] = rsx.getSource(); if(rsx.getType()== Type.BACKPRESSURE){ async = Type.BACKPRESSURE; } if(async== Type.SYNC && rsx.getType()== Type.NO_BACKPRESSURE){ async = Type.NO_BACKPRESSURE; } }else{ operators[index] = new SpliteratorToOperator<T>(next.spliterator()); } index++; } return new ReactiveStreamX<>(new ArrayConcatonatingOperator<T>(operators)).withAsync(async); }
public static <T> ReactiveSeq<T> fromSpliterator(Spliterator<T> spliterator){ return syncStream(new SpliteratorToOperator<T>(spliterator)); } /**
@Override protected <R> ReactiveSeq<R> createSeq(Stream<R> stream) { if (stream instanceof ReactiveSeq) return (ReactiveSeq) stream; if (stream instanceof Iterable) return new ReactiveStreamX<>(new IterableSourceOperator<>((Iterable<R>) stream)); return new ReactiveStreamX<>(new SpliteratorToOperator<>(stream.spliterator())); }
@Override public <U, R> ReactiveSeq<R> zipWithStream(Stream<? extends U> other, BiFunction<? super T, ? super U, ? extends R> zipper) { Operator<U> right; if (other instanceof ReactiveStreamX) { right = ((ReactiveStreamX<U>) other).source; } else { right = new SpliteratorToOperator<U>(((Stream<U>) other).spliterator()); } return createSeq(new ZippingOperator<>(source, right, zipper), async); }
/** * Unfold a function into a ReactiveSeq * * <pre> * {@code * ReactiveSeq.unfold(1,i->i<=6 ? Optional.of(Tuple.tuple(i,i+1)) : Optional.empty()); * * //(1,2,3,4,5) * * }</code> * * @param seed Initial value * @param unfolder Iteratively applied function, terminated by an empty Optional * @return ReactiveSeq generated by unfolder function */ static <U, T> ReactiveSeq<T> unfold(final U seed, final Function<? super U, Option<Tuple2<T, U>>> unfolder) { return reactiveStream(new SpliteratorToOperator<T>(new UnfoldSpliterator<>(seed, unfolder))); } public static <T> ReactiveSeq<T> concat(Publisher<Publisher<T>> pubs){
@Override public <U> ReactiveSeq<Tuple2<T, U>> zipWithStream(Stream<? extends U> other) { Operator<U> right; if (other instanceof ReactiveStreamX) { right = ((ReactiveStreamX<U>) other).source; } else if (other instanceof Iterable) { right = new IterableSourceOperator<U>(((Iterable<U>) other)); } else { //not replayable right = new SpliteratorToOperator<U>(((Stream<U>) other).spliterator()); } ReactiveStreamX<Tuple2<T, U>> res = createSeq(new ZippingOperator<>(source, right, Tuple::tuple)); if (this.async == SYNC) { //zip could recieve an asyncrhonous Stream so we force onto the async path return res.withAsync(BACKPRESSURE); } return res; }
public static <T> ReactiveSeq<T> concat(Stream<? extends T>... streams){ Operator<T>[] operators = new Operator[streams.length]; int index = 0; Type async = Type.SYNC; for(Stream<T> next : (Stream<T>[])streams){ if(next instanceof ReactiveStreamX){ ReactiveStreamX rsx = ((ReactiveStreamX) next); operators[index] = rsx.getSource(); if(rsx.getType()== Type.BACKPRESSURE){ async = Type.BACKPRESSURE; } if(async== Type.SYNC && rsx.getType()== Type.NO_BACKPRESSURE){ async = Type.NO_BACKPRESSURE; } }else{ operators[index] = new SpliteratorToOperator<T>(next.spliterator()); } index++; } return new ReactiveStreamX<>(new ArrayConcatonatingOperator<T>(operators)).withAsync(async); }