public class InterruptingImmediateSupplier<T> extends java.lang.Object implements ImmediateSupplier<T>, DeferredSupplier<T>
Supplier
as an ImmediateSupplier
by interrupting the thread before calling Supplier#get()
.
If the call succeeds, the result is returned.
If the call throws any trace including an InterruptedException
or RuntimeInterruptedException
(ie the call failed due to the interruption, typically because it tried to wait)
then this class concludes that there is no value available immediately and returns Maybe.absent()
.
If the call throws any other error, that is returned.
The interruption is cleared afterwards (unless the thread was interrupted when the method was entered).
Note that some "immediate" methods, such as Semaphore.acquire()
when a semaphore is available,
will throw if the thread is interrupted. Typically there are workarounds, for instance:
if (semaphore.tryAcquire()) semaphore.acquire();
.
Modifier and Type | Class and Description |
---|---|
static class |
InterruptingImmediateSupplier.InterruptingImmediateSupplierNotSupportedForObject |
ImmediateSupplier.ImmediateUnsupportedException, ImmediateSupplier.ImmediateValueNotAvailableException
Constructor and Description |
---|
InterruptingImmediateSupplier(<any> nestedSupplier) |
Modifier and Type | Method and Description |
---|---|
T |
get() |
Maybe<T> |
getImmediately()
Gets the value promptly, or returns
Maybe.absent() if the value requires blocking,
or throws ImmediateSupplier.ImmediateUnsupportedException if it cannot be determined whether the value requires blocking or not. |
static <T> InterruptingImmediateSupplier<T> |
of(java.lang.Object o) |
static <T> ReferenceWithError<InterruptingImmediateSupplier<T>> |
ofSafe(java.lang.Object o) |
public InterruptingImmediateSupplier(<any> nestedSupplier)
public Maybe<T> getImmediately()
ImmediateSupplier
Maybe.absent()
if the value requires blocking,
or throws ImmediateSupplier.ImmediateUnsupportedException
if it cannot be determined whether the value requires blocking or not.
The Maybe.absent()
returned here indicates that a value definitively is pending, just it is not yet available,
and an attempt to Maybe.get()
it should throw an ImmediateSupplier.ImmediateValueNotAvailableException
;
it can be created with ImmediateSupplier.ImmediateValueNotAvailableException.newAbsentWithExceptionSupplier()
to
avoid creating traces (or simply with Maybe.absent(new ImmediateValueNotAvailableException(...))
).
This is in contrast with this method throwing a ImmediateSupplier.ImmediateUnsupportedException
which should be done
if the presence of an eventual value cannot even be determined in a non-blocking way.
Implementations of this method should typically catch the former exception if encountered and return a
Maybe.absent()
wrapping it, whereas ImmediateSupplier.ImmediateUnsupportedException
instances should be propagated.
getImmediately
in interface ImmediateSupplier<T>
public T get()
get
in interface DeferredSupplier<T>
public static <T> InterruptingImmediateSupplier<T> of(java.lang.Object o)
public static <T> ReferenceWithError<InterruptingImmediateSupplier<T>> ofSafe(java.lang.Object o)