All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.internal.operators.OperatorOnBackpressureLatest Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package rx.internal.operators;

import java.util.concurrent.atomic.*;

import rx.*;
import rx.Observable.Operator;

/**
 * An operator which drops all but the last received value in case the downstream
 * doesn't request more.
 * @param  the value type
 */
public final class OperatorOnBackpressureLatest implements Operator {
    /** Holds a singleton instance initialized on class-loading. */
    static final class Holder {
        static final OperatorOnBackpressureLatest INSTANCE = new OperatorOnBackpressureLatest();
    }

    /**
     * Returns a singleton instance of the OnBackpressureLatest operator since it is stateless.
     * @param  the value type
     * @return the single instanceof OperatorOnBackpressureLatest
     */
    @SuppressWarnings("unchecked")
    public static  OperatorOnBackpressureLatest instance() {
        return (OperatorOnBackpressureLatest)Holder.INSTANCE;
    }

    @Override
    public Subscriber call(Subscriber child) {
        final LatestEmitter producer = new LatestEmitter(child);
        LatestSubscriber parent = new LatestSubscriber(producer);
        producer.parent = parent;
        child.add(parent);
        child.add(producer);
        child.setProducer(producer);
        return parent;
    }
    /**
     * A terminable producer which emits the latest items on request.
     * @param 
     */
    static final class LatestEmitter extends AtomicLong implements Producer, Subscription, Observer {
        /** */
        private static final long serialVersionUID = -1364393685005146274L;
        final Subscriber child;
        LatestSubscriber parent;
        final AtomicReference value;
        /** Written before done, read after done. */
        Throwable terminal;
        volatile boolean done;
        /** Guarded by this. */
        boolean emitting;
        /** Guarded by this. */
        boolean missed;
        static final Object EMPTY = new Object();
        static final long NOT_REQUESTED = Long.MIN_VALUE / 2;
        public LatestEmitter(Subscriber child) {
            this.child = child;
            this.value = new AtomicReference(EMPTY);
            this.lazySet(NOT_REQUESTED); // not
        }
        @Override
        public void request(long n) {
            if (n >= 0) {
                for (;;) {
                    long r = get();
                    if (r == Long.MIN_VALUE) {
                        return;
                    }
                    long u;
                    if (r == NOT_REQUESTED) {
                        u = n;
                    } else {
                        u = r + n;
                        if (u < 0) {
                            u = Long.MAX_VALUE;
                        }
                    }
                    if (compareAndSet(r, u)) {
                        if (r == NOT_REQUESTED) {
                            parent.requestMore(Long.MAX_VALUE);
                        }
                        emit();
                        return;
                    }
                }
            }
        }
        long produced(long n) {
            for (;;) {
                long r = get();
                if (r < 0) {
                    return r;
                }
                long u = r - n;
                if (compareAndSet(r, u)) {
                    return u;
                }
            }
        }
        @Override
        public boolean isUnsubscribed() {
            return get() == Long.MIN_VALUE;
        }
        @Override
        public void unsubscribe() {
            if (get() >= 0) {
                getAndSet(Long.MIN_VALUE);
            }
        }

        @Override
        public void onNext(T t) {
            value.lazySet(t); // emit's synchronized block does a full release
            emit();
        }
        @Override
        public void onError(Throwable e) {
            terminal = e;
            done = true;
            emit();
        }
        @Override
        public void onCompleted() {
            done = true;
            emit();
        }
        void emit() {
            synchronized (this) {
                if (emitting) {
                    missed = true;
                    return;
                }
                emitting = true;
                missed = false;
            }
            boolean skipFinal = false;
            try {
                for (;;) {
                    long r = get();
                    if (r == Long.MIN_VALUE) {
                        skipFinal = true;
                        break;
                    }
                    Object v = value.get();
                    if (r > 0 && v != EMPTY) {
                        @SuppressWarnings("unchecked")
                        T v2 = (T)v;
                        child.onNext(v2);
                        value.compareAndSet(v, EMPTY);
                        produced(1);
                        v = EMPTY;
                    }
                    if (v == EMPTY && done) {
                        Throwable e = terminal;
                        if (e != null) {
                            child.onError(e);
                        } else {
                            child.onCompleted();
                        }
                    }
                    synchronized (this) {
                        if (!missed) {
                            emitting = false;
                            skipFinal = true;
                            break;
                        }
                        missed = false;
                    }
                }
            } finally {
                if (!skipFinal) {
                    synchronized (this) {
                        emitting = false;
                    }
                }
            }
        }
    }
    static final class LatestSubscriber extends Subscriber {
        private final LatestEmitter producer;

        LatestSubscriber(LatestEmitter producer) {
            this.producer = producer;
        }

        @Override
        public void onStart() {
            // don't run until the child actually requested to avoid synchronous problems
            request(0);
        }

        @Override
        public void onNext(T t) {
            producer.onNext(t);
        }

        @Override
        public void onError(Throwable e) {
            producer.onError(e);
        }

        @Override
        public void onCompleted() {
            producer.onCompleted();
        }
        void requestMore(long n) {
            request(n);
        }
    }
}