Class KeyFactory
java.lang.Object
com.google.appengine.api.datastore.KeyFactory
This class enables direct creation of
Key
objects, both in the root entity group (no
parent) and as the child of a given parent. Keys can also be created indirectly by putting named
Entities into the datastore, which will allocate a new key. See Entity(String, String)
for details.
This class also has methods for encoding and decoding Key
objects to and from strings.
Clients should not make any assumptions about the encoding format, except that it is a websafe
string that does not need to be quoted when used in HTML or in URLs.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Helper class that aids in the construction ofKeys
with ancestors. -
Method Summary
Modifier and TypeMethodDescriptionstatic Key
Creates a newKey
with the provided parent from its kind and ID.static Key
Creates a newKey
with the provided parent from its kind and name.static Key
Creates a newKey
having no parent from its kind and ID.static Key
Creates a newKey
having no parent from its kind and name.static String
createKeyString
(@Nullable Key parent, String kind, long id) Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(Key, String, long)
static String
createKeyString
(@Nullable Key parent, String kind, String name) Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(Key, String, String)
static String
createKeyString
(String kind, long id) Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(String, long)
static String
createKeyString
(String kind, String name) Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(String, String)
static String
keyToString
(Key key) Converts aKey
into a websafe string.static Key
stringToKey
(String encoded) Converts aString
-representation of aKey
into theKey
instance it represents.
-
Method Details
-
createKey
Creates a newKey
having no parent from its kind and ID.- Parameters:
kind
- the kind of the key to createid
- the numeric identifier of the key inkind
, unique across all root entities of this kind, must not be zero
-
createKey
Creates a newKey
with the provided parent from its kind and ID.- Parameters:
parent
- the parent of the key to create, can benull
kind
- the kind of the key to createid
- the numeric identifier of the key inkind
, unique across all entities of this kind with the same parent, must not be zero
-
createKey
Creates a newKey
having no parent from its kind and name.- Parameters:
kind
- the kind of the key to createname
- the name of the key inkind
, as an arbitrary string unique across all root entities of thiskind
-
createKey
Creates a newKey
with the provided parent from its kind and name.- Parameters:
parent
- the parent of the key to create, can benull
kind
- the kind of the key to createname
- the name of the key inkind
, as an arbitrary string unique across all entities of thiskind
with the same parent
-
createKeyString
Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(String, long)
- Parameters:
kind
- the kind of the key to createid
- the numeric identifier of the key inkind
, unique across all root entities of this kind, must not be zero- Returns:
- A websafe
String
representation of theKey
that was created
-
createKeyString
Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(Key, String, long)
- Parameters:
parent
- the parent of the key to create, can benull
.kind
- the kind of the key to createid
- the numeric identifier of the key inkind
, unique across entities of this kind with the same parent, must not be zero- Returns:
- A websafe
String
representation of theKey
that was created
-
createKeyString
Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(String, String)
- Parameters:
kind
- the kind of the key to createname
- the name of the key inkind
, as an arbitrary string unique across root entities of thiskind
- Returns:
- A websafe
String
representation of theKey
that was created
-
createKeyString
Shorthand for invokingkeyToString(Key)
on the result ofcreateKey(Key, String, String)
- Parameters:
parent
- the parent of the key to create, can benull
.kind
- the kind of the key to createname
- the name of the key inkind
, as an arbitrary string unique across entities of thiskind
with the same parent- Returns:
- A websafe
String
representation of theKey
that was created
-
keyToString
Converts aKey
into a websafe string. For example, this string can safely be used as an URL parameter embedded in a HTML document. Note thatkey.equals(KeyFactory.stringToKey(KeyFactory.keyToString(key))
should evaluate totrue
.- Parameters:
key
- TheKey
to convert to aString
.- Returns:
- A websafe
String
representation of the providedString
. - Throws:
IllegalArgumentException
- If the specifiedKey
is incomplete.
-
stringToKey
Converts aString
-representation of aKey
into theKey
instance it represents. Note thatstr.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))
should evaluate totrue
for all strings returned bykeyToString(com.google.appengine.api.datastore.Key)
.- Parameters:
encoded
- TheString
representation of aKey
.- Returns:
- The
Key
that the givenString
represents. - Throws:
IllegalArgumentException
- If the string cannot be parsed.
-