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

com.github.tix320.kiwi.api.reactive.publisher.SinglePublisher Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package com.github.tix320.kiwi.api.reactive.publisher;

import java.util.Collection;

import com.github.tix320.kiwi.internal.reactive.publisher.BasePublisher;

public final class SinglePublisher extends BasePublisher {

	private volatile T value;

	public SinglePublisher() {

	}

	public SinglePublisher(T initialValue) {
		this.value = validateValue(initialValue);
	}

	@Override
	protected boolean onSubscribe(InternalSubscription subscription) {
		if (value == null) {
			return true;
		}
		else {
			return subscription.onPublish(value);
		}
	}

	@Override
	public void publish(T object) {
		runInLock(() -> {
			failIfCompleted();
			value = validateValue(object);
			Collection subscriptions = getSubscriptionsCopy();
			for (InternalSubscription subscription : subscriptions) {
				try {
					boolean needMore = subscription.onPublish(object);
					if (!needMore) {
						subscription.unsubscribe();
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void publish(T[] objects) {
		throw new UnsupportedOperationException("Single publisher must publish only one value at once");
	}

	@Override
	public void publish(Iterable iterable) {
		throw new UnsupportedOperationException("Single publisher must publish only one value at once");
	}

	public T getValue() {
		return value;
	}

	private T validateValue(T value) {
		if (value == null) {
			throw new NullPointerException("Value in SinglePublisher cannot be null");
		}
		return value;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy