Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListValue: add static methods and builder setters for specific types #934

Merged
merged 1 commit into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.ArrayList;
import java.util.List;

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

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

/**
* Adds the provided values to the {@code ListValue} builder.
*/
public Builder addValue(Value<?> first, Value<?>... other) {
addValueHelper(first);
for (Value<?> value : other) {
Expand All @@ -88,7 +94,107 @@ public Builder addValue(Value<?> first, Value<?>... other) {
}

/**
* Copy the list of values.
* Adds the provided string values to the {@code ListValue} builder.
*/
public Builder addValue(String first, String... other) {
listBuilder.add(StringValue.of(first));
for (String value : other) {
listBuilder.add(StringValue.of(value));
}
return this;
}

/**
* Adds the provided long values to the {@code ListValue} builder.
*/
public Builder addValue(long first, long... other) {
listBuilder.add(LongValue.of(first));
for (long value : other) {
listBuilder.add(LongValue.of(value));
}
return this;
}

/**
* Adds the provided double values to the {@code ListValue} builder.
*/
public Builder addValue(double first, double... other) {
listBuilder.add(DoubleValue.of(first));
for (double value : other) {
listBuilder.add(DoubleValue.of(value));
}
return this;
}

/**
* Adds the provided boolean values to the {@code ListValue} builder.
*/
public Builder addValue(boolean first, boolean... other) {
listBuilder.add(BooleanValue.of(first));
for (boolean value : other) {
listBuilder.add(BooleanValue.of(value));
}
return this;
}

/**
* Adds the provided {@code DateTime} values to the {@code ListValue} builder.
*/
public Builder addValue(DateTime first, DateTime... other) {
listBuilder.add(DateTimeValue.of(first));
for (DateTime value : other) {
listBuilder.add(DateTimeValue.of(value));
}
return this;
}

/**
* Adds the provided {@code LatLng} values to the {@code ListValue} builder.
*/
public Builder addValue(LatLng first, LatLng... other) {
listBuilder.add(LatLngValue.of(first));
for (LatLng value : other) {
listBuilder.add(LatLngValue.of(value));
}
return this;
}

/**
* Adds the provided {@code Key} values to the {@code ListValue} builder.
*/
public Builder addValue(Key first, Key... other) {
listBuilder.add(KeyValue.of(first));
for (Key value : other) {
listBuilder.add(KeyValue.of(value));
}
return this;
}

/**
* Adds the provided {@code FullEntity} values to the {@code ListValue} builder.
*/
public Builder addValue(FullEntity<?> first, FullEntity<?>... other) {
listBuilder.add(EntityValue.of(first));
for (FullEntity<?> value : other) {
listBuilder.add(EntityValue.of(value));
}
return this;
}

/**
* Adds the provided {@code Blob} values to the {@code ListValue} builder.
*/
public Builder addValue(Blob first, Blob... other) {
listBuilder.add(BlobValue.of(first));
for (Blob value : other) {
listBuilder.add(BlobValue.of(value));
}
return this;
}

/**
* Sets the list of values of this {@code ListValue} builder to {@code values}. The provided
* list is copied.
*
* @see com.google.cloud.datastore.Value.BaseBuilder#set(java.lang.Object)
*/
Expand All @@ -106,6 +212,9 @@ public List<? extends Value<?>> get() {
return listBuilder.build();
}

/**
* Creates a {@code ListValue} object.
*/
@Override
public ListValue build() {
return new ListValue(this);
Expand All @@ -124,19 +233,94 @@ private ListValue(Builder builder) {
super(builder);
}

/**
* Returns a builder for the list value object.
*/
@Override
public Builder toBuilder() {
return new Builder().mergeFrom(this);
}

/**
* Creates a {@code ListValue} object given a list of {@code Value} objects.
*/
public static ListValue of(List<? extends Value<?>> values) {
return new ListValue(values);
}

/**
* Creates a {@code ListValue} object given a number of {@code Value} objects.
*/
public static ListValue of(Value<?> first, Value<?>... other) {
return new ListValue(first, other);
}

/**
* Creates a {@code ListValue} object given a number of string values.
*/
public static ListValue of(String first, String... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of long values.
*/
public static ListValue of(long first, long... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of double values.
*/
public static ListValue of(double first, double... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of boolean values.
*/
public static ListValue of(boolean first, boolean... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of {@code DateTime} values.
*/
public static ListValue of(DateTime first, DateTime... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of {@code LatLng} values.
*/
public static ListValue of(LatLng first, LatLng... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of {@code Key} values.
*/
public static ListValue of(Key first, Key... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of {@code FullEntity} values.
*/
public static ListValue of(FullEntity<?> first, FullEntity<?>... other) {
return builder().addValue(first, other).build();
}

/**
* Creates a {@code ListValue} object given a number of {@code Blob} values.
*/
public static ListValue of(Blob first, Blob... other) {
return builder().addValue(first, other).build();
}

/**
* Returns a builder for {@code ListValue} objects.
*/
public static Builder builder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public class ListValueTest {

private static final List<Value<?>> CONTENT =
ImmutableList.of(NullValue.of(), StringValue.of("foo"));
private static final String STRING1 = "string1";
private static final String STRING2 = "string2";
private static final long LONG1 = 1L;
private static final long LONG2 = 2L;
private static final double DOUBLE1 = 1.0;
private static final double DOUBLE2 = 2.0;
private static final boolean BOOLEAN1 = true;
private static final boolean BOOLEAN2 = false;
private static final DateTime DATETIME1 = new DateTime(1);
private static final DateTime DATETIME2 = new DateTime(2);
private static final LatLng LATLNG1 = LatLng.of(DOUBLE1, DOUBLE2);
private static final LatLng LATLNG2 = LatLng.of(DOUBLE2, DOUBLE1);
private static final Key KEY1 = Key.builder("project", "kind", "name1").build();
private static final Key KEY2 = Key.builder("project", "kind", "name2").build();
private static final FullEntity<Key> ENTITY1 = FullEntity.builder(KEY1).build();
private static final FullEntity<Key> ENTITY2 = FullEntity.builder(KEY2).build();
private static final Blob BLOB1 = Blob.copyFrom(new byte[]{0xD, 0xE, 0xA, 0xD});
private static final Blob BLOB2 = Blob.copyFrom(new byte[]{0xB, 0x0, 0x0, 0x0});

@Test
public void testToBuilder() throws Exception {
Expand All @@ -46,6 +64,44 @@ public void testOf() throws Exception {
value = ListValue.of(Collections.<Value<?>>emptyList());
assertEquals(Collections.<Value<?>>emptyList(), value.get());
assertFalse(value.excludeFromIndexes());
value = ListValue.of(STRING1);
assertEquals(ImmutableList.of(StringValue.of(STRING1)), value.get());
value = ListValue.of(STRING1, STRING2);
assertEquals(ImmutableList.of(StringValue.of(STRING1), StringValue.of(STRING2)), value.get());
value = ListValue.of(LONG1);
assertEquals(ImmutableList.of(LongValue.of(LONG1)), value.get());
value = ListValue.of(LONG1, LONG2);
assertEquals(ImmutableList.of(LongValue.of(LONG1), LongValue.of(LONG2)), value.get());
value = ListValue.of(DOUBLE1);
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1)), value.get());
value = ListValue.of(DOUBLE1, DOUBLE2);
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1), DoubleValue.of(DOUBLE2)), value.get());
value = ListValue.of(BOOLEAN1);
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1)), value.get());
value = ListValue.of(BOOLEAN1, BOOLEAN2);
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1), BooleanValue.of(BOOLEAN2)),
value.get());
value = ListValue.of(DATETIME1);
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1)), value.get());
value = ListValue.of(DATETIME1, DATETIME2);
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1), DateTimeValue.of(DATETIME2)),
value.get());
value = ListValue.of(LATLNG1);
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1)), value.get());
value = ListValue.of(LATLNG1, LATLNG2);
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1), LatLngValue.of(LATLNG2)), value.get());
value = ListValue.of(KEY1);
assertEquals(ImmutableList.of(KeyValue.of(KEY1)), value.get());
value = ListValue.of(KEY1, KEY2);
assertEquals(ImmutableList.of(KeyValue.of(KEY1), KeyValue.of(KEY2)), value.get());
value = ListValue.of(ENTITY1);
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1)), value.get());
value = ListValue.of(ENTITY1, ENTITY2);
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1), EntityValue.of(ENTITY2)), value.get());
value = ListValue.of(BLOB1);
assertEquals(ImmutableList.of(BlobValue.of(BLOB1)), value.get());
value = ListValue.of(BLOB1, BLOB2);
assertEquals(ImmutableList.of(BlobValue.of(BLOB1), BlobValue.of(BLOB2)), value.get());
}

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

builder = builder.set(Collections.<Value<?>>emptyList());
assertEquals(Collections.<Value<?>>emptyList(), builder.build().get());

builder = builder.addValue(STRING1);
assertEquals(ImmutableList.of(StringValue.of(STRING1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(STRING1, STRING2);
assertEquals(ImmutableList.of(StringValue.of(STRING1), StringValue.of(STRING2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(LONG1);
assertEquals(ImmutableList.of(LongValue.of(LONG1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(LONG1, LONG2);
assertEquals(ImmutableList.of(LongValue.of(LONG1), LongValue.of(LONG2)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(DOUBLE1);
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(DOUBLE1, DOUBLE2);
assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1), DoubleValue.of(DOUBLE2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(BOOLEAN1);
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(BOOLEAN1, BOOLEAN2);
assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1), BooleanValue.of(BOOLEAN2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(DATETIME1);
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(DATETIME1, DATETIME2);
assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1), DateTimeValue.of(DATETIME2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(LATLNG1);
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(LATLNG1, LATLNG2);
assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1), LatLngValue.of(LATLNG2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(KEY1);
assertEquals(ImmutableList.of(KeyValue.of(KEY1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(KEY1, KEY2);
assertEquals(ImmutableList.of(KeyValue.of(KEY1), KeyValue.of(KEY2)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(ENTITY1);
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(ENTITY1, ENTITY2);
assertEquals(ImmutableList.of(EntityValue.of(ENTITY1), EntityValue.of(ENTITY2)),
builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(BLOB1);
assertEquals(ImmutableList.of(BlobValue.of(BLOB1)), builder.build().get());
builder = builder.set(Collections.<Value<?>>emptyList());

builder = builder.addValue(BLOB1, BLOB2);
assertEquals(ImmutableList.of(BlobValue.of(BLOB1), BlobValue.of(BLOB2)), builder.build().get());
}
}