Skip to content

Commit d18e7bb

Browse files
committed
Merge pull request #934 from mziccard/datastore-list-value
ListValue: add static methods and builder setters for specific types
2 parents 9de36ed + 996aa47 commit d18e7bb

File tree

2 files changed

+318
-1
lines changed

2 files changed

+318
-1
lines changed

gcloud-java-datastore/src/main/java/com/google/cloud/datastore/ListValue.java

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27+
/**
28+
* A Google Cloud Datastore list value. A list value is a list of {@link Value} objects.
29+
*/
2730
public final class ListValue extends Value<List<? extends Value<?>>> {
2831

2932
private static final long serialVersionUID = -5461475706792576395L;
@@ -79,6 +82,9 @@ private void addValueHelper(Value<?> value) {
7982
listBuilder.add(value);
8083
}
8184

85+
/**
86+
* Adds the provided values to the {@code ListValue} builder.
87+
*/
8288
public Builder addValue(Value<?> first, Value<?>... other) {
8389
addValueHelper(first);
8490
for (Value<?> value : other) {
@@ -88,7 +94,107 @@ public Builder addValue(Value<?> first, Value<?>... other) {
8894
}
8995

9096
/**
91-
* Copy the list of values.
97+
* Adds the provided string values to the {@code ListValue} builder.
98+
*/
99+
public Builder addValue(String first, String... other) {
100+
listBuilder.add(StringValue.of(first));
101+
for (String value : other) {
102+
listBuilder.add(StringValue.of(value));
103+
}
104+
return this;
105+
}
106+
107+
/**
108+
* Adds the provided long values to the {@code ListValue} builder.
109+
*/
110+
public Builder addValue(long first, long... other) {
111+
listBuilder.add(LongValue.of(first));
112+
for (long value : other) {
113+
listBuilder.add(LongValue.of(value));
114+
}
115+
return this;
116+
}
117+
118+
/**
119+
* Adds the provided double values to the {@code ListValue} builder.
120+
*/
121+
public Builder addValue(double first, double... other) {
122+
listBuilder.add(DoubleValue.of(first));
123+
for (double value : other) {
124+
listBuilder.add(DoubleValue.of(value));
125+
}
126+
return this;
127+
}
128+
129+
/**
130+
* Adds the provided boolean values to the {@code ListValue} builder.
131+
*/
132+
public Builder addValue(boolean first, boolean... other) {
133+
listBuilder.add(BooleanValue.of(first));
134+
for (boolean value : other) {
135+
listBuilder.add(BooleanValue.of(value));
136+
}
137+
return this;
138+
}
139+
140+
/**
141+
* Adds the provided {@code DateTime} values to the {@code ListValue} builder.
142+
*/
143+
public Builder addValue(DateTime first, DateTime... other) {
144+
listBuilder.add(DateTimeValue.of(first));
145+
for (DateTime value : other) {
146+
listBuilder.add(DateTimeValue.of(value));
147+
}
148+
return this;
149+
}
150+
151+
/**
152+
* Adds the provided {@code LatLng} values to the {@code ListValue} builder.
153+
*/
154+
public Builder addValue(LatLng first, LatLng... other) {
155+
listBuilder.add(LatLngValue.of(first));
156+
for (LatLng value : other) {
157+
listBuilder.add(LatLngValue.of(value));
158+
}
159+
return this;
160+
}
161+
162+
/**
163+
* Adds the provided {@code Key} values to the {@code ListValue} builder.
164+
*/
165+
public Builder addValue(Key first, Key... other) {
166+
listBuilder.add(KeyValue.of(first));
167+
for (Key value : other) {
168+
listBuilder.add(KeyValue.of(value));
169+
}
170+
return this;
171+
}
172+
173+
/**
174+
* Adds the provided {@code FullEntity} values to the {@code ListValue} builder.
175+
*/
176+
public Builder addValue(FullEntity<?> first, FullEntity<?>... other) {
177+
listBuilder.add(EntityValue.of(first));
178+
for (FullEntity<?> value : other) {
179+
listBuilder.add(EntityValue.of(value));
180+
}
181+
return this;
182+
}
183+
184+
/**
185+
* Adds the provided {@code Blob} values to the {@code ListValue} builder.
186+
*/
187+
public Builder addValue(Blob first, Blob... other) {
188+
listBuilder.add(BlobValue.of(first));
189+
for (Blob value : other) {
190+
listBuilder.add(BlobValue.of(value));
191+
}
192+
return this;
193+
}
194+
195+
/**
196+
* Sets the list of values of this {@code ListValue} builder to {@code values}. The provided
197+
* list is copied.
92198
*
93199
* @see com.google.cloud.datastore.Value.BaseBuilder#set(java.lang.Object)
94200
*/
@@ -106,6 +212,9 @@ public List<? extends Value<?>> get() {
106212
return listBuilder.build();
107213
}
108214

215+
/**
216+
* Creates a {@code ListValue} object.
217+
*/
109218
@Override
110219
public ListValue build() {
111220
return new ListValue(this);
@@ -124,19 +233,94 @@ private ListValue(Builder builder) {
124233
super(builder);
125234
}
126235

236+
/**
237+
* Returns a builder for the list value object.
238+
*/
127239
@Override
128240
public Builder toBuilder() {
129241
return new Builder().mergeFrom(this);
130242
}
131243

244+
/**
245+
* Creates a {@code ListValue} object given a list of {@code Value} objects.
246+
*/
132247
public static ListValue of(List<? extends Value<?>> values) {
133248
return new ListValue(values);
134249
}
135250

251+
/**
252+
* Creates a {@code ListValue} object given a number of {@code Value} objects.
253+
*/
136254
public static ListValue of(Value<?> first, Value<?>... other) {
137255
return new ListValue(first, other);
138256
}
139257

258+
/**
259+
* Creates a {@code ListValue} object given a number of string values.
260+
*/
261+
public static ListValue of(String first, String... other) {
262+
return builder().addValue(first, other).build();
263+
}
264+
265+
/**
266+
* Creates a {@code ListValue} object given a number of long values.
267+
*/
268+
public static ListValue of(long first, long... other) {
269+
return builder().addValue(first, other).build();
270+
}
271+
272+
/**
273+
* Creates a {@code ListValue} object given a number of double values.
274+
*/
275+
public static ListValue of(double first, double... other) {
276+
return builder().addValue(first, other).build();
277+
}
278+
279+
/**
280+
* Creates a {@code ListValue} object given a number of boolean values.
281+
*/
282+
public static ListValue of(boolean first, boolean... other) {
283+
return builder().addValue(first, other).build();
284+
}
285+
286+
/**
287+
* Creates a {@code ListValue} object given a number of {@code DateTime} values.
288+
*/
289+
public static ListValue of(DateTime first, DateTime... other) {
290+
return builder().addValue(first, other).build();
291+
}
292+
293+
/**
294+
* Creates a {@code ListValue} object given a number of {@code LatLng} values.
295+
*/
296+
public static ListValue of(LatLng first, LatLng... other) {
297+
return builder().addValue(first, other).build();
298+
}
299+
300+
/**
301+
* Creates a {@code ListValue} object given a number of {@code Key} values.
302+
*/
303+
public static ListValue of(Key first, Key... other) {
304+
return builder().addValue(first, other).build();
305+
}
306+
307+
/**
308+
* Creates a {@code ListValue} object given a number of {@code FullEntity} values.
309+
*/
310+
public static ListValue of(FullEntity<?> first, FullEntity<?>... other) {
311+
return builder().addValue(first, other).build();
312+
}
313+
314+
/**
315+
* Creates a {@code ListValue} object given a number of {@code Blob} values.
316+
*/
317+
public static ListValue of(Blob first, Blob... other) {
318+
return builder().addValue(first, other).build();
319+
}
320+
321+
/**
322+
* Returns a builder for {@code ListValue} objects.
323+
*/
140324
public static Builder builder() {
141325
return new Builder();
142326
}

gcloud-java-datastore/src/test/java/com/google/cloud/datastore/ListValueTest.java

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public class ListValueTest {
3131

3232
private static final List<Value<?>> CONTENT =
3333
ImmutableList.of(NullValue.of(), StringValue.of("foo"));
34+
private static final String STRING1 = "string1";
35+
private static final String STRING2 = "string2";
36+
private static final long LONG1 = 1L;
37+
private static final long LONG2 = 2L;
38+
private static final double DOUBLE1 = 1.0;
39+
private static final double DOUBLE2 = 2.0;
40+
private static final boolean BOOLEAN1 = true;
41+
private static final boolean BOOLEAN2 = false;
42+
private static final DateTime DATETIME1 = new DateTime(1);
43+
private static final DateTime DATETIME2 = new DateTime(2);
44+
private static final LatLng LATLNG1 = LatLng.of(DOUBLE1, DOUBLE2);
45+
private static final LatLng LATLNG2 = LatLng.of(DOUBLE2, DOUBLE1);
46+
private static final Key KEY1 = Key.builder("project", "kind", "name1").build();
47+
private static final Key KEY2 = Key.builder("project", "kind", "name2").build();
48+
private static final FullEntity<Key> ENTITY1 = FullEntity.builder(KEY1).build();
49+
private static final FullEntity<Key> ENTITY2 = FullEntity.builder(KEY2).build();
50+
private static final Blob BLOB1 = Blob.copyFrom(new byte[]{0xD, 0xE, 0xA, 0xD});
51+
private static final Blob BLOB2 = Blob.copyFrom(new byte[]{0xB, 0x0, 0x0, 0x0});
3452

3553
@Test
3654
public void testToBuilder() throws Exception {
@@ -46,6 +64,44 @@ public void testOf() throws Exception {
4664
value = ListValue.of(Collections.<Value<?>>emptyList());
4765
assertEquals(Collections.<Value<?>>emptyList(), value.get());
4866
assertFalse(value.excludeFromIndexes());
67+
value = ListValue.of(STRING1);
68+
assertEquals(ImmutableList.of(StringValue.of(STRING1)), value.get());
69+
value = ListValue.of(STRING1, STRING2);
70+
assertEquals(ImmutableList.of(StringValue.of(STRING1), StringValue.of(STRING2)), value.get());
71+
value = ListValue.of(LONG1);
72+
assertEquals(ImmutableList.of(LongValue.of(LONG1)), value.get());
73+
value = ListValue.of(LONG1, LONG2);
74+
assertEquals(ImmutableList.of(LongValue.of(LONG1), LongValue.of(LONG2)), value.get());
75+
value = ListValue.of(DOUBLE1);
76+
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1)), value.get());
77+
value = ListValue.of(DOUBLE1, DOUBLE2);
78+
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1), DoubleValue.of(DOUBLE2)), value.get());
79+
value = ListValue.of(BOOLEAN1);
80+
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1)), value.get());
81+
value = ListValue.of(BOOLEAN1, BOOLEAN2);
82+
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1), BooleanValue.of(BOOLEAN2)),
83+
value.get());
84+
value = ListValue.of(DATETIME1);
85+
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1)), value.get());
86+
value = ListValue.of(DATETIME1, DATETIME2);
87+
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1), DateTimeValue.of(DATETIME2)),
88+
value.get());
89+
value = ListValue.of(LATLNG1);
90+
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1)), value.get());
91+
value = ListValue.of(LATLNG1, LATLNG2);
92+
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1), LatLngValue.of(LATLNG2)), value.get());
93+
value = ListValue.of(KEY1);
94+
assertEquals(ImmutableList.of(KeyValue.of(KEY1)), value.get());
95+
value = ListValue.of(KEY1, KEY2);
96+
assertEquals(ImmutableList.of(KeyValue.of(KEY1), KeyValue.of(KEY2)), value.get());
97+
value = ListValue.of(ENTITY1);
98+
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1)), value.get());
99+
value = ListValue.of(ENTITY1, ENTITY2);
100+
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1), EntityValue.of(ENTITY2)), value.get());
101+
value = ListValue.of(BLOB1);
102+
assertEquals(ImmutableList.of(BlobValue.of(BLOB1)), value.get());
103+
value = ListValue.of(BLOB1, BLOB2);
104+
assertEquals(ImmutableList.of(BlobValue.of(BLOB1), BlobValue.of(BLOB2)), value.get());
49105
}
50106

51107
@SuppressWarnings("deprecation")
@@ -65,5 +121,82 @@ public void testBuilder() throws Exception {
65121

66122
builder = builder.set(Collections.<Value<?>>emptyList());
67123
assertEquals(Collections.<Value<?>>emptyList(), builder.build().get());
124+
125+
builder = builder.addValue(STRING1);
126+
assertEquals(ImmutableList.of(StringValue.of(STRING1)), builder.build().get());
127+
builder = builder.set(Collections.<Value<?>>emptyList());
128+
129+
builder = builder.addValue(STRING1, STRING2);
130+
assertEquals(ImmutableList.of(StringValue.of(STRING1), StringValue.of(STRING2)),
131+
builder.build().get());
132+
builder = builder.set(Collections.<Value<?>>emptyList());
133+
134+
builder = builder.addValue(LONG1);
135+
assertEquals(ImmutableList.of(LongValue.of(LONG1)), builder.build().get());
136+
builder = builder.set(Collections.<Value<?>>emptyList());
137+
138+
builder = builder.addValue(LONG1, LONG2);
139+
assertEquals(ImmutableList.of(LongValue.of(LONG1), LongValue.of(LONG2)), builder.build().get());
140+
builder = builder.set(Collections.<Value<?>>emptyList());
141+
142+
builder = builder.addValue(DOUBLE1);
143+
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1)), builder.build().get());
144+
builder = builder.set(Collections.<Value<?>>emptyList());
145+
146+
builder = builder.addValue(DOUBLE1, DOUBLE2);
147+
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1), DoubleValue.of(DOUBLE2)),
148+
builder.build().get());
149+
builder = builder.set(Collections.<Value<?>>emptyList());
150+
151+
builder = builder.addValue(BOOLEAN1);
152+
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1)), builder.build().get());
153+
builder = builder.set(Collections.<Value<?>>emptyList());
154+
155+
builder = builder.addValue(BOOLEAN1, BOOLEAN2);
156+
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1), BooleanValue.of(BOOLEAN2)),
157+
builder.build().get());
158+
builder = builder.set(Collections.<Value<?>>emptyList());
159+
160+
builder = builder.addValue(DATETIME1);
161+
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1)), builder.build().get());
162+
builder = builder.set(Collections.<Value<?>>emptyList());
163+
164+
builder = builder.addValue(DATETIME1, DATETIME2);
165+
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1), DateTimeValue.of(DATETIME2)),
166+
builder.build().get());
167+
builder = builder.set(Collections.<Value<?>>emptyList());
168+
169+
builder = builder.addValue(LATLNG1);
170+
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1)), builder.build().get());
171+
builder = builder.set(Collections.<Value<?>>emptyList());
172+
173+
builder = builder.addValue(LATLNG1, LATLNG2);
174+
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1), LatLngValue.of(LATLNG2)),
175+
builder.build().get());
176+
builder = builder.set(Collections.<Value<?>>emptyList());
177+
178+
builder = builder.addValue(KEY1);
179+
assertEquals(ImmutableList.of(KeyValue.of(KEY1)), builder.build().get());
180+
builder = builder.set(Collections.<Value<?>>emptyList());
181+
182+
builder = builder.addValue(KEY1, KEY2);
183+
assertEquals(ImmutableList.of(KeyValue.of(KEY1), KeyValue.of(KEY2)), builder.build().get());
184+
builder = builder.set(Collections.<Value<?>>emptyList());
185+
186+
builder = builder.addValue(ENTITY1);
187+
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1)), builder.build().get());
188+
builder = builder.set(Collections.<Value<?>>emptyList());
189+
190+
builder = builder.addValue(ENTITY1, ENTITY2);
191+
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1), EntityValue.of(ENTITY2)),
192+
builder.build().get());
193+
builder = builder.set(Collections.<Value<?>>emptyList());
194+
195+
builder = builder.addValue(BLOB1);
196+
assertEquals(ImmutableList.of(BlobValue.of(BLOB1)), builder.build().get());
197+
builder = builder.set(Collections.<Value<?>>emptyList());
198+
199+
builder = builder.addValue(BLOB1, BLOB2);
200+
assertEquals(ImmutableList.of(BlobValue.of(BLOB1), BlobValue.of(BLOB2)), builder.build().get());
68201
}
69202
}

0 commit comments

Comments
 (0)