use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class FutureStream method mergeLatest.
/**
* Merges this stream and the supplied Streams into a single Stream where the next value
* is the next returned across any of the involved Streams. Suitable for merging infinite streams
*
* <pre>
* {@code
* FutureStream<Integer> fast = ... // [1,2,3,4,5,6,7..]
* FutureStream<Integer> slow = ... // [100,200,300,400,500,600..]
*
* FutureStream<Integer> merged = fast.mergeLatest(slow); //[1,2,3,4,5,6,7,8,100,9,10,11,12,13,14,15,16,200..]
* }
* </pre>
*
* @param streams
* @return
*/
default <R> FutureStream<R> mergeLatest(final FutureStream<?>... streams) {
final Queue queue = Queue.createMergeQueue();
addToQueue(queue);
ReactiveSeq.of(streams).forEach(s -> s.addToQueue(queue));
return fromStream(queue.stream(this.getSubscription()));
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class PipesTest method testGetPresent.
@Test
public void testGetPresent() {
pipes.register("hello", new Queue());
assertTrue(pipes.get("hello").isPresent());
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class LazyToQueue method toQueue.
/*
* Populate provided queues with the sharded data from this Stream.
*
* @param shards Map of key to Queue shards
* @param sharder Sharding function, element to key converter
* @see com.oath.cyclops.react.stream.traits.ToQueue#toQueue(java.util.Map, java.util.function.Function)
*/
@Override
default <K> void toQueue(final Map<K, Queue<U>> shards, final Function<? super U, ? extends K> sharder) {
// in this case all the items have to be pushed to the shards,
// we can't rely on the client pulling them all to getValue them in to the right shards
final LazyReact service = getPopulator();
then(it -> shards.get(sharder.apply(it)).offer(it), service.getExecutor()).runThread(() -> {
shards.values().forEach(it -> it.close());
returnPopulator(service);
});
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class EagerFutureStreamFunctions method withLatest.
/**
* Zip two streams into one using a {@link BiFunction} to produce resulting.
* values. Uses the latest values from each rather than waiting for both.
*/
static <T1, T2, R> ReactiveSeq<R> withLatest(final SimpleReactStream<T1> left, final SimpleReactStream<T2> right, final BiFunction<T1, T2, R> zipper) {
final Queue q = left.then(it -> new Val(Val.Pos.left, it)).merge(right.then(it -> new Val(Val.Pos.right, it))).toQueue();
final Iterator<Val> it = q.stream(left.getSubscription()).iterator();
class Zip implements Iterator<R> {
T1 lastLeft = null;
T2 lastRight = null;
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public R next() {
final Val v = it.next();
if (v.pos == Val.Pos.left) {
lastLeft = (T1) v.val;
return zipper.apply(lastLeft, lastRight);
} else
lastRight = (T2) v.val;
return (R) Optional.empty();
}
}
return ReactiveSeq.fromIterator(new Zip()).filter(next -> !(next instanceof Optional));
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class EagerFutureStreamFunctions method dropUntil.
static <T1, T2> ReactiveSeq<T1> dropUntil(final SimpleReactStream<T1> left, final SimpleReactStream<T2> right) {
final Queue q = left.then(it -> new Val(Val.Pos.left, it)).merge(right.then(it -> new Val(Val.Pos.right, it))).toQueue();
final Iterator<Val> it = q.stream(left.getSubscription()).iterator();
new Object();
class Zip implements Iterator<T1> {
Optional<T1> lastLeft = Optional.empty();
Optional<T2> lastRight = Optional.empty();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public T1 next() {
final Val v = it.next();
if (v.pos == Val.Pos.left) {
if (lastRight.isPresent())
lastLeft = Optional.of((T1) v.val);
} else
lastRight = Optional.of((T2) v.val);
if (!lastRight.isPresent())
return (T1) Optional.empty();
if (lastLeft.isPresent())
return lastLeft.get();
else
return (T1) Optional.empty();
}
}
return ReactiveSeq.fromIterable(() -> new Zip()).filter(next -> !(next instanceof Optional));
}
Aggregations