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

com.github.davidmoten.rx.SingleSubscribeSubject Maven / Gradle / Ivy

There is a newer version: 0.7.19
Show newest version
package com.github.davidmoten.rx;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;

public final class SingleSubscribeSubject extends Observable implements
		Observer {

    //Visible for testing
    static final String ONLY_ONE_SUBSCRIPTION_IS_ALLOWED = "only one subscription is allowed";
    
	private final SingleSubscribeOnSubscribe subscriberHolder;

	private SingleSubscribeSubject(SingleSubscribeOnSubscribe onSubscribe) {
		super(onSubscribe);
		subscriberHolder = onSubscribe;
	}

	private SingleSubscribeSubject() {
		this(new SingleSubscribeOnSubscribe());
	}

	public static  SingleSubscribeSubject create(){
	    return new SingleSubscribeSubject();
	}

	@Override
	public void onCompleted() {
		if (subscriberHolder.subscriber != null) {
			subscriberHolder.subscriber.onCompleted();
		}
	}

	@Override
	public void onError(Throwable e) {
		if (subscriberHolder.subscriber != null)
			subscriberHolder.subscriber.onError(e);
	}

	@Override
	public void onNext(T t) {
		if (subscriberHolder.subscriber != null)
			subscriberHolder.subscriber.onNext(t);
	}

	private static class SingleSubscribeOnSubscribe implements
			OnSubscribe {

		
        volatile Subscriber subscriber;

		@Override
		public void call(Subscriber subscriber) {
			if (this.subscriber != null)
				throw new RuntimeException(ONLY_ONE_SUBSCRIPTION_IS_ALLOWED);
			this.subscriber = subscriber;
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy