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

rx.joins.JoinObserver1 Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show 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.joins;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;

import rx.Notification;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.observers.SafeSubscriber;

/**
 * Default implementation of a join observer.
 */
public final class JoinObserver1 extends Subscriber> implements JoinObserver {
    private Object gate;
    private final Observable source;
    private final Action1 onError;
    private final List activePlans;
    private final Queue> queue;
    private final AtomicBoolean subscribed = new AtomicBoolean(false);
    private final SafeSubscriber> safeObserver;

    public JoinObserver1(Observable source, Action1 onError) {
        this.source = source;
        this.onError = onError;
        queue = new LinkedList>();
        activePlans = new ArrayList();
        safeObserver = new SafeSubscriber>(new InnerObserver());
        // add this subscription so it gets unsubscribed when the parent does
        add(safeObserver);
    }

    public Queue> queue() {
        return queue;
    }

    public void addActivePlan(ActivePlan0 activePlan) {
        activePlans.add(activePlan);
    }

    @Override
    public void subscribe(Object gate) {
        if (subscribed.compareAndSet(false, true)) {
            this.gate = gate;
            source.materialize().subscribe(this);
        } else {
            throw new IllegalStateException("Can only be subscribed to once.");
        }
    }

    @Override
    public void dequeue() {
        queue.remove();
    }


    @Override
    public void onNext(Notification args) {
        safeObserver.onNext(args);
    }

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

    @Override
    public void onCompleted() {
        safeObserver.onCompleted();
    }

    void removeActivePlan(ActivePlan0 activePlan) {
        activePlans.remove(activePlan);
        if (activePlans.isEmpty()) {
            unsubscribe();
        }
    }
    
    
    private final class InnerObserver extends Subscriber> {

        @Override
        public void onNext(Notification args) {
            synchronized (gate) {
                if (!isUnsubscribed()) {
                    if (args.isOnError()) {
                        onError.call(args.getThrowable());
                        return;
                    }
                    queue.add(args);

                    // remark: activePlans might change while iterating
                    for (ActivePlan0 a : new ArrayList(activePlans)) {
                        a.match();
                    }
                }
            }
        }

        @Override
        public void onError(Throwable e) {
            // not expected
        }

        @Override
        public void onCompleted() {
            // not expected or ignored
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy