Skip to content

Commit a68a0b6

Browse files
author
Ajay Kannan
committed
Add set methods for lists of same type
1 parent 7864a93 commit a68a0b6

3 files changed

Lines changed: 110 additions & 8 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.protobuf.InvalidProtocolBufferException;
3434

3535
import java.util.HashMap;
36+
import java.util.LinkedList;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.Objects;
@@ -138,43 +139,120 @@ public B set(String name, String value) {
138139
return self();
139140
}
140141

142+
public B set(String name, String first, String second, String... others) {
143+
List<StringValue> values = new LinkedList<>();
144+
values.add(of(first));
145+
values.add(of(second));
146+
for (String other : others) {
147+
values.add(of(other));
148+
}
149+
properties.put(name, of(values));
150+
return self();
151+
}
152+
141153
public B set(String name, long value) {
142154
properties.put(name, of(value));
143155
return self();
144156
}
145157

158+
public B set(String name, long first, long second, long... others) {
159+
List<LongValue> values = new LinkedList<>();
160+
values.add(of(first));
161+
values.add(of(second));
162+
for (long other : others) {
163+
values.add(of(other));
164+
}
165+
properties.put(name, of(values));
166+
return self();
167+
}
168+
146169
public B set(String name, double value) {
147170
properties.put(name, of(value));
148171
return self();
149172
}
150173

174+
public B set(String name, double first, double second, double... others) {
175+
List<DoubleValue> values = new LinkedList<>();
176+
values.add(of(first));
177+
values.add(of(second));
178+
for (double other : others) {
179+
values.add(of(other));
180+
}
181+
properties.put(name, of(values));
182+
return self();
183+
}
184+
151185
public B set(String name, boolean value) {
152186
properties.put(name, of(value));
153187
return self();
154188
}
155189

190+
public B set(String name, boolean first, boolean second, boolean... others) {
191+
List<BooleanValue> values = new LinkedList<>();
192+
values.add(of(first));
193+
values.add(of(second));
194+
for (boolean other : others) {
195+
values.add(of(other));
196+
}
197+
properties.put(name, of(values));
198+
return self();
199+
}
200+
156201
public B set(String name, DateTime value) {
157202
properties.put(name, of(value));
158203
return self();
159204
}
160205

206+
public B set(String name, DateTime first, DateTime second, DateTime... others) {
207+
List<DateTimeValue> values = new LinkedList<>();
208+
values.add(of(first));
209+
values.add(of(second));
210+
for (DateTime other : others) {
211+
values.add(of(other));
212+
}
213+
properties.put(name, of(values));
214+
return self();
215+
}
216+
161217
public B set(String name, Key value) {
162218
properties.put(name, of(value));
163219
return self();
164220
}
165221

222+
public B set(String name, Key first, Key second, Key... others) {
223+
List<KeyValue> values = new LinkedList<>();
224+
values.add(of(first));
225+
values.add(of(second));
226+
for (Key other : others) {
227+
values.add(of(other));
228+
}
229+
properties.put(name, of(values));
230+
return self();
231+
}
232+
166233
public B set(String name, FullEntity<?> value) {
167234
properties.put(name, of(value));
168235
return self();
169236
}
170237

238+
public B set(String name, FullEntity<?> first, FullEntity<?> second, FullEntity<?>... others) {
239+
List<EntityValue> values = new LinkedList<>();
240+
values.add(of(first));
241+
values.add(of(second));
242+
for (FullEntity<?> other : others) {
243+
values.add(of(other));
244+
}
245+
properties.put(name, of(values));
246+
return self();
247+
}
248+
171249
public B set(String name, List<? extends Value<?>> values) {
172250
properties.put(name, of(values));
173251
return self();
174252
}
175253

176-
public B set(String name, Value<?> value, Value<?>... other) {
177-
properties.put(name, of(value, other));
254+
public B set(String name, Value<?> first, Value<?> second, Value<?>... other) {
255+
properties.put(name, of(first, second, other));
178256
return self();
179257
}
180258

@@ -183,6 +261,17 @@ public B set(String name, Blob value) {
183261
return self();
184262
}
185263

264+
public B set(String name, Blob first, Blob second, Blob... others) {
265+
List<BlobValue> values = new LinkedList<>();
266+
values.add(of(first));
267+
values.add(of(second));
268+
for (Blob other : others) {
269+
values.add(of(other));
270+
}
271+
properties.put(name, of(values));
272+
return self();
273+
}
274+
186275
public B setNull(String name) {
187276
properties.put(name, of());
188277
return self();

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ public Builder addValue(Value<?> value) {
7777
return this;
7878
}
7979

80-
public Builder addValue(Value<?> first, Value<?>... other) {
80+
public Builder addValue(Value<?> first, Value<?> second, Value<?>... other) {
8181
addValue(first);
82+
addValue(second);
8283
for (Value<?> value : other) {
8384
addValue(value);
8485
}
@@ -121,8 +122,8 @@ public ListValue(List<? extends Value<?>> values) {
121122
this(builder().set(values));
122123
}
123124

124-
public ListValue(Value<?> first, Value<?>... other) {
125-
this(new Builder().addValue(first, other));
125+
public ListValue(Value<?> first, Value<?> second, Value<?>... other) {
126+
this(new Builder().addValue(first, second, other));
126127
}
127128

128129
private ListValue(Builder builder) {
@@ -138,8 +139,8 @@ public static ListValue of(List<? extends Value<?>> values) {
138139
return new ListValue(values);
139140
}
140141

141-
public static ListValue of(Value<?> first, Value<?>... other) {
142-
return new ListValue(first, other);
142+
public static ListValue of(Value<?> first, Value<?> second, Value<?>... other) {
143+
return new ListValue(first, second, other);
143144
}
144145

145146
public static Builder builder() {

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseEntityTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public void setUp() {
6767
builder.set("list1", NullValue.of(), StringValue.of("foo"));
6868
builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2)));
6969
builder.set("list3", Collections.singletonList(BooleanValue.of(true)));
70+
builder.set(
71+
"blobList", BLOB, Blob.copyFrom(new byte[] {3, 4}), Blob.copyFrom(new byte[] {5, 6}));
72+
builder.set("booleanList", true, false, true);
73+
builder.set("dateTimeList", DateTime.now(), DateTime.now(), DateTime.now());
74+
builder.set("doubleList", 12.3, 4.56, .789);
75+
builder.set("keyList", KEY, Key.builder("ds2", "k2", "n2").build(),
76+
Key.builder("ds3", "k3", "n3").build());
77+
builder.set("entityList", ENTITY, PARTIAL_ENTITY, ENTITY);
78+
builder.set("stringList", "s1", "s2", "s3");
79+
builder.set("longList", 1, 23, 456);
7080
}
7181

7282
@Test
@@ -198,7 +208,9 @@ public void testGetBlob() throws Exception {
198208
public void testNames() throws Exception {
199209
Set<String> names = ImmutableSet.<String>builder()
200210
.add("string", "stringValue", "boolean", "double", "long", "list1", "list2", "list3")
201-
.add("entity", "partialEntity", "null", "dateTime", "blob", "key")
211+
.add("entity", "partialEntity", "null", "dateTime", "blob", "key", "blobList")
212+
.add("booleanList", "dateTimeList", "doubleList", "keyList", "entityList", "stringList")
213+
.add("longList")
202214
.build();
203215
BaseEntity<Key> entity = builder.build();
204216
assertEquals(names, entity.names());

0 commit comments

Comments
 (0)