Module jtrim.query

Class RefCachedData<DataType>

java.lang.Object
org.jtrim2.concurrent.query.RefCachedData<DataType>
Type Parameters:
DataType - the type of data referenced by the RefCachedData

public final class RefCachedData<DataType> extends Object
Defines a data and a VolatileReference of the same data.

Instances of RefCachedData store a hard reference to the data, so in case the VolatileReference is backed by a WeakReference or a SoftReference, then the reference from the VolatileReference will not disappear (unless cleared explicitly).

It should always be the case that getDataRef().get() be the same object as the data of the RefCachedData or be null (if the reference disappeared from the VolatileReference).

Thread safety

The methods of this class are safe to be accessed by multiple threads concurrently. Once created, the properties of this class cannot be changed. Note however, that if the data is mutable then, it can be changed. Also, the stored VolatileReference can be cleared.

Synchronization transparency

The methods of this class are synchronization transparent.
See Also:
  • Constructor Details

    • RefCachedData

      public RefCachedData(DataType data, VolatileReference<DataType> dataRef)
      Initializes this RefCachedData with the specified data and reference to the data.

      The specified VolatileReference must return the same data (in terms of "==") as the one specified or return null if the data has disappeared from the reference. Note however that this is not checked by this method.

      Parameters:
      data - the actual data which is referenced by the specified VolatileReference. This argument can be null.
      dataRef - the VolatileReference referencing the specified data. This argument cannot be null.
      Throws:
      NullPointerException - thrown if the specified VolatileReference is null
    • RefCachedData

      public RefCachedData(DataType data, ObjectCache cache, ReferenceType refType)
      Initializes this RefCachedData with the specified data and a VolatileReference to the data, created by the specified cache.
      Parameters:
      data - the actual data to which a VolatileReference will be created using the specified cache. This argument can be null.
      cache - the ObjectCache to be used to create the VolatileReference to the specified data. This argument can be null, in which case ObjectCache.javaRefCache() will be used instead.
      refType - the ReferenceType to be used to reference the cached data using the specified ObjectCache. This argument cannot be null.
      Throws:
      NullPointerException - thrown if the specified ReferenceType is null
  • Method Details

    • getData

      public DataType getData()
      Returns the data which is referenced by the stored VolatileReference returned by the getDataRef method.
      Returns:
      the data which is referenced by the stored VolatileReference returned by the getDataRef method. This method may return null if null was specified at construction time.
      See Also:
    • getDataRef

      public VolatileReference<DataType> getDataRef()
      Returns the VolatileReference referencing the stored data.
      Returns:
      the VolatileReference referencing the stored data. This method never returns null.
      See Also:
    • toString

      public String toString()
      Returns the string representation of this RefCachedData in no particular format. The string representation contains both the data and the reference to it.

      This method is intended to be used for debugging only.

      Overrides:
      toString in class Object
      Returns:
      the string representation of this object in no particular format. This method never returns null.