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

hu.akarnokd.rxjava2.internal.operators.OperatorTakeUntil 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.AtomicBoolean;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.internal.disposables.ArrayCompositeResource;
import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;

public final class OperatorTakeUntil implements Operator {
    final Publisher other;
    public OperatorTakeUntil(Publisher other) {
        this.other = other;
    }
    @Override
    public Subscriber apply(Subscriber child) {
        final SerializedSubscriber serial = new SerializedSubscriber(child);
        
        final ArrayCompositeResource frc = new ArrayCompositeResource(2, SubscriptionHelper.consumeAndCancel());
        
        final TakeUntilSubscriber tus = new TakeUntilSubscriber(serial, frc); 
        
        other.subscribe(new Subscriber() {
            @Override
            public void onSubscribe(Subscription s) {
                if (frc.setResource(1, s)) {
                    s.request(Long.MAX_VALUE);
                }
            }
            @Override
            public void onNext(U t) {
                frc.dispose();
                if (tus.compareAndSet(false, true)) {
                    EmptySubscription.complete(serial);
                } else {
                    serial.onComplete();
                }
            }
            @Override
            public void onError(Throwable t) {
                frc.dispose();
                if (tus.compareAndSet(false, true)) {
                    EmptySubscription.error(t, serial);
                } else {
                    serial.onError(t);
                }
            }
            @Override
            public void onComplete() {
                frc.dispose();
                if (tus.compareAndSet(false, true)) {
                    EmptySubscription.complete(serial);
                } else {
                    serial.onComplete();
                }
            }
        });
        
        return tus;
    }
    
    static final class TakeUntilSubscriber extends AtomicBoolean implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = 3451719290311127173L;
        final Subscriber actual;
        final ArrayCompositeResource frc;
        
        Subscription s;
        
        public TakeUntilSubscriber(Subscriber actual, ArrayCompositeResource frc) {
            this.actual = actual;
            this.frc = frc;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            if (frc.setResource(0, s)) {
                if (compareAndSet(false, true)) {
                    actual.onSubscribe(this);
                }
            }
        }
        
        @Override
        public void onNext(T t) {
            actual.onNext(t);
        }
        
        @Override
        public void onError(Throwable t) {
            frc.dispose();
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            frc.dispose();
            actual.onComplete();
        }
        
        @Override
        public void request(long n) {
            s.request(n);
        }
        
        @Override
        public void cancel() {
            frc.dispose();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy