Class Key<T>

java.lang.Object
com.googlecode.objectify.Key<T>
All Implemented Interfaces:
Serializable, Comparable<Key<?>>

public class Key<T>
extends Object
implements Serializable, Comparable<Key<?>>

A typesafe wrapper for the datastore Key object.

Author:
Jeff Schnitzer , Scott Hernandez
See Also:
Serialized Form
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Key<?>
    Cache the instance of the parent wrapper to avoid unnecessary garbage
    protected com.google.appengine.api.datastore.Key
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    compareTo​(Key<?> other)
    Compares based on comparison of the raw key
    static <T> Key<T>
    create​(com.google.appengine.api.datastore.Key raw)
    Key.create(key) is easier to type than new Key(key)
    static <T> Key<T>
    create​(Key<?> parent, Class<? extends T> kindClass, long id)
    Key.create(parent, Blah.class, id) is easier to type than new Key(parent, Blah.class, id)
    static <T> Key<T>
    create​(Key<?> parent, Class<? extends T> kindClass, String name)
    Key.create(parent, Blah.class, name) is easier to type than new Key(parent, Blah.class, name)
    static <T> Key<T>
    create​(Class<? extends T> kindClass, long id)
    Key.create(Blah.class, id) is easier to type than new Key(Blah.class, id)
    static <T> Key<T>
    create​(Class<? extends T> kindClass, String name)
    Key.create(Blah.class, name) is easier to type than new Key(Blah.class, name)
    static <T> Key<T>
    create​(String webSafeString)
    Key.create(webSafeString) is easier to type than new Key(webSafeString)
    static <T> Key<T>
    create​(T pojo)
    Create a key from a registered POJO entity.
    boolean
    equals​(Object obj)
     
    boolean
    equivalent​(Key<T> other)
    A type-safe equivalence comparison
    boolean
    equivalent​(Ref<T> other)
    A type-safe equivalence comparison
    long
     
     
    static String
    getKind​(Class<?> clazz)
    Determines the kind for a Class, as understood by the datastore.
     
    <V> Key<V>
     
    com.google.appengine.api.datastore.Key
     
    <V> Key<V>
    Gets the root of a parent graph of keys.
    Call KeyFactory.keyToString() on the underlying Key.
    int
     
    static <V> Key<V>
    key​(com.google.appengine.api.datastore.Key raw)
    Easy null-safe conversion of the raw key.
    static com.google.appengine.api.datastore.Key
    key​(Key<?> typed)
    Easy null-safe conversion of the typed key.
    Creates a human-readable version of this key
    Call KeyFactory.keyToString() on the underlying Key.
    static <T> Key<T>
    valueOf​(String webSafeString)
    This is an alias for Key.create(String) which exists for JAX-RS compliance.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • raw

      protected com.google.appengine.api.datastore.Key raw
    • parent

      protected transient Key<?> parent
      Cache the instance of the parent wrapper to avoid unnecessary garbage
  • Method Details

    • create

      public static <T> Key<T> create(com.google.appengine.api.datastore.Key raw)
      Key.create(key) is easier to type than new Key(key)
    • create

      public static <T> Key<T> create(Class<? extends T> kindClass, long id)
      Key.create(Blah.class, id) is easier to type than new Key(Blah.class, id)
    • create

      public static <T> Key<T> create(Class<? extends T> kindClass, String name)
      Key.create(Blah.class, name) is easier to type than new Key(Blah.class, name)
    • create

      public static <T> Key<T> create(Key<?> parent, Class<? extends T> kindClass, long id)
      Key.create(parent, Blah.class, id) is easier to type than new Key(parent, Blah.class, id)
    • create

      public static <T> Key<T> create(Key<?> parent, Class<? extends T> kindClass, String name)
      Key.create(parent, Blah.class, name) is easier to type than new Key(parent, Blah.class, name)
    • create

      public static <T> Key<T> create(String webSafeString)
      Key.create(webSafeString) is easier to type than new Key(webSafeString)
    • valueOf

      public static <T> Key<T> valueOf(String webSafeString)
      This is an alias for Key.create(String) which exists for JAX-RS compliance.
    • create

      public static <T> Key<T> create(T pojo)
      Create a key from a registered POJO entity.
    • getRaw

      public com.google.appengine.api.datastore.Key getRaw()
      Returns:
      the raw datastore version of this key
    • getId

      public long getId()
      Returns:
      the id associated with this key, or 0 if this key has a name.
    • getName

      public String getName()
      Returns:
      the name associated with this key, or null if this key has an id
    • getKind

      public String getKind()
      Returns:
      the low-level datastore kind associated with this Key
    • getParent

      public <V> Key<V> getParent()
      Returns:
      the parent key, or null if there is no parent. Note that the parent could potentially have any type.
    • getRoot

      public <V> Key<V> getRoot()
      Gets the root of a parent graph of keys. If a Key has no parent, it is the root.
      Returns:
      the topmost parent key, or this object itself if it is the root. Note that the root key could potentially have any type.
    • compareTo

      public int compareTo(Key<?> other)

      Compares based on comparison of the raw key

      Specified by:
      compareTo in interface Comparable<T>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • equivalent

      public boolean equivalent(Key<T> other)
      A type-safe equivalence comparison
    • equivalent

      public boolean equivalent(Ref<T> other)
      A type-safe equivalence comparison
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Creates a human-readable version of this key
      Overrides:
      toString in class Object
    • getString

      public String getString()
      Call KeyFactory.keyToString() on the underlying Key. You can reconstitute a Key using the constructor that takes a websafe string. This is a javabeans-style alias for toWebSafeString().
    • toWebSafeString

      public String toWebSafeString()
      Call KeyFactory.keyToString() on the underlying Key. You can reconstitute a Key using the constructor that takes a websafe string. Note that toString() is only useful for debugging; it cannot be used to create a key with Key.create(String).
    • key

      public static <V> Key<V> key(com.google.appengine.api.datastore.Key raw)
      Easy null-safe conversion of the raw key.
    • key

      public static com.google.appengine.api.datastore.Key key(Key<?> typed)
      Easy null-safe conversion of the typed key.
    • getKind

      public static String getKind(Class<?> clazz)

      Determines the kind for a Class, as understood by the datastore. The first class in a hierarchy that has @Entity defines the kind (either explicitly or as that class' simplename).

      If no @Entity annotation is found, just uses the simplename as is.