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

hu.akarnokd.rxjava2.internal.operators.OperatorSamplePublisher Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and 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 hu.akarnokd.rxjava2.internal.operators;

import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;

public final class OperatorSamplePublisher implements Operator {
    final Publisher other;
    
    public OperatorSamplePublisher(Publisher other) {
        this.other = other;
    }
    
    
    @Override
    public Subscriber apply(Subscriber t) {
        SerializedSubscriber serial = new SerializedSubscriber(t);
        return new SamplePublisherSubscriber(serial, other);
    }
    
    static final class SamplePublisherSubscriber extends AtomicReference implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = -3517602651313910099L;

        final Subscriber actual;
        final Publisher sampler;
        
        volatile long requested;
        @SuppressWarnings("rawtypes")
        static final AtomicLongFieldUpdater REQUESTED =
                AtomicLongFieldUpdater.newUpdater(SamplePublisherSubscriber.class, "requested");

        volatile Subscription other;
        @SuppressWarnings("rawtypes")
        static final AtomicReferenceFieldUpdater OTHER =
                AtomicReferenceFieldUpdater.newUpdater(SamplePublisherSubscriber.class, Subscription.class, "other");
        
        static final Subscription CANCELLED = new Subscription() {
            @Override
            public void request(long n) {
                
            }
            
            @Override
            public void cancel() {
                
            }
        };
        
        Subscription s;
        
        public SamplePublisherSubscriber(Subscriber actual, Publisher other) {
            this.actual = actual;
            this.sampler = other;
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            
            this.s = s;
            actual.onSubscribe(this);
            if (other == null) {
                sampler.subscribe(new SamplerSubscriber(this));
                s.request(Long.MAX_VALUE);
            }
            
        }
        
        @Override
        public void onNext(T t) {
            lazySet(t);
        }
        
        @Override
        public void onError(Throwable t) {
            cancelOther();
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            cancelOther();
            actual.onComplete();
        }
        
        void cancelOther() {
            Subscription o = other;
            if (o != CANCELLED) {
                o = OTHER.getAndSet(this, CANCELLED);
                if (o != CANCELLED && o != null) {
                    o.cancel();
                }
            }
        }
        
        boolean setOther(Subscription o) {
            if (other == null) {
                if (OTHER.compareAndSet(this, null, o)) {
                    return true;
                }
                o.cancel();
            }
            return false;
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            
            BackpressureHelper.add(REQUESTED, this, n);
        }
        
        @Override
        public void cancel() {
            cancelOther();
            s.cancel();
        }
        
        public void error(Throwable e) {
            cancel();
            actual.onError(e);
        }
        
        public void complete() {
            cancel();
            actual.onComplete();
        }
        
        public void emit() {
            T value = getAndSet(null);
            if (value != null) {
                long r = requested;
                if (r != 0L) {
                    actual.onNext(value);
                    if (r != Long.MAX_VALUE) {
                        REQUESTED.decrementAndGet(this);
                    }
                } else {
                    cancel();
                    actual.onError(new IllegalStateException("Couldn't emit value due to lack of requests!"));
                }
            }
        }
    }
    
    static final class SamplerSubscriber implements Subscriber {
        final SamplePublisherSubscriber parent;
        public SamplerSubscriber(SamplePublisherSubscriber parent) {
            this.parent = parent;
            
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (parent.setOther(s)) {
                s.request(Long.MAX_VALUE);
            }
        }
        
        @Override
        public void onNext(Object t) {
            parent.emit();
        }
        
        @Override
        public void onError(Throwable t) {
            parent.error(t);
        }
        
        @Override
        public void onComplete() {
            parent.complete();
        }
    }
}