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

rx.subjects.TestSubject 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.subjects;

import java.util.concurrent.TimeUnit;

import rx.Observer;
import rx.Scheduler;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
import rx.schedulers.TestScheduler;
import rx.subjects.SubjectSubscriptionManager.SubjectObserver;

/**
 * Subject that, once an {@link Observer} has subscribed, publishes all subsequent events to the subscriber.
 * 

* *

* Example usage: *

*

 {@code

  PublishSubject subject = PublishSubject.create();
  // observer1 will receive all onNext and onCompleted events
  subject.subscribe(observer1);
  subject.onNext("one");
  subject.onNext("two");
  // observer2 will only receive "three" and onCompleted
  subject.subscribe(observer2);
  subject.onNext("three");
  subject.onCompleted();

  } 
 * 
 * @param 
 *          the type of item observed by and emitted by the subject
 * @warn javadoc seems misleading
 */
public final class TestSubject extends Subject {

    /**
     * @warn javadoc missing
     * @return
     */
    public static  TestSubject create(TestScheduler scheduler) {
        final SubjectSubscriptionManager state = new SubjectSubscriptionManager();

        state.onAdded = new Action1>() {

            @Override
            public void call(SubjectObserver o) {
                o.emitFirst(state.get(), state.nl);
            }
            
        };
        state.onTerminated = state.onAdded;

        return new TestSubject(state, state, scheduler);
    }

    private final SubjectSubscriptionManager state;
    private final Scheduler.Worker innerScheduler;

    protected TestSubject(OnSubscribe onSubscribe, SubjectSubscriptionManager state, TestScheduler scheduler) {
        super(onSubscribe);
        this.state = state;
        this.innerScheduler = scheduler.createWorker();
    }

    @Override
    public void onCompleted() {
        onCompleted(innerScheduler.now());
    }

    private void _onCompleted() {
        if (state.active) {
            for (SubjectObserver bo : state.terminate(NotificationLite.instance().completed())) {
                bo.onCompleted();
            }
        }
    }

    /**
     * @warn javadoc missing
     * @param timeInMilliseconds
     */
    public void onCompleted(long timeInMilliseconds) {
        innerScheduler.schedule(new Action0() {

            @Override
            public void call() {
                _onCompleted();
            }

        }, timeInMilliseconds, TimeUnit.MILLISECONDS);
    }

    @Override
    public void onError(final Throwable e) {
        onError(e, innerScheduler.now());
    }

    private void _onError(final Throwable e) {
        if (state.active) {
            for (SubjectObserver bo : state.terminate(NotificationLite.instance().error(e))) {
                bo.onError(e);
            }
        }
    }

    /**
     * @warn javadoc missing
     * @param e
     * @param timeInMilliseconds
     */
    public void onError(final Throwable e, long timeInMilliseconds) {
        innerScheduler.schedule(new Action0() {

            @Override
            public void call() {
                _onError(e);
            }

        }, timeInMilliseconds, TimeUnit.MILLISECONDS);
    }

    @Override
    public void onNext(T v) {
        onNext(v, innerScheduler.now());
    }

    private void _onNext(T v) {
        for (Observer o : state.observers()) {
            o.onNext(v);
        }
    }

    /**
     * @warn javadoc missing
     * @param v
     * @param timeInMilliseconds
     */
    public void onNext(final T v, long timeInMilliseconds) {
        innerScheduler.schedule(new Action0() {

            @Override
            public void call() {
                _onNext(v);
            }

        }, timeInMilliseconds, TimeUnit.MILLISECONDS);
    }
}