Interface Send<T extends Resource<T>>

  • 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 a Resource, 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 Detail

      • sending

        static <T extends Resource<T>> Send<T> sending​(Class<T> concreteObjectType,
                                                       Supplier<? extends T> supplier)
        Construct a Send based on the given Supplier. The supplier will be called only once, in the receiving thread.
        Type Parameters:
        T - The type of object being sent.
        concreteObjectType - The concrete type of the object being sent. Specifically, the object returned from the Supplier.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.
        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 a Send from which an object of the given type can be received.
        type - The type of object we wish to receive.
        candidate - The candidate object that might be a Send of an object of the given type.
        true if the candidate object is a Send that would deliver an object of the given type, otherwise false.
      • receive

        T receive()
        Receive the Resource 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.

        The sent resource instance, never null.
        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.
        type - The result type of the mapping function.
        mapper - The mapping function to apply to the object being sent.
        A new Send instance that will deliver an object that is the result of the mapping.
      • close

        void close()
        Discard this Send and the object it contains. This has no effect if the send-object has already been received.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface SafeCloseable
      • referentIsInstanceOf

        boolean referentIsInstanceOf​(Class<?> cls)
        Determine if the object received from this Send is an instance of the given class.
        cls - The type to check.
        true if the object received from this Send can be assigned fields or variables of the given type, otherwise false.