forked from objectify/objectify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.java
More file actions
392 lines (332 loc) · 11.5 KB
/
Key.java
File metadata and controls
392 lines (332 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package com.googlecode.objectify;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.PathElement;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.impl.Keys;
import com.googlecode.objectify.impl.TypeUtils;
import com.googlecode.objectify.util.KeyFormat;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.Iterator;
/**
* <p>A typesafe wrapper for the datastore Key object.</p>
*
* @author Jeff Schnitzer <jeff@infohazard.org>
* @author Scott Hernandez
*/
@EqualsAndHashCode(of="raw")
public class Key<T> implements Serializable, Comparable<Key<?>>
{
private static final long serialVersionUID = -262390393952444121L;
/** Key.create(key) is easier to type than new Key<Blah>(key) */
public static <T> Key<T> create(final com.google.cloud.datastore.Key raw) {
if (raw == null)
throw new NullPointerException("Cannot create a Key<?> from a null datastore Key");
return new Key<>(raw);
}
/** Key.create(Blah.class, id) is easier to type than new Key<Blah>(Blah.class, id) */
public static <T> Key<T> create(final Class<? extends T> kindClass, final long id) {
return new Key<>(kindClass, id);
}
/** Key.create(Blah.class, name) is easier to type than new Key<Blah>(Blah.class, name) */
public static <T> Key<T> create(final Class<? extends T> kindClass, final String name) {
return new Key<>(kindClass, name);
}
/** Key.create(parent, Blah.class, id) is easier to type than new Key<Blah>(parent, Blah.class, id) */
public static <T> Key<T> create(final Key<?> parent, final Class<? extends T> kindClass, final long id) {
return new Key<>(parent, kindClass, id);
}
/** Key.create(parent, Blah.class, name) is easier to type than new Key<Blah>(parent, Blah.class, name) */
public static <T> Key<T> create(final Key<?> parent, final Class<? extends T> kindClass, final String name) {
return new Key<>(parent, kindClass, name);
}
/** Key.create(urlSafeString) is easier to type than new Key<Blah>(urlSafeString) */
public static <T> Key<T> create(final String urlSafeString) {
if (urlSafeString == null)
throw new NullPointerException("Cannot create a Key<?> from a null String");
return new Key<>(urlSafeString);
}
/** This is an alias for Key.create(String) which exists for JAX-RS compliance. */
public static <T> Key<T> valueOf(final String webSafeString) {
return Key.create(webSafeString);
}
/** Create a key from a registered POJO entity. */
public static <T> Key<T> create(final T pojo) {
return ObjectifyService.factory().keys().keyOf(pojo);
}
/** */
protected final com.google.cloud.datastore.Key raw;
/** Cache the instance of the parent wrapper to avoid unnecessary garbage */
transient protected Key<?> parent;
/** Wrap a raw Key */
private Key(final com.google.cloud.datastore.Key raw) {
this.raw = raw;
this.parent = null;
}
/** Create a key with a long id */
private Key(final Class<? extends T> kindClass, final long id) {
this(null, kindClass, id);
}
/** Create a key with a String name */
private Key(final Class<? extends T> kindClass, final String name) {
this(null, kindClass, name);
}
/** Create a key with a parent and a long id */
private Key(final Key<?> parent, final Class<? extends T> kindClass, final long id) {
final String kind = getKind(kindClass);
if (parent == null) {
this.raw = newKeyFactory().setKind(kind).newKey(id);
} else {
this.raw = com.google.cloud.datastore.Key.newBuilder(key(parent), kind, id).build();
}
this.parent = parent;
}
/** Create a key with a parent and a String name */
private Key(final Key<?> parent, final Class<? extends T> kindClass, final String name) {
final String kind = getKind(kindClass);
if (parent == null) {
this.raw = newKeyFactory().setKind(kind).newKey(name);
} else {
this.raw = com.google.cloud.datastore.Key.newBuilder(key(parent), kind, name).build();
}
this.parent = parent;
}
/**
* Reconstitute a Key from a web safe string. Understands the current (Cloud SDK) format and the legacy
* format (always starts with 'a'). These can be generated with toUrlSafeString() and toLegacyUrlSafeString().
*/
private Key(final String urlSafe) {
this(Keys.fromUrlSafe(urlSafe));
}
/** Obtain it from the global ObjectifyFactory */
private KeyFactory newKeyFactory() {
return ObjectifyService.factory().datastore().newKeyFactory();
}
/**
* @return the raw datastore version of this key
*/
public com.google.cloud.datastore.Key getRaw() {
return this.raw;
}
/**
* @return the id associated with this key, or 0 if this key has a name.
*/
public long getId() {
return this.raw.getId();
}
/**
* @return the name associated with this key, or null if this key has an id
*/
public String getName() {
return this.raw.getName();
}
/**
* @return the low-level datastore kind associated with this Key
*/
public String getKind() {
return this.raw.getKind();
}
/**
* @return the parent key, or null if there is no parent. Note that
* the parent could potentially have any type.
*/
@SuppressWarnings("unchecked")
public <V> Key<V> getParent() {
if (this.parent == null && this.raw.getParent() != null)
this.parent = new Key<V>(this.raw.getParent());
return (Key<V>)this.parent;
}
/**
* Gets the root of a parent graph of keys. If a Key has no parent, it is the root.
*
* @return the topmost parent key, or this object itself if it is the root.
* Note that the root key could potentially have any type.
*/
@SuppressWarnings("unchecked")
public <V> Key<V> getRoot() {
if (this.getParent() == null)
return (Key<V>)this;
else
return this.getParent().getRoot();
}
/**
* <p>The new cloud sdk Key doesn't have compareTo(), so we reimplement the logic from the old GAE SDK.</p>
*/
@Override
public int compareTo(final Key<?> other) {
if (this.raw == other.raw) {
return 0;
}
{
final int result = this.raw.getProjectId().compareTo(other.raw.getProjectId());
if (result != 0)
return result;
}
{
final int result = this.raw.getNamespace().compareTo(other.raw.getNamespace());
if (result != 0)
return result;
}
{
final int result = this.compareAncestors(other);
if (result != 0)
return result;
}
{
// Too bad PathElement and Key don't share any kind of interface grrr
final int result = this.getRaw().getKind().compareTo(other.getRaw().getKind());
if (result != 0) {
return result;
}
else if (this.raw.getNameOrId() == null && other.raw.getNameOrId() == null) {
return compareToWithIdentityHash(this.raw, other.raw);
}
else if (this.raw.hasId()) {
return other.raw.hasId() ? Long.compare(this.raw.getId(), other.raw.getId()) : -1;
}
else {
return other.raw.hasId() ? 1 : this.raw.getName().compareTo(other.raw.getName());
}
}
}
private int compareAncestors(final Key<?> other) {
final Iterator<PathElement> thisPath = this.raw.getAncestors().iterator();
final Iterator<PathElement> otherPath = other.raw.getAncestors().iterator();
int result;
do {
if (!thisPath.hasNext()) {
return otherPath.hasNext() ? -1 : 0;
}
if (!otherPath.hasNext()) {
return 1;
}
final PathElement thisKey = thisPath.next();
final PathElement otherKey = otherPath.next();
result = comparePathElements(thisKey, otherKey);
} while (result == 0);
return result;
}
private int comparePathElements(final PathElement here, final PathElement there) {
final int result = here.getKind().compareTo(there.getKind());
if (result != 0) {
return result;
}
else if (here.getNameOrId() == null && there.getNameOrId() == null) {
return compareToWithIdentityHash(here, there);
}
else if (here.hasId()) {
return there.hasId() ? Long.compare(here.getId(), there.getId()) : -1;
}
else {
return there.hasId() ? 1 : here.getName().compareTo(there.getName());
}
}
/** I have no idea what this is about, it was in the old logic */
private int compareToWithIdentityHash(final Object k1, final Object k2) {
return Integer.compare(System.identityHashCode(k1), System.identityHashCode(k2));
}
/** A type-safe equivalence comparison */
public boolean equivalent(final Key<T> other) {
return equals(other);
}
/** A type-safe equivalence comparison */
public boolean equivalent(final Ref<T> other) {
return (other != null) && equals(other.key());
}
/** Creates a human-readable version of this key */
@Override
public String toString() {
return "Key<?>(" + this.raw + ")";
}
/**
* @deprecated Use toUrlSafe() instead.
*/
@Deprecated
public String getString() {
return toUrlSafe();
}
/**
* This is an alias for toLegacyUrlSafe() and exists solely for backwards compatibility.
*
* @deprecated Use toLegacyUrlSafe(), or if you aren't trying to generate old-style keys, use
* toUrlSafe().
*/
@Deprecated
public String toWebSafeString() {
return this.raw.toUrlSafe();
}
/**
* Call toUrlSafe() on the underlying Key. You can reconstitute a {@code 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).
*/
public String toUrlSafe() {
return this.raw.toUrlSafe();
}
/**
* Generates the string that would have been generated by the old appengine SDK.
* The strings look like 'ag1zfnZvb2Rvb2R5bmUwcgcLEgFCGAEM'. The String constructor
* for {@code Key<?>} understands both formats.
*/
public String toLegacyUrlSafe() {
return KeyFormat.INSTANCE.formatOldStyleAppEngineKey(this.raw);
}
/**
* Easy null-safe conversion of the raw key.
*/
public static <V> Key<V> key(final com.google.cloud.datastore.Key raw) {
if (raw == null)
return null;
else
return new Key<>(raw);
}
/**
* Easy null-safe conversion of the typed key.
*/
public static com.google.cloud.datastore.Key key(final Key<?> typed) {
if (typed == null)
return null;
else
return typed.getRaw();
}
/**
* <p>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).</p>
*
* <p>If no @Entity annotation is found, just uses the simplename as is.</p>
*/
public static String getKind(final Class<?> clazz) {
final String kind = getKindRecursive(clazz);
if (kind == null)
return clazz.getSimpleName();
else
return kind;
}
/**
* <p>Recursively looks for the @Entity annotation.</p>
*
* @return null if kind cannot be found
*/
private static String getKindRecursive(final Class<?> clazz) {
if (clazz == Object.class)
return null;
final String kind = getKindHere(clazz);
if (kind != null)
return kind;
else
return getKindRecursive(clazz.getSuperclass());
}
/**
* Get the kind from the class if the class has an @Entity annotation, otherwise return null.
*/
private static String getKindHere(final Class<?> clazz) {
// @Entity is inherited so we have to be explicit about the declared annotations
final Entity ourAnn = TypeUtils.getDeclaredAnnotation(clazz, Entity.class);
if (ourAnn != null)
if (ourAnn.name().length() != 0)
return ourAnn.name();
else
return clazz.getSimpleName();
return null;
}
}