Class ReferencedObject<T>

java.lang.Object
com.github.cafapi.common.util.ref.ReferencedObject<T>
Type Parameters:
T - the type of the wrapped, referenced object

public final class ReferencedObject<T> extends Object
Utility wrapper for allowing data to potentially be within a message or located on a remote DataStore. The acquire(ObjectSource) method allows transparent method of obtaining the wrapped data, which will only be retrieved the first time acquire is called (if it is not already present). It should be noted that because this class returns a fully formed Java object instance, the entire instance must be loaded into memory. As such, this wrapper should only be used for objects that are at most a few megabytes of data.
  • Method Details

    • acquire

      public T acquire(DataSource source) throws DataSourceException
      Return the referenced object, potentially performing a remote DataStore lookup and deserialisation. If the object is already present or has been previously acquired, it is immediately returned.
      Parameters:
      source - the implementation that provides object instances given the provided references
      Returns:
      the object that this container is wrapping
      Throws:
      DataSourceException - if the object cannot be acquired
      IllegalStateException - if there is no object or reference present
    • getReference

      public String getReference()
      Returns:
      the remote reference to the object in the DataStore, if set
    • getReferencedObject

      public static <T> ReferencedObject<T> getReferencedObject(Class<T> clazz, String ref)
      Create a ReferencedObject that uses a remote reference to data present in an ObjectSource.
      Type Parameters:
      T - the type of the referenced object
      Parameters:
      clazz - the class of the referenced object
      ref - the reference to be interpreted by the DataStore
      Returns:
      a new ReferencedObject instance that relates to a remote object via reference
    • getWrappedObject

      public static <T> ReferencedObject<T> getWrappedObject(Class<T> clazz, T obj)
      Create a ReferencedObject that directly wraps an object without a reference.
      Type Parameters:
      T - the type of the object
      Parameters:
      clazz - the class of the object
      obj - the object to wrapper
      Returns:
      a new ReferencedObject instance that directly wraps the object specified