Class Ref<T>

  • All Implemented Interfaces:
    Serializable, Comparable<Ref<T>>
    Direct Known Subclasses:
    DeadRef, LiveRef

    public abstract class Ref<T>
    extends Object
    implements Serializable, Comparable<Ref<T>>

    Ref is a Key which allows the entity value to be fetched directly.

    Note that the methods might or might not throw runtime exceptions related to datastore operations; ConcurrentModificationException, DatastoreTimeoutException, DatastoreFailureException, and DatastoreNeedIndexException. Some Refs hide datastore operations that could throw these exceptions.

    Author:
    Jeff Schnitzer
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Key<T> key
      The key associated with this ref
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Ref()
      For GWT serialization
      protected Ref​(Key<T> key)
      Create a Ref based on the key, with the specified session
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Ref<T> o)
      Comparison is based on key
      static <T> Ref<T> create​(Key<T> key)
      Key.create(Blah.class, id) is easier to type than new Key(Blah.class, id)
      static <T> Ref<T> create​(T value)
      Creates a Ref from a registered pojo entity
      boolean equals​(Object obj)
      Equality comparison is based on key equivalence
      boolean equivalent​(Key<T> other)
      Type safe comparison for key equivalence
      boolean equivalent​(Ref<T> other)
      Type-safe comparison for key equivalence
      abstract T get()
      Obtain the entity value associated with the key.
      Key<T> getKey()
      Same as key() but conforms to JavaBeans conventions in case this is being processed by a JSON converter or expression language.
      T getValue()
      This method exists to facilitate serialization via javabeans conventions.
      int hashCode()
      Hash code is simply that of key
      abstract boolean isLoaded()
      If an entity has been loaded into the session or is otherwise available, this will return true.
      Key<T> key()  
      T safe()
      Obtain the entity value, throwing an exception if the entity was not found.
      String toString()
      Renders some info about the key
    • Field Detail

      • key

        protected Key<T> key
        The key associated with this ref
    • Constructor Detail

      • Ref

        protected Ref()
        For GWT serialization
      • Ref

        protected Ref​(Key<T> key)
        Create a Ref based on the key, with the specified session
    • Method Detail

      • create

        public static <T> Ref<T> create​(Key<T> key)
        Key.create(Blah.class, id) is easier to type than new Key(Blah.class, id)
      • create

        public static <T> Ref<T> create​(T value)
        Creates a Ref from a registered pojo entity
      • key

        public Key<T> key()
        Returns:
        the key associated with this Ref
      • get

        public abstract T get()
        Obtain the entity value associated with the key. Will pull from session if present, otherwise will fetch from the datastore.
        Returns:
        the entity referenced, or null if the entity was not found
      • isLoaded

        public abstract boolean isLoaded()
        If an entity has been loaded into the session or is otherwise available, this will return true. Calls to get() will not require a trip to backing store. Note that even when loaded, get() can still return null if there is no entity which corresponds to the key.
        Returns:
        true if the value is in the session or otherwise immediately available; false if get() will require a trip to the datastore or memcache.
      • getValue

        public final T getValue()
        This method exists to facilitate serialization via javabeans conventions. Unlike get(), it will return null if isLoaded() is false.
        Returns:
        the entity referenced, or null if either the entity was not found or isLoaded() is false.
      • getKey

        public final Key<T> getKey()
        Same as key() but conforms to JavaBeans conventions in case this is being processed by a JSON converter or expression language.
      • safe

        public final T safe()
                     throws NotFoundException
        Obtain the entity value, throwing an exception if the entity was not found.
        Returns:
        the entity referenced. Never returns null.
        Throws:
        NotFoundException - if the specified entity was not found
      • compareTo

        public int compareTo​(Ref<T> o)
        Comparison is based on key
        Specified by:
        compareTo in interface Comparable<T>
      • equals

        public boolean equals​(Object obj)
        Equality comparison is based on key equivalence
        Overrides:
        equals in class Object
      • equivalent

        public boolean equivalent​(Ref<T> other)
        Type-safe comparison for key equivalence
      • equivalent

        public boolean equivalent​(Key<T> other)
        Type safe comparison for key equivalence
      • hashCode

        public int hashCode()
        Hash code is simply that of key
        Overrides:
        hashCode in class Object
      • toString

        public String toString()
        Renders some info about the key
        Overrides:
        toString in class Object