|
| 1 | +package com.google.gcloud.datastore; |
| 2 | + |
| 3 | +import static com.google.api.services.datastore.DatastoreV1.Value.LIST_VALUE_FIELD_NUMBER; |
| 4 | + |
| 5 | +import com.google.api.services.datastore.DatastoreV1; |
| 6 | +import com.google.common.base.Preconditions; |
| 7 | +import com.google.common.collect.ImmutableList; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public final class ListValue extends |
| 13 | + Value<List<Value<?, ?, ?>>, ListValue, ListValue.Builder> { |
| 14 | + |
| 15 | + private static final long serialVersionUID = -5461475706792576395L; |
| 16 | + |
| 17 | + static final BaseMarshaller<List<Value<?, ?, ?>>, ListValue, Builder> MARSHALLER = |
| 18 | + new BaseMarshaller<List<Value<?, ?, ?>>, ListValue, Builder>() { |
| 19 | + |
| 20 | + @Override |
| 21 | + public int getProtoFieldId() { |
| 22 | + return LIST_VALUE_FIELD_NUMBER; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public Builder newBuilder(List<Value<?, ?, ?>> values) { |
| 27 | + return new Builder().set(values); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected List<Value<?, ?, ?>> getValue(DatastoreV1.Value from) { |
| 32 | + List<Value<?, ?, ?>> properties = new ArrayList<>(from.getListValueCount()); |
| 33 | + for (DatastoreV1.Value valuePb : from.getListValueList()) { |
| 34 | + properties.add(Value.fromPb(valuePb)); |
| 35 | + } |
| 36 | + return properties; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void setValue(ListValue from, DatastoreV1.Value.Builder to) { |
| 41 | + for (Value<?, ?, ?> property : from.get()) { |
| 42 | + to.addListValue(property.toPb()); |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + public static final class Builder extends |
| 48 | + Value.BaseBuilder<List<Value<?, ?, ?>>, ListValue, Builder> { |
| 49 | + |
| 50 | + private ImmutableList.Builder<Value<?, ?, ?>> listBuilder = ImmutableList.builder(); |
| 51 | + |
| 52 | + public Builder() { |
| 53 | + super(Type.LIST); |
| 54 | + indexed(false); |
| 55 | + } |
| 56 | + |
| 57 | + public Builder addValue(Value<?, ?, ?> value) { |
| 58 | + Preconditions.checkArgument(value.getType() != Type.LIST, "Cannot contain another list"); |
| 59 | + listBuilder.add(value); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public Builder addValue(Value<?, ?, ?> first, Value<?, ?, ?>... other) { |
| 64 | + addValue(first); |
| 65 | + for (Value<?, ?, ?> value : other) { |
| 66 | + addValue(value); |
| 67 | + } |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Builder set(List<Value<?, ?, ?>> properties) { |
| 73 | + listBuilder = ImmutableList.<Value<?, ?, ?>>builder(); |
| 74 | + for (Value<?, ?, ?> property : properties) { |
| 75 | + addValue(property); |
| 76 | + } |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public List<Value<?, ?, ?>> get() { |
| 82 | + return listBuilder.build(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ListValue build() { |
| 87 | + Preconditions.checkState(!get().isEmpty(), "value list could not be empty"); |
| 88 | + return new ListValue(this); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public ListValue(List<Value<?, ?, ?>> properties) { |
| 93 | + this(new Builder().set(properties)); |
| 94 | + } |
| 95 | + |
| 96 | + public ListValue(Value<?, ?, ?> first, Value<?, ?, ?>... other) { |
| 97 | + this(new Builder().addValue(first, other)); |
| 98 | + } |
| 99 | + |
| 100 | + ListValue(Builder builder) { |
| 101 | + super(builder); |
| 102 | + } |
| 103 | +} |
0 commit comments