Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / PreGetContext.java
blob5d2e1e9fe244ba5fa266b728f393ff8d21e019e4
1 // Copyright 2012 Google. All Rights Reserved.
2 package com.google.appengine.api.datastore;
4 import java.util.List;
5 import java.util.Map;
7 /**
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> {
18 /**
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;
29 @Override
30 String getKind(Key key) {
31 return key.getKind();
34 /**
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
39 * results.
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) {
47 if (entity == null) {
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);