forked from objectify/objectify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaverImpl.java
More file actions
75 lines (63 loc) · 2.01 KB
/
Copy pathSaverImpl.java
File metadata and controls
75 lines (63 loc) · 2.01 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
package com.googlecode.objectify.impl;
import com.google.cloud.datastore.FullEntity;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import com.googlecode.objectify.cmd.Saver;
import com.googlecode.objectify.impl.translate.SaveContext;
import com.googlecode.objectify.util.ResultWrapper;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
/**
* Implementation of the Saver interface.
*
* @author Jeff Schnitzer <jeff@infohazard.org>
*/
public abstract class SaverImpl implements Saver
{
/** */
protected final ObjectifyImpl ofy;
/** */
public SaverImpl(final ObjectifyImpl ofy) {
this.ofy = ofy;
}
/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entity(java.lang.Object)
*/
@Override
public <E> Result<Key<E>> entity(final E entity) {
final Result<Map<Key<E>, E>> base = this.<E>entities(Collections.singleton(entity));
return new ResultWrapper<Map<Key<E>, E>, Key<E>>(base) {
private static final long serialVersionUID = 1L;
@Override
protected Key<E> wrap(final Map<Key<E>, E> base) {
return base.keySet().iterator().next();
}
};
}
/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entities(E[])
*/
@Override
public <E> Result<Map<Key<E>, E>> entities(final E... entities) {
return this.entities(Arrays.asList(entities));
}
/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entities(java.lang.Iterable)
*/
@Override
abstract public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities);
/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#toEntity(java.lang.Object)
*/
@Override
public FullEntity<?> toEntity(final Object pojo) {
if (pojo instanceof FullEntity<?>) {
return (FullEntity<?>)pojo;
} else {
@SuppressWarnings("unchecked")
final EntityMetadata<Object> meta = (EntityMetadata<Object>)ofy.factory().getMetadata(pojo.getClass());
return meta.save(pojo, new SaveContext());
}
}
}