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

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

/**
 * 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.AtomicLong;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.functions.Consumer;
import hu.akarnokd.rxjava2.internal.functions.Functions;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;

public final class OperatorOnBackpressureDrop implements Operator {
    
    private static final OperatorOnBackpressureDrop DEFAULT =
            new OperatorOnBackpressureDrop(Functions.emptyConsumer());
    
    @SuppressWarnings("unchecked")
    public static  OperatorOnBackpressureDrop instance() {
        return (OperatorOnBackpressureDrop)DEFAULT;
    }
    
    private final Consumer onDrop;
    
    public OperatorOnBackpressureDrop(Consumer onDrop) {
        this.onDrop = onDrop;
    }
    
    @Override
    public Subscriber apply(Subscriber t) {
        return new BackpressureDropSubscriber(t, onDrop);
    }
    
    static final class BackpressureDropSubscriber extends AtomicLong implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = -6246093802440953054L;
        
        final Subscriber actual;
        final Consumer onDrop;
        
        Subscription s;
        
        boolean done;
        
        public BackpressureDropSubscriber(Subscriber actual, Consumer onDrop) {
            this.actual = actual;
            this.onDrop = onDrop;
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            actual.onSubscribe(this);
            s.request(Long.MAX_VALUE);
        }
        
        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            long r = get();
            if (r != 0L) {
                actual.onNext(t);
                if (r != Long.MAX_VALUE) {
                    decrementAndGet();
                }
            } else {
                try {
                    onDrop.accept(t);
                } catch (Throwable e) {
                    done = true;
                    cancel();
                    actual.onError(e);
                }
            }
        }
        
        @Override
        public void onError(Throwable t) {
            if (done) {
                return;
            }
            done = true;
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            actual.onComplete();
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            BackpressureHelper.add(this, n);
        }
        
        @Override
        public void cancel() {
            s.cancel();
        }
    }
}