@Override public <T> Result<T> find(final Key<? extends T> key) { Result<Map<Key<T>, T>> multi = this.get(Collections.singleton(key)); Future<T> wrapped = new SimpleFutureWrapper<Map<Key<T>, T>, T>(multi.getFuture()) { @Override protected T wrap(Map<Key<T>, T> base) throws Exception { return base.get(key); } }; return new ResultAdapter<T>(wrapped); }
@Override public <T> Result<Key<T>> put(final T obj) { // let's just translate this to a put(iterable) call Result<Map<Key<T>, T>> result = this.put(Collections.singleton(obj)); Future<Key<T>> future = new SimpleFutureWrapper<Map<Key<T>, T>, Key<T>>(result.getFuture()) { @Override protected Key<T> wrap(Map<Key<T>, T> putted) throws Exception { return putted.keySet().iterator().next(); } }; return new ResultAdapter<Key<T>>(future); }
@Override public <T> Result<T> get(final Key<? extends T> key) { // The actual implementation is find(). Result<T> found = this.find(key); Future<T> wrapped = new SimpleFutureWrapper<T, T>(found.getFuture()) { @Override protected T wrap(T t) throws Exception { if (t != null) return t; else throw new NotFoundException(key); } }; return new ResultAdapter<T>(wrapped); }
@Override public <T> Result<Map<Key<T>, T>> put(final Iterable<? extends T> objs) { // Unfortunately we can't put the data in the session cache right away because // the entities might not have populated ids. They keys/ids only get populated // when the data is fetched. //for (T t: objs) // this.cache.put(this.factory.getKey(t), t); // //return super.put(objs); Result<Map<Key<T>, T>> orig = super.put(objs); Future<Map<Key<T>, T>> triggered = new TriggerSuccessFuture<Map<Key<T>, T>>(orig.getFuture()) { @Override protected void success(Map<Key<T>, T> result) { for (Map.Entry<Key<T>, T> entry: result.entrySet()) cache.put(entry.getKey(), entry.getValue()); } }; return new ResultAdapter<Map<Key<T>, T>>(triggered); }
Future<Map<S, T>> wrapped = new SimpleFutureWrapper<Map<Key<T>, T>, Map<S, T>>(fetched.getFuture()) { @Override protected Map<S, T> wrap(Map<Key<T>, T> base) throws Exception
Future<Map<Key<T>, T>> fromDatastore = super.get(keys).getFuture();