rx.internal.operators.NotificationLite Java Examples

The following examples show how to use rx.internal.operators.NotificationLite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OperatorGroupBy.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(T t) {
    try {
        final K key = keySelector.call(t);
        GroupState<K, T> group = groups.get(key);
        if (group == null) {
            // this group doesn't exist
            if (child.isUnsubscribed()) {
                // we have been unsubscribed on the outer so won't send any  more groups
                return;
            }
            group = createNewGroup(key);
        }
        emitItem(group, NotificationLite.next(t));
    } catch (Throwable e) {
        onError(OnErrorThrowable.addValueAsLastCause(e, t));
    }
}
 
Example #2
Source File: SortedMerge.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(T t) {
    try {
        queue.onNext(NotificationLite.next(t));
    } catch (MissingBackpressureException mbe) {
        try {
            onError(mbe);
        } finally {
            unsubscribe();
        }
        return;
    } catch (IllegalStateException ex) {
        if (!isUnsubscribed()) {
            try {
                onError(ex);
            } finally {
                unsubscribe();
            }
        }
        return;
    }
    parent.emit();
}
 
Example #3
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param o the value to buffer
 * @throws MissingBackpressureException
 *             if more onNext are sent than have been requested
 */
public void onNext(Object o) throws MissingBackpressureException {
    boolean iae = false;
    boolean mbe = false;
    synchronized (this) {
        Queue<Object> q = queue;
        if (q != null) {
            mbe = !q.offer(NotificationLite.next(o));
        } else {
            iae = true;
        }
    }

    if (iae) {
        throw new IllegalStateException("This instance has been unsubscribed and the queue is no longer usable.");
    }
    if (mbe) {
        throw new MissingBackpressureException();
    }
}
 
Example #4
Source File: OrderedMerge.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(T t) {
    if (done) {
        return;
    }
    try {
        queue.onNext(NotificationLite.next(t));
    } catch (MissingBackpressureException mbe) {
        try {
            onError(mbe);
        } finally {
            unsubscribe();
        }
        return;
    } catch (IllegalStateException ex) {
        if (!isUnsubscribed()) {
            try {
                onError(ex);
            } finally {
                unsubscribe();
            }
        }
        return;
    }
    parent.emit();
}
 
Example #5
Source File: OperatorGroupBy.java    From mantis with Apache License 2.0 6 votes vote down vote up
private void emitItem(GroupState<K, T> groupState, Object item) {
    Queue<Object> q = groupState.buffer;
    AtomicLong keyRequested = groupState.requested;
    REQUESTED.decrementAndGet(this);
    // short circuit buffering
    if (keyRequested != null && keyRequested.get() > 0 && (q == null || q.isEmpty())) {
        @SuppressWarnings("unchecked")
        Observer<Object> obs = (Observer<Object>) groupState.getObserver();
        NotificationLite.accept(obs, item);
        keyRequested.decrementAndGet();
    } else {
        q.add(item);
        BUFFERED_COUNT.incrementAndGet(this);

        if (groupState.count.getAndIncrement() == 0) {
            pollQueue(groupState);
        }
    }
    requestMoreIfNecessary();
}
 
Example #6
Source File: OperatorGroupBy.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Override
public void onCompleted() {
    if (TERMINATED_UPDATER.compareAndSet(this, 0, 1)) {
        // if we receive onCompleted from our parent we onComplete children
        // for each group check if it is ready to accept more events if so pass the oncomplete through else buffer it.
        for (GroupState<K, T> group : groups.values()) {
            emitItem(group, NotificationLite.completed());
        }

        // special case (no groups emitted ... or all unsubscribed)
        if (groups.size() == 0) {
            // we must track 'completionEmitted' seperately from 'completed' since `completeInner` can result in childObserver.onCompleted() being emitted
            if (COMPLETION_EMITTED_UPDATER.compareAndSet(this, 0, 1)) {
                child.onCompleted();
            }
        }
    }
}
 
Example #7
Source File: BufferUntilSubscriber.java    From mantis with Apache License 2.0 6 votes vote down vote up
private void emit(Object v) {
    synchronized (state.guard) {
        state.buffer.add(v);
        if (state.get() != null && !state.emitting) {
            // Have an observer and nobody is emitting,
            // should drain the `buffer`
            forward = true;
            state.emitting = true;
        }
    }
    if (forward) {
        Object o;
        while ((o = state.buffer.poll()) != null) {
            NotificationLite.accept(state.get(), o);
        }
        // Because `emit(Object v)` will be called in sequence,
        // no event will be put into `buffer` after we drain it.
    }
}
 
Example #8
Source File: OperatorGroupBy.java    From mantis with Apache License 2.0 6 votes vote down vote up
private void drainIfPossible(GroupState<K, T> groupState) {
    while (groupState.requested.get() > 0) {
        Object t = groupState.buffer.poll();
        if (t != null) {
            @SuppressWarnings("unchecked")
            Observer<Object> obs = (Observer<Object>) groupState.getObserver();
            NotificationLite.accept(obs, t);
            groupState.requested.decrementAndGet();
            BUFFERED_COUNT.decrementAndGet(this);

            // if we have used up all the events we requested from upstream then figure out what to ask for this time based on the empty space in the buffer
            requestMoreIfNecessary();
        } else {
            // queue is empty break
            break;
        }
    }
}
 
Example #9
Source File: BufferUntilSubscriber.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Override
public void call(final Subscriber<? super T> s) {
    if (state.casObserverRef(null, s)) {
        s.add(Subscriptions.create(new Action0() {
            @SuppressWarnings("unchecked")
            @Override
            public void call() {
                state.set(EMPTY_OBSERVER);
            }
        }));
        boolean win = false;
        synchronized (state.guard) {
            if (!state.emitting) {
                state.emitting = true;
                win = true;
            }
        }
        if (win) {
            while (true) {
                Object o;
                while ((o = state.buffer.poll()) != null) {
                    NotificationLite.accept(state.get(), o);
                }
                synchronized (state.guard) {
                    if (state.buffer.isEmpty()) {
                        // Although the buffer is empty, there is still a chance
                        // that further events may be put into the `buffer`.
                        // `emit(Object v)` should handle it.
                        state.emitting = false;
                        break;
                    }
                }
            }
        }
    } else {
        s.onError(new IllegalStateException("Only one subscriber allowed!"));
    }
}
 
Example #10
Source File: BufferOnBackPressureOperator.java    From mantis with Apache License 2.0 5 votes vote down vote up
private void drainIfPossible(final Subscriber<? super T> child,
                             AtomicLong requested,
                             AtomicInteger bufferedCount,
                             AtomicBoolean onCompleteReceived,
                             AtomicInteger completionEmitted
) {
    while (requested.get() > 0) {
        Object t = queue.poll();
        if (t != null) {
            NotificationLite.accept((Observer) child, t);
            requested.decrementAndGet();
            requestedGauge.decrement();
            bufferedCount.decrementAndGet();
            bufferedGauge.decrement();
            //		System.out.println("buffered count: " + bufferedGauge.value() + " next " + next.value())  ;
        } else {
            if (onCompleteReceived.get()) {
                if (completionEmitted.compareAndSet(0, 1)) {
                    child.onCompleted();
                    queue.clear();
                    bufferedGauge.set(0);
                }
            }
            // queue is empty break
            break;
        }
    }
}
 
Example #11
Source File: BufferUntilSubscriber.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(T t) {
    if (forward) {
        state.get().onNext(t);
    } else {
        emit(NotificationLite.next(t));
    }
}
 
Example #12
Source File: BufferUntilSubscriber.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable e) {
    if (forward) {
        state.get().onError(e);
    } else {
        emit(NotificationLite.error(e));
    }
}
 
Example #13
Source File: BufferUntilSubscriber.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Override
public void onCompleted() {
    if (forward) {
        state.get().onCompleted();
    } else {
        emit(NotificationLite.completed());
    }
}
 
Example #14
Source File: BufferOnBackPressureOperator.java    From mantis with Apache License 2.0 4 votes vote down vote up
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
    subscribe.increment();
    final AtomicLong requested = new AtomicLong();
    final AtomicInteger completionEmitted = new AtomicInteger();
    final AtomicInteger terminated = new AtomicInteger();

    final AtomicInteger bufferedCount = new AtomicInteger();
    final AtomicBoolean onCompleteReceived = new AtomicBoolean();

    final AtomicInteger wip = new AtomicInteger();
    child.add(Subscriptions.create(new Action0() {
        @Override
        public void call() {
            subscribe.decrement();
        }
    }));

    child.setProducer(new Producer() {

        @Override
        public void request(long n) {
            requested.getAndAdd(n);
            requestedGauge.increment(n);
            //         System.out.println("request: " + requested.get());
            pollQueue(child,
                    requested,

                    bufferedCount,
                    onCompleteReceived,
                    completionEmitted,
                    wip);
        }

    });

    Subscriber<T> parent = new Subscriber<T>() {
        @Override
        public void onStart() {
            request(Long.MAX_VALUE);
        }

        @Override
        public void onCompleted() {
            if (terminated.compareAndSet(0, 1)) {
                complete.increment();
                onCompleteReceived.set(true);
                pollQueue(child,
                        requested,

                        bufferedCount,
                        onCompleteReceived,
                        completionEmitted,
                        wip);
            }
        }

        @Override
        public void onError(Throwable e) {
            if (terminated.compareAndSet(0, 1)) {
                child.onError(e);
                error.increment();
                queue.clear();
            }
        }

        @Override
        public void onNext(T t) {
            emitItem(NotificationLite.next(t));
        }

        private void emitItem(Object item) {
            // short circuit buffering
            if (requested.get() > 0 && queue.isEmpty()) {
                NotificationLite.accept((Observer) child, item);
                requested.decrementAndGet();
                requestedGauge.decrement();
                next.increment();
                //		System.out.println("next count: " + next.value());
            } else {
                boolean success = queue.offer(item);
                if (success) {
                    bufferedCount.incrementAndGet();
                    bufferedGauge.increment();
                    //				System.out.println("buffered count: " + bufferedGauge.value());
                    drainIfPossible(child, requested, bufferedCount, onCompleteReceived, completionEmitted);

                } else {
                    dropped.increment();
                    //			System.out.println("dropped count: " + dropped.value());
                    // dropped
                }
            }
        }


    };
    // if child unsubscribes it should unsubscribe the parent, but not the other way around
    child.add(parent);
    return parent;
}
 
Example #15
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public void onCompleted() {
    // we ignore terminal events if we already have one
    if (terminalState == null) {
        terminalState = NotificationLite.completed();
    }
}
 
Example #16
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public void onError(Throwable t) {
    // we ignore terminal events if we already have one
    if (terminalState == null) {
        terminalState = NotificationLite.error(t);
    }
}
 
Example #17
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public boolean isCompleted(Object o) {
    return NotificationLite.isCompleted(o);
}
 
Example #18
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public boolean isError(Object o) {
    return NotificationLite.isError(o);
}
 
Example #19
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public Object getValue(Object o) {
    return NotificationLite.getValue(o);
}
 
Example #20
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean accept(Object o, Observer child) {
    return NotificationLite.accept(child, o);
}
 
Example #21
Source File: RxRingBuffer.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
public Throwable asError(Object o) {
    return NotificationLite.getError(o);
}