1 // Copyright 2012 Google. All Rights Reserved.
2 package com
.google
.appengine
.api
.datastore
;
8 * Concrete {@link CallbackContext} implementation that is specific to
9 * intercepted get() operations. Methods annotated with {@link PreGet} that receive
10 * instances of this class may modify the result of the get() operation by
11 * calling {@link #setResultForCurrentElement(Entity)}. Keys that receive
12 * results via this method will not be fetched from the datastore. This is an
13 * effective way to inject cached results.
16 public final class PreGetContext
extends BaseCallbackContext
<Key
> {
19 * The Map that wil ultimately be populated with the result of the get() RPC.
21 private final Map
<Key
, Entity
> resultMap
;
23 PreGetContext(CurrentTransactionProvider currentTransactionProvider
, List
<Key
> keys
,
24 Map
<Key
, Entity
> resultMap
) {
25 super(currentTransactionProvider
, keys
);
26 this.resultMap
= resultMap
;
30 String
getKind(Key key
) {
35 * Set the {@link Entity} that will be associated with the {@link Key}
36 * returned by {@link #getCurrentElement()} in the result of the get()
37 * operation. This will prevent the get() operation from fetching the
38 * Entity from the datastore. This is an effective way to inject cached
41 * @param entity The entity to provide as the result for the current element.
43 * @throws IllegalArgumentException If the key of the provided entity is not
44 * equal to the key returned by {@link #getCurrentElement()}.
46 public void setResultForCurrentElement(Entity entity
) {
48 throw new NullPointerException("entity cannot be null");
50 Key curKey
= getCurrentElement();
51 if (!curKey
.equals(entity
.getKey())) {
52 throw new IllegalArgumentException("key of provided entity must be equal to current element");
54 resultMap
.put(getCurrentElement(), entity
);