From 996aa476a830616b023864ed4c91e375d8265506 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 18 Apr 2016 10:59:30 +0200 Subject: [PATCH] ListValue: add static methods and builder setters for specific types --- .../com/google/cloud/datastore/ListValue.java | 186 +++++++++++++++++- .../google/cloud/datastore/ListValueTest.java | 133 +++++++++++++ 2 files changed, 318 insertions(+), 1 deletion(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/ListValue.java b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/ListValue.java index f3692e5395bd..d43b78053df7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/ListValue.java +++ b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/ListValue.java @@ -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>> { private static final long serialVersionUID = -5461475706792576395L; @@ -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) { @@ -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) */ @@ -106,6 +212,9 @@ public List> get() { return listBuilder.build(); } + /** + * Creates a {@code ListValue} object. + */ @Override public ListValue build() { return new ListValue(this); @@ -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> 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(); } diff --git a/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/ListValueTest.java b/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/ListValueTest.java index 47acc549d65d..567f2778dc38 100644 --- a/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/ListValueTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/ListValueTest.java @@ -31,6 +31,24 @@ public class ListValueTest { private static final List> 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 ENTITY1 = FullEntity.builder(KEY1).build(); + private static final FullEntity 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 { @@ -46,6 +64,44 @@ public void testOf() throws Exception { value = ListValue.of(Collections.>emptyList()); assertEquals(Collections.>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") @@ -65,5 +121,82 @@ public void testBuilder() throws Exception { builder = builder.set(Collections.>emptyList()); assertEquals(Collections.>emptyList(), builder.build().get()); + + builder = builder.addValue(STRING1); + assertEquals(ImmutableList.of(StringValue.of(STRING1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(STRING1, STRING2); + assertEquals(ImmutableList.of(StringValue.of(STRING1), StringValue.of(STRING2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(LONG1); + assertEquals(ImmutableList.of(LongValue.of(LONG1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(LONG1, LONG2); + assertEquals(ImmutableList.of(LongValue.of(LONG1), LongValue.of(LONG2)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(DOUBLE1); + assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(DOUBLE1, DOUBLE2); + assertEquals(ImmutableList.of(DoubleValue.of(DOUBLE1), DoubleValue.of(DOUBLE2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(BOOLEAN1); + assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(BOOLEAN1, BOOLEAN2); + assertEquals(ImmutableList.of(BooleanValue.of(BOOLEAN1), BooleanValue.of(BOOLEAN2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(DATETIME1); + assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(DATETIME1, DATETIME2); + assertEquals(ImmutableList.of(DateTimeValue.of(DATETIME1), DateTimeValue.of(DATETIME2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(LATLNG1); + assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(LATLNG1, LATLNG2); + assertEquals(ImmutableList.of(LatLngValue.of(LATLNG1), LatLngValue.of(LATLNG2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(KEY1); + assertEquals(ImmutableList.of(KeyValue.of(KEY1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(KEY1, KEY2); + assertEquals(ImmutableList.of(KeyValue.of(KEY1), KeyValue.of(KEY2)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(ENTITY1); + assertEquals(ImmutableList.of(EntityValue.of(ENTITY1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(ENTITY1, ENTITY2); + assertEquals(ImmutableList.of(EntityValue.of(ENTITY1), EntityValue.of(ENTITY2)), + builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(BLOB1); + assertEquals(ImmutableList.of(BlobValue.of(BLOB1)), builder.build().get()); + builder = builder.set(Collections.>emptyList()); + + builder = builder.addValue(BLOB1, BLOB2); + assertEquals(ImmutableList.of(BlobValue.of(BLOB1), BlobValue.of(BLOB2)), builder.build().get()); } }