public class

Promise

extends Object
java.lang.Object
   ↳ net.schmizz.concurrent.Promise<V, T extends java.lang.Throwable>

Class Overview

Represents promised data of the parameterized type V and allows waiting on it. An exception may also be delivered to a waiter, and will be of the parameterized type T.

For atomic operations on a promise, e.g. checking if a value is delivered and if it is not then setting it, the associated lock for the promise should be acquired while doing so.

Summary

Fields
private final ExceptionChainer<T extends Throwable> chainer
private final Condition cond
private final ReentrantLock lock
private final Logger log
private final String name
private T extends Throwable pendingEx
private V val
Public Constructors
Promise(String name, ExceptionChainer<T> chainer)
Creates this promise with given name and exception chainer.
Promise(String name, ExceptionChainer<T> chainer, ReentrantLock lock)
Creates this promise with given name, exception chainer, and associated lock.
Public Methods
void clear()
Clears this promise by setting its value and queued exception to null.
void deliver(V val)
Set this promise's value to val.
void deliverError(Throwable e)
Queues error that will be thrown in any waiting thread or any thread that attempts to wait on this promise hereafter.
boolean hasWaiters()
boolean inError()
boolean isDelivered()
void lock()
Acquire the lock associated with this promise.
V retrieve()
Wait indefinitely for this promise's value to be deliver.
V retrieve(long timeout, TimeUnit unit)
Wait for timeout duration for this promise's value to be deliver.
String toString()
V tryRetrieve(long timeout, TimeUnit unit)
Wait for timeout duration for this promise's value to be deliver.
void unlock()
Release the lock associated with this promise.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

private final ExceptionChainer<T extends Throwable> chainer

private final Condition cond

private final ReentrantLock lock

private final Logger log

private final String name

private T extends Throwable pendingEx

private V val

Public Constructors

public Promise (String name, ExceptionChainer<T> chainer)

Creates this promise with given name and exception chainer. Allocates a new java.util.concurrent.locks.Lock lock object for this promise.

Parameters
name Name of this promise
chainer ExceptionChainer that will be used for chaining exceptions

public Promise (String name, ExceptionChainer<T> chainer, ReentrantLock lock)

Creates this promise with given name, exception chainer, and associated lock.

Parameters
name Name of this promise
chainer ExceptionChainer that will be used for chaining exceptions
lock Lock to use

Public Methods

public void clear ()

Clears this promise by setting its value and queued exception to null.

public void deliver (V val)

Set this promise's value to val. Any waiters will be delivered this value.

Parameters
val The value

public void deliverError (Throwable e)

Queues error that will be thrown in any waiting thread or any thread that attempts to wait on this promise hereafter.

Parameters
e The error

public boolean hasWaiters ()

Returns
  • whether this promise has threads waiting on it.

public boolean inError ()

Returns
  • whether this promise has been delivered an error.

public boolean isDelivered ()

Returns
  • whether this promise has a value delivered, and no error waiting to pop.

public void lock ()

Acquire the lock associated with this promise.

public V retrieve ()

Wait indefinitely for this promise's value to be deliver.

Returns
  • the value
Throws
Throwable

public V retrieve (long timeout, TimeUnit unit)

Wait for timeout duration for this promise's value to be deliver.

Parameters
timeout The timeout
unit Time unit for the timeout
Returns
  • the value
Throws
Throwable

public String toString ()

public V tryRetrieve (long timeout, TimeUnit unit)

Wait for timeout duration for this promise's value to be deliver.

If the value is not deliver by the time the timeout expires, returns null.

Parameters
timeout The timeout
unit Time unit for the timeout
Returns
  • the value or null
Throws
Throwable

public void unlock ()

Release the lock associated with this promise.