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 invalid input: '<'[email protected]>, Scott Hernandez
See Also:
  • Field Summary

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

    Modifier and Type
    Method
    Description
    int
    compareTo(Key<?> other)
    The new cloud sdk Key doesn't have compareTo(), so we reimplement the logic from the old GAE SDK.
    static <T> Key<T>
    create(com.google.cloud.datastore.Key raw)
    Create an Objectify key from the native datastore key
    static <T> Key<T>
    create(Key<?> parent, Class<? extends T> kindClass, long id)
    Create an Objectify key from a parent, type, and numeric id
    static <T> Key<T>
    create(Key<?> parent, Class<? extends T> kindClass, String name)
    Create an Objectify key from a parent, type, and string id
    static <T> Key<T>
    create(Class<? extends T> kindClass, long id)
    Create an Objectify key from a type and numeric id
    static <T> Key<T>
    create(Class<? extends T> kindClass, String name)
    Create an Objectify key from a type and string id
    static <T> Key<T>
    create(String urlSafeString)
    Create an Objectify key from a web safe string.
    static <T> Key<T>
    create(String namespace, Class<? extends T> kindClass, long id)
    Create an Objectify key from a namespace, type, and numeric id
    static <T> Key<T>
    create(String namespace, Class<? extends T> kindClass, String name)
    Create an Objectify key from a namespace, type, and string id
    static <T> Key<T>
    create(T pojo)
    Create a key from a registered POJO entity.
    boolean
    equivalent(Key<T> other)
    A type-safe equivalence comparison
    boolean
    equivalent(Ref<T> other)
    A type-safe equivalence comparison
     
     
    static String
    getKind(Class<?> clazz)
    Determines the kind for a Class, as understood by the datastore.
     
     
    <V> Key<V>
     
    com.google.cloud.datastore.Key
     
    <V> Key<V>
    Gets the root of a parent graph of keys.
    static <V> Key<V>
    key(com.google.cloud.datastore.Key raw)
    Easy null-safe conversion of the raw key.
    static com.google.cloud.datastore.Key
    key(Key<?> typed)
    Easy null-safe conversion of the typed key.
    Generates the string that would have been generated by the old appengine SDK.
    Creates a human-readable version of this key
    Call toUrlSafe() on the underlying Key.
    static <T> Key<T>
    valueOf(String webSafeString)
    This is an alias for Key.create(String).

    Methods inherited from class java.lang.Object

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

    • raw

      protected final com.google.cloud.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.cloud.datastore.Key raw)
      Create an Objectify key from the native datastore key
    • create

      public static <T> Key<T> create(Class<? extends T> kindClass, long id)
      Create an Objectify key from a type and numeric id
    • create

      public static <T> Key<T> create(Class<? extends T> kindClass, String name)
      Create an Objectify key from a type and string id
    • create

      public static <T> Key<T> create(Key<?> parent, Class<? extends T> kindClass, long id)
      Create an Objectify key from a parent, type, and numeric id
    • create

      public static <T> Key<T> create(Key<?> parent, Class<? extends T> kindClass, String name)
      Create an Objectify key from a parent, type, and string id
    • create

      public static <T> Key<T> create(String namespace, Class<? extends T> kindClass, long id)
      Create an Objectify key from a namespace, type, and numeric id
    • create

      public static <T> Key<T> create(String namespace, Class<? extends T> kindClass, String name)
      Create an Objectify key from a namespace, type, and string id
    • create

      public static <T> Key<T> create(String urlSafeString)
      Create an Objectify key from a web safe string. Understands both 'modern' and 'legacy' GAE formats.
    • valueOf

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

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

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

      public Long getId()
      Returns:
      the id associated with this key, or null 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
    • getNamespace

      public String getNamespace()
      Returns:
      the namespace 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)

      The new cloud sdk Key doesn't have compareTo(), so we reimplement the logic from the old GAE SDK.

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

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

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

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

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

      public String toLegacyUrlSafe()
      Generates the string that would have been generated by the old appengine SDK. The strings look like 'ag1zfnZvb2Rvb2R5bmUwcgcLEgFCGAEM'. The String constructor for Key<?> understands both formats.
    • key

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

      public static com.google.cloud.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.