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

io.datakernel.csp.queue.ChannelQueue Maven / Gradle / Ivy

Go to download

Communicating sequential process via channels, similar to Golang's channels. A channel could be imagine as a pipe which connects some processes.

The newest version!
package io.datakernel.csp.queue;

import io.datakernel.async.process.Cancellable;
import io.datakernel.csp.AbstractChannelConsumer;
import io.datakernel.csp.AbstractChannelSupplier;
import io.datakernel.csp.ChannelConsumer;
import io.datakernel.csp.ChannelSupplier;
import io.datakernel.promise.Promise;
import org.jetbrains.annotations.Nullable;

/**
 * Represents a queue of elements, which you can {@code put}
 * or {@code take} to pass from {@link ChannelConsumer} to
 * {@link ChannelSupplier}.
 *
 * @param  type of values stored in the queue
 */
public interface ChannelQueue extends Cancellable {
	/**
	 * Puts a value in the queue and returns a
	 * {@code promise} of {@code null} as a marker of completion.
	 *
	 * @param value a value passed to the queue
	 * @return {@code promise} of {@code null} as a marker of completion
	 */
	Promise put(@Nullable T value);

	/**
	 * Takes an element of this queue and wraps it in {@code promise}.
	 *
	 * @return a {@code promise} of value from the queue
	 */
	Promise take();

	/**
	 * Returns a {@code ChannelConsumer} which puts value in
	 * this queue when {@code accept(T value)} is called.
	 *
	 * @return a {@code ChannelConsumer} for this queue
	 */
	default ChannelConsumer getConsumer() {
		return new AbstractChannelConsumer(this) {
			@Override
			protected Promise doAccept(T value) {
				return put(value);
			}
		};
	}

	/**
	 * Returns a {@code ChannelConsumer} which puts non-null
	 * value in this queue when {@code accept(T value)} is
	 * called. Otherwise, puts {@code null} and waits for the
	 * {@code acknowledgement} completion.
	 * 

* This method is useful if you need to control the situations * when there are no more elements to be accepted (for example, * get a {@code ChannelSupplier} in such case). * * @param acknowledgement a promise which will work when * a {@code null} value is passed * @return a ChannelConsumer with custom behaviour in case a * {@code null} value is accepted */ default ChannelConsumer getConsumer(Promise acknowledgement) { return new AbstractChannelConsumer(this) { @Override protected Promise doAccept(T value) { if (value != null) return put(value); return put(null).both(acknowledgement); } }; } /** * Returns a {@link ChannelSupplier} which gets value from this * queue wrapped in {@code Promise} when {@code get()} is called. * * @return a ChannelSupplier which takes values from this queue */ default ChannelSupplier getSupplier() { return new AbstractChannelSupplier(this) { @Override protected Promise doGet() { return take(); } }; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy