use of cyclops.data.tuple.Tuple2 in project cyclops by aol.
the class ZippingLatestOperator method subscribeAll.
@Override
public void subscribeAll(Consumer<? super R> onNext, Consumer<? super Throwable> onError, Runnable onCompleteDs) {
AtomicBoolean active = new AtomicBoolean(false);
ManyToOneConcurrentArrayQueue<R> data = new ManyToOneConcurrentArrayQueue<R>(1024);
AtomicReference<Tuple2<T1, T2>> nextValue = new AtomicReference<>(Tuple.tuple((T1) UNSET, (T2) UNSET));
// left & right compelte can be merged into single integer
AtomicBoolean leftComplete = new AtomicBoolean(false);
AtomicBoolean rightComplete = new AtomicBoolean(false);
left.subscribeAll(e -> {
try {
boolean set = false;
while (!set) {
Tuple2<T1, T2> local = nextValue.get();
Tuple2<T1, T2> updated = local.map1(__ -> (T1) e);
set = nextValue.compareAndSet(local, updated);
if (set) {
if (updated._2() != UNSET) {
if (active.compareAndSet(false, true)) {
data.drain(onNext::accept);
onNext.accept(applyFn(updated));
active.set(false);
} else {
while (!data.offer(applyFn(updated))) {
}
}
}
}
}
} catch (Throwable t) {
onError.accept(t);
}
}, e -> {
onError.accept(e);
}, () -> {
while (!active.compareAndSet(false, true)) {
}
data.drain(onNext::accept);
if (rightComplete.get()) {
onCompleteDs.run();
}
leftComplete.set(true);
active.set(false);
});
right.subscribeAll(e -> {
try {
boolean set = false;
while (!set) {
Tuple2<T1, T2> local = nextValue.get();
Tuple2<T1, T2> updated = local.map2(__ -> (T2) e);
set = nextValue.compareAndSet(local, updated);
if (set) {
if (updated._1() != UNSET) {
if (active.compareAndSet(false, true)) {
data.drain(onNext::accept);
onNext.accept(applyFn(updated));
active.set(false);
} else {
while (!data.offer(applyFn(updated))) {
}
}
}
}
}
} catch (Throwable t) {
onError.accept(t);
}
}, e -> {
onError.accept(e);
}, () -> {
while (!active.compareAndSet(false, true)) {
}
data.drain(onNext::accept);
if (leftComplete.get()) {
onCompleteDs.run();
}
rightComplete.set(true);
active.set(false);
});
}
use of cyclops.data.tuple.Tuple2 in project cyclops by aol.
the class SpoutsZipTest method pairWiseIncremental.
@Test
public void pairWiseIncremental() {
AtomicBoolean data = new AtomicBoolean(false);
AtomicReference<Vector<Tuple2<Integer, String>>> values = new AtomicReference<>(Vector.empty());
AtomicBoolean complete = new AtomicBoolean(false);
AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
Subscription sub = Spouts.of(1).zip(Spouts.of("test")).zip(Spouts.of("test2")).map(t -> Tuple.tuple(t._1()._1(), t._1()._2() + t._2())).forEach(0, n -> {
data.set(true);
values.updateAndGet(v -> v.plus(n));
}, e -> {
error.set(e);
}, () -> {
complete.set(true);
});
assertFalse(data.get());
assertFalse(complete.get());
assertNull(error.get());
assertThat(values.get(), equalTo(Vector.empty()));
sub.request(10l);
assertTrue(data.get());
assertTrue(complete.get());
assertNull(error.get());
assertThat(values.get(), equalTo(Vector.of(Tuple.tuple(1, "testtest2"))));
}
use of cyclops.data.tuple.Tuple2 in project cyclops by aol.
the class SimpleReactStream method zip.
/**
* Zip two Streams, zipping against the underlying futures of both Streams
* Placeholders (Futures) will be populated immediately in the new zipped Stream and results
* will be populated asyncrhonously
*
* @param other Another FutureStream to zip Futures with
* @return New Sequence of CompletableFutures
*/
default <R> SimpleReactStream<Tuple2<U, R>> zip(final SimpleReactStream<R> other) {
final ReactiveSeq seq = ReactiveSeq.fromStream(getLastActive().stream()).zip(ReactiveSeq.fromStream(other.getLastActive().stream()));
final ReactiveSeq<Tuple2<CompletableFuture<U>, CompletableFuture<R>>> withType = seq;
final SimpleReactStream futureStream = fromStreamOfFutures((Stream) withType.map(t -> CompletableFuture.allOf(t._1(), t._2()).thenApply(v -> Tuple.tuple(t._1().join(), t._2().join()))));
return futureStream;
}
Aggregations