Skip to content

Commit 6a9fb21

Browse files
committed
---
yaml --- r: 37 b: refs/heads/master c: 615c04e h: refs/heads/master i: 35: cc08ab5 v: v3
1 parent 8c2414f commit 6a9fb21

3 files changed

Lines changed: 245 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 1222d1e35a8add26788843c101530e30c74755c3
2+
refs/heads/master: 615c04e614c84e196c75b525cc1515017b54ce9d
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package com.google.gcloud.datastore;
2+
3+
import com.google.api.services.datastore.DatastoreV1;
4+
import com.google.common.collect.ImmutableSortedMap;
5+
import com.google.gcloud.datastore.Value.Type;
6+
7+
import java.util.Arrays;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
14+
// TODO: make entity and Partial entity extends it and to tests + documentaion the option
15+
// of list and direct value set/get.
16+
17+
/**
18+
* A container of properties (name and Value pairs).
19+
*/
20+
abstract class PropertyContainer
21+
<E extends PropertyContainer<E, B>, B extends PropertyContainer.Builder<E, B>>
22+
extends Serializable<DatastoreV1.Entity> {
23+
24+
private static final long serialVersionUID = 8175618724683792766L;
25+
26+
private final transient ImmutableSortedMap<String, Value<?, ?, ?>> properties;
27+
28+
protected abstract static class Builder
29+
<E extends PropertyContainer<E, B>, B extends Builder<E, B>> {
30+
31+
private final Map<String, Value<?, ?, ?>> properties;
32+
33+
public Builder() {
34+
properties = new HashMap<>();
35+
}
36+
37+
public Builder(PropertyContainer<E, B> entity) {
38+
properties = new HashMap<>(entity.properties());
39+
}
40+
41+
@SuppressWarnings("unchecked")
42+
protected B self() {
43+
return (B) this;
44+
}
45+
46+
public B clearProperties() {
47+
properties.clear();
48+
return self();
49+
}
50+
51+
public B removeProperty(String name) {
52+
properties.remove(name);
53+
return self();
54+
}
55+
56+
public B setProperty(String name, Value<?, ?, ?> value) {
57+
properties.put(name, value);
58+
return self();
59+
}
60+
61+
public B setNullProperty(String name) {
62+
properties.put(name, new NullValue());
63+
return self();
64+
}
65+
66+
public B setStringProperty(String name, String value) {
67+
properties.put(name, new StringValue(value));
68+
return self();
69+
}
70+
71+
public B setLongProperty(String name, long value) {
72+
properties.put(name, new LongValue(value));
73+
return self();
74+
}
75+
76+
public B setDoubleProperty(String name, double value) {
77+
properties.put(name, new DoubleValue(value));
78+
return self();
79+
}
80+
81+
public B setBooleanProperty(String name, boolean value) {
82+
properties.put(name, new BooleanValue(value));
83+
return self();
84+
}
85+
86+
public B setDateAndTimeProperty(String name, DateAndTime value) {
87+
properties.put(name, new DateAndTimeValue(value));
88+
return self();
89+
}
90+
91+
public B setKeyProperty(String name, Key value) {
92+
properties.put(name, new KeyValue(value));
93+
return self();
94+
}
95+
96+
public B setPartialEntityProperty(String name, PartialEntity value) {
97+
properties.put(name, new PartialEntityValue(value));
98+
return self();
99+
}
100+
101+
public B setListProperty(String name, List<Value<?, ?, ?>> values) {
102+
properties.put(name, new ListValue(values));
103+
return self();
104+
}
105+
106+
public B setListProperty(String name, Value<?, ?, ?>... value) {
107+
properties.put(name, new ListValue(Arrays.asList(value)));
108+
return self();
109+
}
110+
111+
public B setBlobProperty(String name, Blob value) {
112+
properties.put(name, new BlobValue(value));
113+
return self();
114+
}
115+
116+
public E build() {
117+
return build(ImmutableSortedMap.copyOf(properties));
118+
}
119+
120+
protected abstract E build(ImmutableSortedMap<String, Value<?, ?, ?>> properties);
121+
}
122+
123+
protected PropertyContainer(ImmutableSortedMap<String, Value<?, ?, ?>> properties) {
124+
this.properties = properties;
125+
}
126+
127+
/**
128+
* Returns {@code true} if there is such property with the given {@code name}.
129+
*/
130+
public boolean hasProperty(String name) {
131+
return properties.containsKey(name);
132+
}
133+
134+
/**
135+
* Returns the {@link Value} of property with the given {@code name}.
136+
*
137+
* @throws DatastoreServiceException if not such property.
138+
*/
139+
public <V extends Value<?, ?, ?>> V property(String name) {
140+
@SuppressWarnings("unchecked")
141+
V property = (V) properties.get(name);
142+
if (property == null) {
143+
throw DatastoreServiceException.throwInvalidRequest("No such property %s", name);
144+
}
145+
return property;
146+
}
147+
148+
public Type propertyType(String name) {
149+
return property(name).type();
150+
}
151+
152+
public boolean isNullProperty(String name) {
153+
return property(name) instanceof NullValue;
154+
}
155+
156+
public String stringProperty(String name) {
157+
return ((StringValue) property(name)).get();
158+
}
159+
160+
public long longProperty(String name) {
161+
return ((LongValue) property(name)).get();
162+
}
163+
164+
public double doubleProperty(String name) {
165+
return ((DoubleValue) property(name)).get();
166+
}
167+
168+
public boolean booleanProperty(String name) {
169+
return ((BooleanValue) property(name)).get();
170+
}
171+
172+
public DateAndTime dateAndTimeProperty(String name) {
173+
return ((DateAndTimeValue) property(name)).get();
174+
}
175+
176+
public Key keyProperty(String name) {
177+
return ((KeyValue) property(name)).get();
178+
}
179+
180+
public PartialEntity partialEntityProperty(String name) {
181+
return ((PartialEntityValue) property(name)).get();
182+
}
183+
184+
public List<? extends Value<?, ?, ?>> listProperty(String name) {
185+
return ((ListValue) property(name)).get();
186+
}
187+
188+
public Blob blobProperty(String name) {
189+
return ((BlobValue) property(name)).get();
190+
}
191+
192+
/**
193+
* Returns the property's value as {@code RawValue}.
194+
*/
195+
public RawValue rawValueProperty(String name) {
196+
Value<?, ?, ?> value = property(name);
197+
if (value instanceof RawValue) {
198+
return (RawValue) value;
199+
}
200+
return new RawValue(value.toPb());
201+
}
202+
203+
public Set<String> propertyNames() {
204+
return properties.keySet();
205+
}
206+
207+
/**
208+
* Returns a new builder for this entity (values are copied).
209+
*/
210+
public abstract B builder();
211+
212+
ImmutableSortedMap<String, Value<?, ?, ?>> properties() {
213+
return properties;
214+
}
215+
216+
@Override
217+
public int hashCode() {
218+
return properties.hashCode();
219+
}
220+
221+
@Override
222+
public boolean equals(Object obj) {
223+
if (!(obj instanceof PropertyContainer)) {
224+
return false;
225+
}
226+
return properties.equals(((PropertyContainer<?, ?>) obj).properties);
227+
}
228+
229+
@Override
230+
protected final DatastoreV1.Entity toPb() {
231+
DatastoreV1.Entity.Builder entityPb = DatastoreV1.Entity.newBuilder();
232+
for (Map.Entry<String, Value<?, ?, ?>> entry : properties.entrySet()) {
233+
DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder();
234+
propertyPb.setName(entry.getKey());
235+
propertyPb.setValue(entry.getValue().toPb());
236+
entityPb.addProperty(propertyPb.build());
237+
}
238+
populateEntityBuilder(entityPb);
239+
return entityPb.build();
240+
}
241+
242+
protected abstract void populateEntityBuilder(DatastoreV1.Entity.Builder entity);
243+
}

trunk/src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void testGetOptions() {
7575

7676
@Test
7777
public void testNewTransactionCommit() {
78+
// TODO (also try list value with different types)
7879
fail("Not yet implemented");
7980
}
8081

0 commit comments

Comments
 (0)