com.googlecode.objectify
Class Key<T>

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

public class Key<T>
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<Key<?>>

A typesafe wrapper for the datastore Key object.

Author:
Jeff Schnitzer , Scott Hernandez
See Also:
Serialized Form

Field Summary
protected  Key<?> parent
          Cache the instance of the parent wrapper to avoid unnecessary garbage
protected  com.google.appengine.api.datastore.Key raw
           
 
Constructor Summary
protected Key()
          For GWT serialization
  Key(java.lang.Class<? extends T> kindClass, long id)
          Create a key with a long id
  Key(java.lang.Class<? extends T> kindClass, java.lang.String name)
          Create a key with a String name
  Key(com.google.appengine.api.datastore.Key raw)
          Wrap a raw Key
  Key(Key<?> parent, java.lang.Class<? extends T> kindClass, long id)
          Create a key with a parent and a long id
  Key(Key<?> parent, java.lang.Class<? extends T> kindClass, java.lang.String name)
          Create a key with a parent and a String name
  Key(java.lang.String webSafe)
          Reconstitute a Key from a web safe string.
 
Method Summary
 int compareTo(Key<?> other)
          Compares based on comparison of the raw key
static
<T> Key<T>
create(java.lang.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(java.lang.Class<? extends T> kindClass, java.lang.String name)
          Key.create(Blah.class, name) is easier to type than new Key(Blah.class, name)
static
<T> Key<T>
create(Key<?> parent, java.lang.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, java.lang.Class<? extends T> kindClass, java.lang.String name)
          Key.create(parent, Blah.class, name) is easier to type than new Key(parent, Blah.class, name)
static
<T> Key<T>
create(java.lang.String webSafeString)
          Key.create(webSafeString) is easier to type than new Key(webSafeString)
 boolean equals(java.lang.Object obj)
           
 long getId()
           
 java.lang.String getKind()
           
static java.lang.String getKind(java.lang.Class<?> clazz)
          Determines the kind for a Class, as understood by the datastore.
 java.lang.String getName()
           
<V> Key<V>
getParent()
           
 com.google.appengine.api.datastore.Key getRaw()
           
<V> Key<V>
getRoot()
          Gets the root of a parent graph of keys.
 java.lang.String getString()
          Call KeyFactory.keyToString() on the underlying Key.
 int hashCode()
           
static com.google.appengine.api.datastore.Key raw(Key<?> typed)
          Easy null-safe conversion of the typed key.
 java.lang.String toString()
          Creates a human-readable version of this key
static
<V> Key<V>
typed(com.google.appengine.api.datastore.Key raw)
          Easy null-safe conversion of the raw key.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

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

Constructor Detail

Key

protected Key()
For GWT serialization


Key

public Key(com.google.appengine.api.datastore.Key raw)
Wrap a raw Key


Key

public Key(java.lang.Class<? extends T> kindClass,
           long id)
Create a key with a long id


Key

public Key(java.lang.Class<? extends T> kindClass,
           java.lang.String name)
Create a key with a String name


Key

public Key(Key<?> parent,
           java.lang.Class<? extends T> kindClass,
           long id)
Create a key with a parent and a long id


Key

public Key(Key<?> parent,
           java.lang.Class<? extends T> kindClass,
           java.lang.String name)
Create a key with a parent and a String name


Key

public Key(java.lang.String webSafe)
Reconstitute a Key from a web safe string. This can be generated with getString() or KeyFactory.strongToKey().

Method Detail

create

public static <T> Key<T> create(java.lang.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(java.lang.Class<? extends T> kindClass,
                                java.lang.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,
                                java.lang.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,
                                java.lang.Class<? extends T> kindClass,
                                java.lang.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(java.lang.String webSafeString)
Key.create(webSafeString) is easier to type than new Key(webSafeString)


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 java.lang.String getName()
Returns:
the name associated with this key, or null if this key has an id

getKind

public java.lang.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 java.lang.Comparable<Key<?>>

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Creates a human-readable version of this key

Overrides:
toString in class java.lang.Object

getString

public java.lang.String getString()
Call KeyFactory.keyToString() on the underlying Key. You can reconstitute a Key using the constructor that takes a websafe string.


typed

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


raw

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


getKind

public static java.lang.String getKind(java.lang.Class<?> clazz)

Determines the kind for a Class, as understood by the datastore. The logic for this is approximately:

Throws:
java.lang.IllegalArgumentException - if a kind cannot be determined (ie @Subclass with invalid hierarchy).


Copyright © 2011 Jeff Schnitzer and a gang of pirates. All Rights Reserved. Build version: 3.1