-
- Type Parameters:
T
-
- All Superinterfaces:
AutoCloseable
,SafeCloseable
- All Known Implementing Classes:
SendFromOwned
,SendFromSupplier
public interface Send<T extends Resource<T>> extends SafeCloseable
A temporary holder of aResource
, used for transferring the ownership of the resource from one thread to another.Prior to the
Send
being created, the originating resource is invalidated, to prevent access while it is being sent. This means it cannot be accessed, closed, or disposed of, while it is in-flight. Once the resource is received, the new ownership is established.Care must be taken to ensure that the resource is always received by some thread. Failure to do so can result in a resource leak.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
close()
Discard thisSend
and the object it contains.static boolean
isSendOf(Class<?> type, Object candidate)
Determine if the given candidate object is an instance of aSend
from which an object of the given type can be received.default <R extends Resource<R>>
Send<R>map(Class<R> type, Function<T,R> mapper)
Apply a mapping function to the object being sent.T
receive()
Receive theResource
instance being sent, and bind its ownership to the calling thread.boolean
referentIsInstanceOf(Class<?> cls)
Determine if the object received from thisSend
is an instance of the given class.static <T extends Resource<T>>
Send<T>sending(Class<T> concreteObjectType, Supplier<? extends T> supplier)
-
-
-
Method Detail
-
sending
static <T extends Resource<T>> Send<T> sending(Class<T> concreteObjectType, Supplier<? extends T> supplier)
Construct aSend
based on the givenSupplier
. The supplier will be called only once, in the receiving thread.- Type Parameters:
T
- The type of object being sent.- Parameters:
concreteObjectType
- The concrete type of the object being sent. Specifically, the object returned from theSupplier.get()
method must be an instance of this class.supplier
- The supplier of the object being sent, which will be called when the object is ready to be received.- Returns:
- A
Send
which will deliver an object of the given type, from the supplier.
-
isSendOf
static boolean isSendOf(Class<?> type, Object candidate)
Determine if the given candidate object is an instance of aSend
from which an object of the given type can be received.
-
receive
T receive()
Receive theResource
instance being sent, and bind its ownership to the calling thread. The invalidation of the sent resource in the sending thread happens-before the return of this method.This method can only be called once, and will throw otherwise.
- Returns:
- The sent resource instance, never
null
. - Throws:
IllegalStateException
- If this method is called more than once.
-
map
default <R extends Resource<R>> Send<R> map(Class<R> type, Function<T,R> mapper)
Apply a mapping function to the object being sent. The mapping will occur when the object is received.- Type Parameters:
R
- The result type of the mapping function.- Parameters:
type
- The result type of the mapping function.mapper
- The mapping function to apply to the object being sent.- Returns:
- A new
Send
instance that will deliver an object that is the result of the mapping.
-
close
void close()
Discard thisSend
and the object it contains. This has no effect if the send-object has already been received.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSafeCloseable
-
referentIsInstanceOf
boolean referentIsInstanceOf(Class<?> cls)
Determine if the object received from thisSend
is an instance of the given class.- Parameters:
cls
- The type to check.- Returns:
true
if the object received from thisSend
can be assigned fields or variables of the given type, otherwise false.
-
-