Class Key
- java.lang.Object
-
- com.google.appengine.api.datastore.Key
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable<Key>
public final class Key extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<Key>
The primary key for a datastore entity.A datastore GUID. A Key instance uniquely identifies an entity across all apps, and includes all information necessary to fetch the entity from the datastore with
DatastoreService.get(Key).You can create
Keyobjects directly by usingKeyFactory.createKey(java.lang.String, long)orgetChild(java.lang.String, long).You can also retrieve the
Keyautomatically created when you create a newEntity, or serializeKeyobjects, or useKeyFactoryto convert them to and from websafe String values.- See Also:
KeyFactory, Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description intcompareTo(Key other)Compares twoKeyobjects.booleanequals(java.lang.Object object)Compares twoKeyobjects by comparing ids, kinds, parent and appIdNamespace.java.lang.StringgetAppId()Returns the appId for thisKey.KeygetChild(java.lang.String kind, long id)Creates a new key havingthisas parent and the given numeric identifier.KeygetChild(java.lang.String kind, java.lang.String name)Creates a new key havingthisas parent and the given name.longgetId()Returns the numeric identifier of thisKey.java.lang.StringgetKind()Returns the kind of theEntityrepresented by thisKey.java.lang.StringgetName()Returns the name of thisKey.java.lang.StringgetNamespace()Returns the namespace for thisKey.KeygetParent()If thisKeyhas a parent, return aKeythat represents it.inthashCode()booleanisComplete()Returns true if this Key has a name specified or has been assigned an identifier.java.lang.StringtoString()
-
-
-
Method Detail
-
getKind
public java.lang.String getKind()
Returns the kind of theEntityrepresented by thisKey.
-
getParent
public Key getParent()
If thisKeyhas a parent, return aKeythat represents it. If not, simply return null.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object object)
Compares twoKeyobjects by comparing ids, kinds, parent and appIdNamespace. If both keys are assigned names rather than ids, compares names instead of ids. If neither key has an id or a name, the keys are only equal if they reference the same object.- Overrides:
equalsin classjava.lang.Object
-
getAppId
public java.lang.String getAppId()
Returns the appId for thisKey.
-
getNamespace
public java.lang.String getNamespace()
Returns the namespace for thisKey.
-
getId
public long getId()
Returns the numeric identifier of thisKey.
-
getName
public java.lang.String getName()
Returns the name of thisKey.
-
getChild
public Key getChild(java.lang.String kind, long id)
Creates a new key havingthisas parent and the given numeric identifier. The parent key must be complete.- Parameters:
kind- the kind of the child key to createid- the numeric identifier of the key inkind, unique for this parent
-
getChild
public Key getChild(java.lang.String kind, java.lang.String name)
Creates a new key havingthisas parent and the given name. The parent key must be complete.- Parameters:
kind- the kind of the child key to createname- the name of the key inkind, as an arbitrary string unique for this parent
-
isComplete
public boolean isComplete()
Returns true if this Key has a name specified or has been assigned an identifier.
-
compareTo
public int compareTo(Key other)
Compares twoKeyobjects. The algorithm proceeds as follows: Turn eachKeyinto an iterator where the first element returned is the top-most ancestor, the next element is the child of the previous element, and so on. The last element will be theKeywe started with. Once we have assembled these two iterators (one for 'this' and one for theKeywe're comparing to), consume them in parallel, comparing the next element from each iterator. If at any point the comparison of these two elements yields a non-zero result, return that as the result of the overall comparison. If we exhaust the iterator built from 'this' before we exhaust the iterator built from the otherKey, we return less than. An example:app1.type1.4.app1.type2.9 < app1.type1.4.app1.type2.9.app1.type3.2If we exhaust the iterator built from the other
Keybefore we exhaust the iterator built from 'this', we return greater than. An example:app1.type1.4.app1.type2.9.app1.type3.2 > app1.type1.4.app1.type2.9The relationship between individual
Key Keysis performed by comparing app followed by kind followed by id. If both keys are assigned names rather than ids, compares names instead of ids. If neither key has an id or a name we return an arbitrary but consistent result. Assuming all other components are equal, all ids are less than all names.- Specified by:
compareToin interfacejava.lang.Comparable<Key>
-
-