From 712b255f40bfd67128edd4f2b287c7a7193156fa Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 29 Mar 2016 17:57:35 +0200 Subject: [PATCH 1/3] Add common interface for field selectors --- .../com/google/gcloud/bigquery/BigQuery.java | 66 +++++-------- .../com/google/gcloud/bigquery/Option.java | 24 ++++- .../java/com/google/gcloud/FieldSelector.java | 30 ++++++ .../com/google/gcloud/dns/AbstractOption.java | 21 ++++ .../main/java/com/google/gcloud/dns/Dns.java | 97 +++++++------------ .../google/gcloud/resourcemanager/Option.java | 21 ++++ .../resourcemanager/ResourceManager.java | 27 +++--- .../com/google/gcloud/storage/Option.java | 21 ++++ .../com/google/gcloud/storage/Storage.java | 50 ++++------ 9 files changed, 205 insertions(+), 152 deletions(-) create mode 100644 gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 14e324a43370..a0724b2026b2 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -19,16 +19,14 @@ import static com.google.common.base.Preconditions.checkArgument; import com.google.common.base.Function; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.util.List; -import java.util.Set; /** * An interface for Google Cloud BigQuery. @@ -43,7 +41,7 @@ public interface BigQuery extends Service { * @see Dataset * Resource */ - enum DatasetField { + enum DatasetField implements FieldSelector { ACCESS("access"), CREATION_TIME("creationTime"), DATASET_REFERENCE("datasetReference"), @@ -56,24 +54,19 @@ enum DatasetField { LOCATION("location"), SELF_LINK("selfLink"); + static final List REQUIRED_FIELDS = + ImmutableList.of(DATASET_REFERENCE); + private final String selector; DatasetField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(DatasetField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(DATASET_REFERENCE.selector()); - for (DatasetField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -82,7 +75,7 @@ static String selector(DatasetField... fields) { * @see Table * Resource */ - enum TableField { + enum TableField implements FieldSelector { CREATION_TIME("creationTime"), DESCRIPTION("description"), ETAG("etag"), @@ -101,25 +94,19 @@ enum TableField { TYPE("type"), VIEW("view"); + static final List REQUIRED_FIELDS = + ImmutableList.of(TABLE_REFERENCE, TYPE); + private final String selector; TableField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(TableField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(TABLE_REFERENCE.selector()); - fieldStrings.add(TYPE.selector()); - for (TableField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -128,7 +115,7 @@ static String selector(TableField... fields) { * @see Job Resource * */ - enum JobField { + enum JobField implements FieldSelector { CONFIGURATION("configuration"), ETAG("etag"), ID("id"), @@ -138,25 +125,19 @@ enum JobField { STATUS("status"), USER_EMAIL("user_email"); + static final List REQUIRED_FIELDS = + ImmutableList.of(JOB_REFERENCE, CONFIGURATION); + private final String selector; JobField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(JobField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(JOB_REFERENCE.selector()); - fieldStrings.add(CONFIGURATION.selector()); - for (JobField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -210,7 +191,8 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { * returned, even if not specified. */ public static DatasetOption fields(DatasetField... fields) { - return new DatasetOption(BigQueryRpc.Option.FIELDS, DatasetField.selector(fields)); + return new DatasetOption(BigQueryRpc.Option.FIELDS, + selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -279,7 +261,8 @@ private TableOption(BigQueryRpc.Option option, Object value) { * of {@link Table#definition()}) are always returned, even if not specified. */ public static TableOption fields(TableField... fields) { - return new TableOption(BigQueryRpc.Option.FIELDS, TableField.selector(fields)); + return new TableOption(BigQueryRpc.Option.FIELDS, + selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -376,9 +359,9 @@ public static JobListOption pageToken(String pageToken) { * listing jobs. */ public static JobListOption fields(JobField... fields) { - String selector = JobField.selector(fields); - StringBuilder builder = new StringBuilder(); - builder.append("etag,jobs(").append(selector).append(",state,errorResult),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("etag,jobs("), JobField.REQUIRED_FIELDS, fields) + .append(",state,errorResult),nextPageToken"); return new JobListOption(BigQueryRpc.Option.FIELDS, builder.toString()); } } @@ -402,7 +385,8 @@ private JobOption(BigQueryRpc.Option option, Object value) { * returned, even if not specified. */ public static JobOption fields(JobField... fields) { - return new JobOption(BigQueryRpc.Option.FIELDS, JobField.selector(fields)); + return new JobOption(BigQueryRpc.Option.FIELDS, + Option.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java index 3fdc27ecab99..9b123fc1c89c 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for BigQuery operation option. @@ -53,8 +58,7 @@ public boolean equals(Object obj) { return false; } Option other = (Option) obj; - return Objects.equals(rpcOption, other.rpcOption) - && Objects.equals(value, other.value); + return Objects.equals(rpcOption, other.rpcOption) && Objects.equals(value, other.value); } @Override @@ -69,4 +73,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java new file mode 100644 index 000000000000..358c4d4798f6 --- /dev/null +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +/** + * Interface for Google Cloud resource's fields. Implementations of this interface can be used to + * select only desired fields when getting or listing Google Cloud resources. + */ +public interface FieldSelector { + + /** + * Returns a string selector. This selector is passed to a Google Cloud service (possibly with + * other field selectors) to specify which resource fields should be returned by an API call. + */ + String selector(); +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java index e12f7412e687..43dfe69df213 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * A base class for options. @@ -67,4 +72,20 @@ public String toString() { .add("rpcOption", rpcOption) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index f2b42f30a9f6..ee0611279d27 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -16,14 +16,14 @@ package com.google.gcloud.dns; -import com.google.common.base.Joiner; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; -import java.util.Set; +import java.util.List; /** * An interface for the Google Cloud DNS service. @@ -39,29 +39,23 @@ public interface Dns extends Service { * {@link Dns#getProject(ProjectOption...)}. Project ID is always returned, even if not * specified. */ - enum ProjectField { + enum ProjectField implements FieldSelector { PROJECT_ID("id"), PROJECT_NUMBER("number"), QUOTA("quota"); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + private final String selector; ProjectField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ProjectField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(PROJECT_ID.selector()); - for (ProjectField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -71,7 +65,7 @@ static String selector(ProjectField... fields) { * {@link Dns#getZone(String, ZoneOption...)}. The name is always returned, even if not * specified. */ - enum ZoneField { + enum ZoneField implements FieldSelector { CREATION_TIME("creationTime"), DESCRIPTION("description"), DNS_NAME("dnsName"), @@ -80,24 +74,18 @@ enum ZoneField { NAME_SERVER_SET("nameServerSet"), NAME_SERVERS("nameServers"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + private final String selector; ZoneField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ZoneField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - for (ZoneField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -107,31 +95,24 @@ static String selector(ZoneField... fields) { * {@link Dns#listRecordSets(String, RecordSetListOption...)}. The name and type are always * returned even if not selected. */ - enum RecordSetField { + enum RecordSetField implements FieldSelector { DNS_RECORDS("rrdatas"), NAME("name"), TTL("ttl"), TYPE("type"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); + private final String selector; RecordSetField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(RecordSetField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - fieldStrings.add(TYPE.selector()); - for (RecordSetField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -141,31 +122,25 @@ static String selector(RecordSetField... fields) { * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always * returned even if not selected. */ - enum ChangeRequestField { + enum ChangeRequestField implements FieldSelector { ID("id"), START_TIME("startTime"), STATUS("status"), ADDITIONS("additions"), DELETIONS("deletions"); + static final List REQUIRED_FIELDS = ImmutableList.of(ID); + private final String selector; ChangeRequestField(String selector) { this.selector = selector; } - String selector() { + @Override + public String selector() { return selector; } - - static String selector(ChangeRequestField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(ID.selector()); - for (ChangeRequestField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -199,9 +174,8 @@ class RecordSetListOption extends AbstractOption implements Serializable { * of fields that can be used. */ public static RecordSetListOption fields(RecordSetField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,rrsets(").append(RecordSetField.selector(fields)) - .append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,rrsets("), + RecordSetField.REQUIRED_FIELDS, fields).append(')'); return new RecordSetListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -260,7 +234,7 @@ class ZoneOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneOption fields(ZoneField... fields) { - return new ZoneOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); + return new ZoneOption(DnsRpc.Option.FIELDS, selector(ZoneField.REQUIRED_FIELDS, fields)); } } @@ -283,8 +257,8 @@ class ZoneListOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,managedZones(").append(ZoneField.selector(fields)).append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,managedZones("), + ZoneField.REQUIRED_FIELDS, fields).append(')'); return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); } @@ -302,7 +276,6 @@ public static ZoneListOption pageToken(String pageToken) { * Restricts the list to only zone with this fully qualified domain name. */ public static ZoneListOption dnsName(String dnsName) { - StringBuilder builder = new StringBuilder(); return new ZoneListOption(DnsRpc.Option.DNS_NAME, dnsName); } @@ -337,7 +310,8 @@ class ProjectOption extends AbstractOption implements Serializable { * can be used. */ public static ProjectOption fields(ProjectField... fields) { - return new ProjectOption(DnsRpc.Option.FIELDS, ProjectField.selector(fields)); + return new ProjectOption(DnsRpc.Option.FIELDS, + selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -362,10 +336,8 @@ class ChangeRequestOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestOption fields(ChangeRequestField... fields) { - return new ChangeRequestOption( - DnsRpc.Option.FIELDS, - ChangeRequestField.selector(fields) - ); + return new ChangeRequestOption(DnsRpc.Option.FIELDS, + selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } @@ -390,9 +362,8 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("nextPageToken,changes(").append(ChangeRequestField.selector(fields)) - .append(')'); + StringBuilder builder = selector(new StringBuilder().append("nextPageToken,changes("), + ChangeRequestField.REQUIRED_FIELDS, fields).append(')'); return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java index 72d62d7fc224..3af167db71a5 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for Resource Manager operation options. @@ -69,4 +74,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 70eeb9c8eb50..595bfab556d6 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -16,15 +16,14 @@ package com.google.gcloud.resourcemanager; -import com.google.common.base.Joiner; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.util.List; -import java.util.Set; /** * An interface for Google Cloud Resource Manager. @@ -42,7 +41,7 @@ public interface ResourceManager extends Service { * {@link ResourceManager#get} or {@link ResourceManager#list}. Project ID is always returned, * even if not specified. */ - enum ProjectField { + enum ProjectField implements FieldSelector { PROJECT_ID("projectId"), NAME("name"), LABELS("labels"), @@ -50,24 +49,18 @@ enum ProjectField { STATE("lifecycleState"), CREATE_TIME("createTime"); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + private final String selector; ProjectField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(ProjectField... fields) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(PROJECT_ID.selector()); - for (ProjectField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -90,7 +83,8 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { * that can be used. */ public static ProjectGetOption fields(ProjectField... fields) { - return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, ProjectField.selector(fields)); + return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, + selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -163,8 +157,9 @@ public static ProjectListOption pageSize(int pageSize) { * that can be used. */ public static ProjectListOption fields(ProjectField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("projects(").append(ProjectField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("projects("), ProjectField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString()); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java index 65c55da7efc8..1ea0c0561eaa 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java @@ -18,11 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; +import com.google.common.collect.Sets; +import com.google.gcloud.FieldSelector; import com.google.gcloud.storage.spi.StorageRpc; import java.io.Serializable; +import java.util.List; import java.util.Objects; +import java.util.Set; /** * Base class for Storage operation option. @@ -69,4 +74,20 @@ public String toString() { .add("value", value) .toString(); } + + static String selector(List required, FieldSelector... others) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + for (FieldSelector field : required) { + fieldStrings.add(field.selector()); + } + for (FieldSelector field : others) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + + static StringBuilder selector(StringBuilder partialSelector, List required, + FieldSelector... others) { + return partialSelector.append(selector(required, others)); + } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 78f421e94e52..eb51e9d0f6c1 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -19,13 +19,12 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; +import com.google.gcloud.FieldSelector; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -38,7 +37,6 @@ import java.net.URL; import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -74,7 +72,7 @@ String entry() { } } - enum BucketField { + enum BucketField implements FieldSelector { ID("id"), SELF_LINK("selfLink"), NAME("name"), @@ -90,27 +88,21 @@ enum BucketField { STORAGE_CLASS("storageClass"), ETAG("etag"); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + private final String selector; BucketField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(BucketField... fields) { - HashSet fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); - fieldStrings.add(NAME.selector()); - for (BucketField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } - enum BlobField { + enum BlobField implements FieldSelector { ACL("acl"), BUCKET("bucket"), CACHE_CONTROL("cacheControl"), @@ -136,25 +128,19 @@ enum BlobField { TIME_DELETED("timeDeleted"), UPDATED("updated"); + static final List REQUIRED_FIELDS = + ImmutableList.of(BUCKET, NAME); + private final String selector; BlobField(String selector) { this.selector = selector; } + @Override public String selector() { return selector; } - - static String selector(BlobField... fields) { - HashSet fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 2); - fieldStrings.add(BUCKET.selector()); - fieldStrings.add(NAME.selector()); - for (BlobField field : fields) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } } /** @@ -269,7 +255,8 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { * specified. */ public static BucketGetOption fields(BucketField... fields) { - return new BucketGetOption(StorageRpc.Option.FIELDS, BucketField.selector(fields)); + return new BucketGetOption(StorageRpc.Option.FIELDS, + selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -609,7 +596,8 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { * specified. */ public static BlobGetOption fields(BlobField... fields) { - return new BlobGetOption(StorageRpc.Option.FIELDS, BlobField.selector(fields)); + return new BlobGetOption(StorageRpc.Option.FIELDS, + selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -653,8 +641,9 @@ public static BucketListOption prefix(String prefix) { * specified. */ public static BucketListOption fields(BucketField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("items(").append(BucketField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("items("), BucketField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString()); } } @@ -722,8 +711,9 @@ public static BlobListOption versions(boolean versions) { * specified. */ public static BlobListOption fields(BlobField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("items(").append(BlobField.selector(fields)).append("),nextPageToken"); + StringBuilder builder = + selector(new StringBuilder().append("items("), BlobField.REQUIRED_FIELDS, fields) + .append("),nextPageToken"); return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString()); } } From 1710a4cc3907a53cec4d1335f8a6b56b3a92b392 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 30 Mar 2016 14:57:00 +0200 Subject: [PATCH 2/3] Refactor field selection and helpers - Add FieldSelector.SelectorHelper with static methods to create selectors - Add SelectorHelperTest - Rename dns.AbstractOption to Option, make other Option classes abstract - Add OptionTest class to resource manager and refactor other OptionTest classes --- .../com/google/gcloud/bigquery/BigQuery.java | 14 ++-- .../com/google/gcloud/bigquery/Option.java | 23 +----- .../gcloud/bigquery/BigQueryImplTest.java | 8 ++- .../google/gcloud/bigquery/OptionTest.java | 41 +++++++++-- .../java/com/google/gcloud/FieldSelector.java | 68 +++++++++++++++++- .../com/google/gcloud/SelectorHelperTest.java | 71 +++++++++++++++++++ .../main/java/com/google/gcloud/dns/Dns.java | 35 +++++---- .../java/com/google/gcloud/dns/DnsImpl.java | 4 +- .../dns/{AbstractOption.java => Option.java} | 29 ++------ ...bstractOptionTest.java => OptionTest.java} | 31 ++++---- .../google/gcloud/resourcemanager/Option.java | 23 +----- .../resourcemanager/ResourceManager.java | 9 ++- .../gcloud/resourcemanager/OptionTest.java | 66 +++++++++++++++++ .../com/google/gcloud/storage/Option.java | 23 +----- .../com/google/gcloud/storage/Storage.java | 17 ++--- .../com/google/gcloud/storage/OptionTest.java | 41 +++++++++-- 16 files changed, 337 insertions(+), 166 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{AbstractOption.java => Option.java} (62%) rename gcloud-java-dns/src/test/java/com/google/gcloud/dns/{AbstractOptionTest.java => OptionTest.java} (66%) create mode 100644 gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index a0724b2026b2..c23ee2844762 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; @@ -192,7 +193,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { */ public static DatasetOption fields(DatasetField... fields) { return new DatasetOption(BigQueryRpc.Option.FIELDS, - selector(DatasetField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -262,7 +263,7 @@ private TableOption(BigQueryRpc.Option option, Object value) { */ public static TableOption fields(TableField... fields) { return new TableOption(BigQueryRpc.Option.FIELDS, - selector(TableField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -359,10 +360,9 @@ public static JobListOption pageToken(String pageToken) { * listing jobs. */ public static JobListOption fields(JobField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("etag,jobs("), JobField.REQUIRED_FIELDS, fields) - .append(",state,errorResult),nextPageToken"); - return new JobListOption(BigQueryRpc.Option.FIELDS, builder.toString()); + return new JobListOption(BigQueryRpc.Option.FIELDS, + SelectorHelper.selector("jobs", JobField.REQUIRED_FIELDS, fields, "state", + "errorResult")); } } @@ -386,7 +386,7 @@ private JobOption(BigQueryRpc.Option option, Object value) { */ public static JobOption fields(JobField... fields) { return new JobOption(BigQueryRpc.Option.FIELDS, - Option.selector(JobField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java index 9b123fc1c89c..e7ac0d0a8cc4 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for BigQuery operation option. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -6647817677804099207L; @@ -73,20 +68,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java index a6f512800024..c7d7cf846ef2 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java @@ -886,12 +886,14 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption()); - assertTrue(selector.contains("etag,jobs(")); + assertTrue(selector.contains("nextPageToken,jobs(")); assertTrue(selector.contains("configuration")); assertTrue(selector.contains("jobReference")); assertTrue(selector.contains("statistics")); - assertTrue(selector.contains("state,errorResult),nextPageToken")); - assertEquals(80, selector.length()); + assertTrue(selector.contains("state")); + assertTrue(selector.contains("errorResult")); + assertTrue(selector.contains(")")); + assertEquals(75, selector.length()); } @Test diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java index 2c89ececedb8..42f19830fb6c 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java @@ -17,22 +17,49 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import com.google.gcloud.bigquery.spi.BigQueryRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class OptionTest { + private static final BigQueryRpc.Option RPC_OPTION = BigQueryRpc.Option.PAGE_TOKEN; + private static final BigQueryRpc.Option ANOTHER_RPC_OPTION = BigQueryRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + @Test - public void testOption() { - Option option = new Option(BigQueryRpc.Option.PAGE_TOKEN, "token"); - assertEquals(BigQueryRpc.Option.PAGE_TOKEN, option.rpcOption()); - assertEquals("token", option.value()); + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); } - @Test(expected = NullPointerException.class) - public void testNullRpcOption() { - new Option(null, "token"); + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java index 358c4d4798f6..343f67e3381b 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -16,9 +16,18 @@ package com.google.gcloud; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + /** * Interface for Google Cloud resource's fields. Implementations of this interface can be used to - * select only desired fields when getting or listing Google Cloud resources. + * select only desired fields from a returned Google Cloud resource. */ public interface FieldSelector { @@ -27,4 +36,61 @@ public interface FieldSelector { * other field selectors) to specify which resource fields should be returned by an API call. */ String selector(); + + /** + * A helper class used to build composite selectors given a number of fields. This class is not + * supposed to be used directly by users. + */ + class SelectorHelper { + + private SelectorHelper() {} + + private static final Function FIELD_TO_STRING_FUNCTION = + new Function() { + @Override + public String apply(FieldSelector fieldSelector) { + return fieldSelector.selector(); + } + }; + + private static String selector(List required, FieldSelector[] others, + String... extraResourceFields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); + fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION)); + fieldStrings.addAll(Lists.transform(Arrays.asList(others), FIELD_TO_STRING_FUNCTION)); + fieldStrings.addAll(Arrays.asList(extraResourceFields)); + return Joiner.on(',').join(fieldStrings); + } + + /** + * Returns a composite selector given a number of fields. The string selector returned by this + * method can be used for field selection in API calls that return a single resource. This + * method is not supposed to be used directly by users. + */ + public static String selector(List required, FieldSelector... others) { + return selector(required, others, new String[]{}); + } + + /** + * Returns a composite selector given a number of fields and a container name. The string + * selector returned by this method can be used for field selection in API calls that return a + * list of resources. This method is not supposed to be used directly by users. + */ + public static String selector(String containerName, List required, + FieldSelector... others) { + return "nextPageToken," + containerName + '(' + selector(required, others) + ')'; + } + + /** + * Returns a composite selector given a number of fields and a container name. This methods also + * takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The + * string selector returned by this method can be used for field selection in API calls that + * return a list of resources. This method is not supposed to be used directly by users. + */ + public static String selector(String containerName, List required, + FieldSelector[] others, String... extraResourceFields) { + return "nextPageToken," + containerName + '(' + + selector(required, others, extraResourceFields) + ')'; + } + } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java new file mode 100644 index 000000000000..2279205539cc --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import com.google.gcloud.FieldSelector.SelectorHelper; + +import org.junit.Test; + +import java.util.List; + +public class SelectorHelperTest { + + private static final FieldSelector FIELD1 = new FieldSelector() { + @Override + public String selector() { + return "field1"; + } + }; + private static final FieldSelector FIELD2 = new FieldSelector() { + @Override + public String selector() { + return "field2"; + } + }; + private static final FieldSelector FIELD3 = new FieldSelector() { + @Override + public String selector() { + return "field3"; + } + }; + private static final List REQUIRED_FIELDS = ImmutableList.of(FIELD1, FIELD2); + private static final String CONTAINER = "container"; + + @Test + public void testSelector() { + String selector = SelectorHelper.selector(REQUIRED_FIELDS, FIELD3); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertEquals(20, selector.length()); + } + + @Test + public void testListSelector() { + String selector = SelectorHelper.selector(CONTAINER, REQUIRED_FIELDS, FIELD3); + assertTrue(selector.startsWith("nextPageToken,container(")); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertTrue(selector.endsWith(")")); + assertEquals(45, selector.length()); + } +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index ee0611279d27..c4b08e31d533 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; @@ -157,7 +158,7 @@ public String selector() { /** * Class for specifying record set listing options. */ - class RecordSetListOption extends AbstractOption implements Serializable { + class RecordSetListOption extends Option implements Serializable { private static final long serialVersionUID = 1009627025381096098L; @@ -174,9 +175,8 @@ class RecordSetListOption extends AbstractOption implements Serializable { * of fields that can be used. */ public static RecordSetListOption fields(RecordSetField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,rrsets("), - RecordSetField.REQUIRED_FIELDS, fields).append(')'); - return new RecordSetListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new RecordSetListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); } /** @@ -218,7 +218,7 @@ public static RecordSetListOption type(RecordSet.Type type) { /** * Class for specifying zone field options. */ - class ZoneOption extends AbstractOption implements Serializable { + class ZoneOption extends Option implements Serializable { private static final long serialVersionUID = -8065564464895945037L; @@ -234,14 +234,15 @@ class ZoneOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneOption fields(ZoneField... fields) { - return new ZoneOption(DnsRpc.Option.FIELDS, selector(ZoneField.REQUIRED_FIELDS, fields)); + return new ZoneOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector(ZoneField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying zone listing options. */ - class ZoneListOption extends AbstractOption implements Serializable { + class ZoneListOption extends Option implements Serializable { private static final long serialVersionUID = -2830645032124504717L; @@ -257,9 +258,8 @@ class ZoneListOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,managedZones("), - ZoneField.REQUIRED_FIELDS, fields).append(')'); - return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new ZoneListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); } /** @@ -293,7 +293,7 @@ public static ZoneListOption pageSize(int pageSize) { /** * Class for specifying project options. */ - class ProjectOption extends AbstractOption implements Serializable { + class ProjectOption extends Option implements Serializable { private static final long serialVersionUID = 6817937338218847748L; @@ -311,14 +311,14 @@ class ProjectOption extends AbstractOption implements Serializable { */ public static ProjectOption fields(ProjectField... fields) { return new ProjectOption(DnsRpc.Option.FIELDS, - selector(ProjectField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request field options. */ - class ChangeRequestOption extends AbstractOption implements Serializable { + class ChangeRequestOption extends Option implements Serializable { private static final long serialVersionUID = 1067273695061077782L; @@ -337,14 +337,14 @@ class ChangeRequestOption extends AbstractOption implements Serializable { */ public static ChangeRequestOption fields(ChangeRequestField... fields) { return new ChangeRequestOption(DnsRpc.Option.FIELDS, - selector(ChangeRequestField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request listing options. */ - class ChangeRequestListOption extends AbstractOption implements Serializable { + class ChangeRequestListOption extends Option implements Serializable { private static final long serialVersionUID = -900209143895376089L; @@ -362,9 +362,8 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { - StringBuilder builder = selector(new StringBuilder().append("nextPageToken,changes("), - ChangeRequestField.REQUIRED_FIELDS, fields).append(')'); - return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); + return new ChangeRequestListOption(DnsRpc.Option.FIELDS, + SelectorHelper.selector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); } /** diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index 51ab0bd92720..9f4fa2a9d9d1 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -309,9 +309,9 @@ public com.google.api.services.dns.model.Change call() { } } - private Map optionMap(AbstractOption... options) { + private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(DnsRpc.Option.class); - for (AbstractOption option : options) { + for (Option option : options) { Object prev = temp.put(option.rpcOption(), option.value()); checkArgument(prev == null, "Duplicate option %s", option); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java similarity index 62% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java index 43dfe69df213..fee99898fb24 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Option.java @@ -18,27 +18,22 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.dns.spi.DnsRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * A base class for options. */ -abstract class AbstractOption implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -5912727967831484228L; private final Object value; private final DnsRpc.Option rpcOption; - AbstractOption(DnsRpc.Option rpcOption, Object value) { + Option(DnsRpc.Option rpcOption, Object value) { this.rpcOption = checkNotNull(rpcOption); this.value = value; } @@ -53,10 +48,10 @@ DnsRpc.Option rpcOption() { @Override public boolean equals(Object obj) { - if (!(obj instanceof AbstractOption)) { + if (!(obj instanceof Option)) { return false; } - AbstractOption other = (AbstractOption) obj; + Option other = (Option) obj; return Objects.equals(value, other.value) && Objects.equals(rpcOption, other.rpcOption); } @@ -72,20 +67,4 @@ public String toString() { .add("rpcOption", rpcOption) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/OptionTest.java similarity index 66% rename from gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java rename to gcloud-java-dns/src/test/java/com/google/gcloud/dns/OptionTest.java index d88ea85c5846..e9906354f963 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/OptionTest.java @@ -18,24 +18,27 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNull; import com.google.gcloud.dns.spi.DnsRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -public class AbstractOptionTest { +public class OptionTest { private static final DnsRpc.Option RPC_OPTION = DnsRpc.Option.DNS_TYPE; private static final DnsRpc.Option ANOTHER_RPC_OPTION = DnsRpc.Option.DNS_NAME; private static final String VALUE = "some value"; private static final String OTHER_VALUE = "another value"; - private static final AbstractOption OPTION = new AbstractOption(RPC_OPTION, VALUE) {}; - private static final AbstractOption OPTION_EQUALS = new AbstractOption(RPC_OPTION, VALUE) {}; - private static final AbstractOption OPTION_NOT_EQUALS1 = - new AbstractOption(RPC_OPTION, OTHER_VALUE) {}; - private static final AbstractOption OPTION_NOT_EQUALS2 = - new AbstractOption(ANOTHER_RPC_OPTION, VALUE) {}; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); @Test public void testEquals() { @@ -53,12 +56,10 @@ public void testHashCode() { public void testConstructor() { assertEquals(RPC_OPTION, OPTION.rpcOption()); assertEquals(VALUE, OPTION.value()); - try { - new AbstractOption(null, VALUE) {}; - fail("Cannot build with empty option."); - } catch (NullPointerException e) { - // expected - } - new AbstractOption(RPC_OPTION, null) {}; // null value is ok + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java index 3af167db71a5..3df68468f69f 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for Resource Manager operation options. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = 2655177550880762967L; @@ -74,20 +69,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 595bfab556d6..e000ca69c359 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; @@ -84,7 +85,7 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { */ public static ProjectGetOption fields(ProjectField... fields) { return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, - selector(ProjectField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -157,10 +158,8 @@ public static ProjectListOption pageSize(int pageSize) { * that can be used. */ public static ProjectListOption fields(ProjectField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("projects("), ProjectField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString()); + return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, + SelectorHelper.selector("projects", ProjectField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java new file mode 100644 index 000000000000..729c7a4b8911 --- /dev/null +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/OptionTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.resourcemanager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class OptionTest { + + private static final ResourceManagerRpc.Option RPC_OPTION = ResourceManagerRpc.Option.FILTER; + private static final ResourceManagerRpc.Option ANOTHER_RPC_OPTION = + ResourceManagerRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + + @Test + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); + } + + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; + } +} diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java index 1ea0c0561eaa..774023eff78b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java @@ -18,21 +18,16 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; -import com.google.common.collect.Sets; -import com.google.gcloud.FieldSelector; import com.google.gcloud.storage.spi.StorageRpc; import java.io.Serializable; -import java.util.List; import java.util.Objects; -import java.util.Set; /** * Base class for Storage operation option. */ -class Option implements Serializable { +abstract class Option implements Serializable { private static final long serialVersionUID = -73199088766477208L; @@ -74,20 +69,4 @@ public String toString() { .add("value", value) .toString(); } - - static String selector(List required, FieldSelector... others) { - Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); - for (FieldSelector field : required) { - fieldStrings.add(field.selector()); - } - for (FieldSelector field : others) { - fieldStrings.add(field.selector()); - } - return Joiner.on(',').join(fieldStrings); - } - - static StringBuilder selector(StringBuilder partialSelector, List required, - FieldSelector... others) { - return partialSelector.append(selector(required, others)); - } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index eb51e9d0f6c1..87d82aad686b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -25,6 +25,7 @@ import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.FieldSelector; +import com.google.gcloud.FieldSelector.SelectorHelper; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -256,7 +257,7 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { */ public static BucketGetOption fields(BucketField... fields) { return new BucketGetOption(StorageRpc.Option.FIELDS, - selector(BucketField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -597,7 +598,7 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { */ public static BlobGetOption fields(BlobField... fields) { return new BlobGetOption(StorageRpc.Option.FIELDS, - selector(BlobField.REQUIRED_FIELDS, fields)); + SelectorHelper.selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -641,10 +642,8 @@ public static BucketListOption prefix(String prefix) { * specified. */ public static BucketListOption fields(BucketField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("items("), BucketField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString()); + return new BucketListOption(StorageRpc.Option.FIELDS, + SelectorHelper.selector("items", BucketField.REQUIRED_FIELDS, fields)); } } @@ -711,10 +710,8 @@ public static BlobListOption versions(boolean versions) { * specified. */ public static BlobListOption fields(BlobField... fields) { - StringBuilder builder = - selector(new StringBuilder().append("items("), BlobField.REQUIRED_FIELDS, fields) - .append("),nextPageToken"); - return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString()); + return new BlobListOption(StorageRpc.Option.FIELDS, + SelectorHelper.selector("items", BlobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java index 5924174ab138..08a8e79b2c3b 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java @@ -17,22 +17,49 @@ package com.google.gcloud.storage; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import com.google.gcloud.storage.spi.StorageRpc; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class OptionTest { + private static final StorageRpc.Option RPC_OPTION = StorageRpc.Option.DELIMITER; + private static final StorageRpc.Option ANOTHER_RPC_OPTION = StorageRpc.Option.FIELDS; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final Option OPTION = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {}; + private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {}; + private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + @Test - public void testOption() { - Option option = new Option(StorageRpc.Option.DELIMITER, "/"); - assertEquals(StorageRpc.Option.DELIMITER, option.rpcOption()); - assertEquals("/", option.value()); + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); } - @Test(expected = NullPointerException.class) - public void testIndexOutOfBoundsException() { - new Option(null, "/"); + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + Option option = new Option(RPC_OPTION, null) {}; + assertEquals(RPC_OPTION, option.rpcOption()); + assertNull(option.value()); + thrown.expect(NullPointerException.class); + new Option(null, VALUE) {}; } } From 8db4b9b6f7b0c56c9b93c5a614a1c945b10336b6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 30 Mar 2016 22:42:29 +0200 Subject: [PATCH 3/3] Minor refactoring for SelectorHelper - Rename selector method for lists to listSelector - Update selectors to accept List parameters - Add missing unit test for listSelector - Remove redundant extends Serializable from dns' option classes --- .../com/google/gcloud/bigquery/BigQuery.java | 23 ++++++----- .../java/com/google/gcloud/FieldSelector.java | 12 +++--- ...Test.java => FieldSelectorHelperTest.java} | 21 ++++++++-- .../main/java/com/google/gcloud/dns/Dns.java | 38 +++++++++---------- .../resourcemanager/ResourceManager.java | 8 ++-- .../com/google/gcloud/storage/Storage.java | 17 ++++----- 6 files changed, 65 insertions(+), 54 deletions(-) rename gcloud-java-core/src/test/java/com/google/gcloud/{SelectorHelperTest.java => FieldSelectorHelperTest.java} (72%) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index c23ee2844762..2d864383e81a 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -22,7 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.bigquery.spi.BigQueryRpc; @@ -55,8 +55,8 @@ enum DatasetField implements FieldSelector { LOCATION("location"), SELF_LINK("selfLink"); - static final List REQUIRED_FIELDS = - ImmutableList.of(DATASET_REFERENCE); + static final List REQUIRED_FIELDS = + ImmutableList.of(DATASET_REFERENCE); private final String selector; @@ -95,8 +95,8 @@ enum TableField implements FieldSelector { TYPE("type"), VIEW("view"); - static final List REQUIRED_FIELDS = - ImmutableList.of(TABLE_REFERENCE, TYPE); + static final List REQUIRED_FIELDS = + ImmutableList.of(TABLE_REFERENCE, TYPE); private final String selector; @@ -126,8 +126,8 @@ enum JobField implements FieldSelector { STATUS("status"), USER_EMAIL("user_email"); - static final List REQUIRED_FIELDS = - ImmutableList.of(JOB_REFERENCE, CONFIGURATION); + static final List REQUIRED_FIELDS = + ImmutableList.of(JOB_REFERENCE, CONFIGURATION); private final String selector; @@ -193,7 +193,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) { */ public static DatasetOption fields(DatasetField... fields) { return new DatasetOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(DatasetField.REQUIRED_FIELDS, fields)); + Helper.selector(DatasetField.REQUIRED_FIELDS, fields)); } } @@ -263,7 +263,7 @@ private TableOption(BigQueryRpc.Option option, Object value) { */ public static TableOption fields(TableField... fields) { return new TableOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(TableField.REQUIRED_FIELDS, fields)); + Helper.selector(TableField.REQUIRED_FIELDS, fields)); } } @@ -361,8 +361,7 @@ public static JobListOption pageToken(String pageToken) { */ public static JobListOption fields(JobField... fields) { return new JobListOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector("jobs", JobField.REQUIRED_FIELDS, fields, "state", - "errorResult")); + Helper.listSelector("jobs", JobField.REQUIRED_FIELDS, fields, "state", "errorResult")); } } @@ -386,7 +385,7 @@ private JobOption(BigQueryRpc.Option option, Object value) { */ public static JobOption fields(JobField... fields) { return new JobOption(BigQueryRpc.Option.FIELDS, - SelectorHelper.selector(JobField.REQUIRED_FIELDS, fields)); + Helper.selector(JobField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java index 343f67e3381b..be6ab73d00bf 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/FieldSelector.java @@ -41,9 +41,9 @@ public interface FieldSelector { * A helper class used to build composite selectors given a number of fields. This class is not * supposed to be used directly by users. */ - class SelectorHelper { + class Helper { - private SelectorHelper() {} + private Helper() {} private static final Function FIELD_TO_STRING_FUNCTION = new Function() { @@ -53,7 +53,7 @@ public String apply(FieldSelector fieldSelector) { } }; - private static String selector(List required, FieldSelector[] others, + private static String selector(List required, FieldSelector[] others, String... extraResourceFields) { Set fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length); fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION)); @@ -67,7 +67,7 @@ private static String selector(List required, FieldSelector[] oth * method can be used for field selection in API calls that return a single resource. This * method is not supposed to be used directly by users. */ - public static String selector(List required, FieldSelector... others) { + public static String selector(List required, FieldSelector... others) { return selector(required, others, new String[]{}); } @@ -76,7 +76,7 @@ public static String selector(List required, FieldSelector... oth * selector returned by this method can be used for field selection in API calls that return a * list of resources. This method is not supposed to be used directly by users. */ - public static String selector(String containerName, List required, + public static String listSelector(String containerName, List required, FieldSelector... others) { return "nextPageToken," + containerName + '(' + selector(required, others) + ')'; } @@ -87,7 +87,7 @@ public static String selector(String containerName, List required * string selector returned by this method can be used for field selection in API calls that * return a list of resources. This method is not supposed to be used directly by users. */ - public static String selector(String containerName, List required, + public static String listSelector(String containerName, List required, FieldSelector[] others, String... extraResourceFields) { return "nextPageToken," + containerName + '(' + selector(required, others, extraResourceFields) + ')'; diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java similarity index 72% rename from gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java rename to gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java index 2279205539cc..9871c942180e 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SelectorHelperTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/FieldSelectorHelperTest.java @@ -20,13 +20,13 @@ import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import org.junit.Test; import java.util.List; -public class SelectorHelperTest { +public class FieldSelectorHelperTest { private static final FieldSelector FIELD1 = new FieldSelector() { @Override @@ -51,7 +51,7 @@ public String selector() { @Test public void testSelector() { - String selector = SelectorHelper.selector(REQUIRED_FIELDS, FIELD3); + String selector = Helper.selector(REQUIRED_FIELDS, FIELD3); assertTrue(selector.contains("field1")); assertTrue(selector.contains("field2")); assertTrue(selector.contains("field3")); @@ -60,7 +60,7 @@ public void testSelector() { @Test public void testListSelector() { - String selector = SelectorHelper.selector(CONTAINER, REQUIRED_FIELDS, FIELD3); + String selector = Helper.listSelector(CONTAINER, REQUIRED_FIELDS, FIELD3); assertTrue(selector.startsWith("nextPageToken,container(")); assertTrue(selector.contains("field1")); assertTrue(selector.contains("field2")); @@ -68,4 +68,17 @@ public void testListSelector() { assertTrue(selector.endsWith(")")); assertEquals(45, selector.length()); } + + @Test + public void testListSelectorWithExtraFields() { + String selector = Helper.listSelector(CONTAINER, REQUIRED_FIELDS, + new FieldSelector[]{FIELD3}, "field4"); + assertTrue(selector.startsWith("nextPageToken,container(")); + assertTrue(selector.contains("field1")); + assertTrue(selector.contains("field2")); + assertTrue(selector.contains("field3")); + assertTrue(selector.contains("field4")); + assertTrue(selector.endsWith(")")); + assertEquals(52, selector.length()); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index c4b08e31d533..d95d11a97c19 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -18,7 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.dns.spi.DnsRpc; @@ -45,7 +45,7 @@ enum ProjectField implements FieldSelector { PROJECT_NUMBER("number"), QUOTA("quota"); - static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); private final String selector; @@ -75,7 +75,7 @@ enum ZoneField implements FieldSelector { NAME_SERVER_SET("nameServerSet"), NAME_SERVERS("nameServers"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); private final String selector; @@ -102,7 +102,7 @@ enum RecordSetField implements FieldSelector { TTL("ttl"), TYPE("type"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE); private final String selector; @@ -120,8 +120,8 @@ public String selector() { * The fields of a change request. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is always - * returned even if not selected. + * {@link Dns#applyChangeRequest(String, ChangeRequestInfo, ChangeRequestOption...)} The ID is + * always returned even if not selected. */ enum ChangeRequestField implements FieldSelector { ID("id"), @@ -130,7 +130,7 @@ enum ChangeRequestField implements FieldSelector { ADDITIONS("additions"), DELETIONS("deletions"); - static final List REQUIRED_FIELDS = ImmutableList.of(ID); + static final List REQUIRED_FIELDS = ImmutableList.of(ID); private final String selector; @@ -158,7 +158,7 @@ public String selector() { /** * Class for specifying record set listing options. */ - class RecordSetListOption extends Option implements Serializable { + class RecordSetListOption extends Option { private static final long serialVersionUID = 1009627025381096098L; @@ -176,7 +176,7 @@ class RecordSetListOption extends Option implements Serializable { */ public static RecordSetListOption fields(RecordSetField... fields) { return new RecordSetListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); + Helper.listSelector("rrsets", RecordSetField.REQUIRED_FIELDS, fields)); } /** @@ -218,7 +218,7 @@ public static RecordSetListOption type(RecordSet.Type type) { /** * Class for specifying zone field options. */ - class ZoneOption extends Option implements Serializable { + class ZoneOption extends Option { private static final long serialVersionUID = -8065564464895945037L; @@ -235,14 +235,14 @@ class ZoneOption extends Option implements Serializable { */ public static ZoneOption fields(ZoneField... fields) { return new ZoneOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ZoneField.REQUIRED_FIELDS, fields)); + Helper.selector(ZoneField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying zone listing options. */ - class ZoneListOption extends Option implements Serializable { + class ZoneListOption extends Option { private static final long serialVersionUID = -2830645032124504717L; @@ -259,7 +259,7 @@ class ZoneListOption extends Option implements Serializable { */ public static ZoneListOption fields(ZoneField... fields) { return new ZoneListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); + Helper.listSelector("managedZones", ZoneField.REQUIRED_FIELDS, fields)); } /** @@ -293,7 +293,7 @@ public static ZoneListOption pageSize(int pageSize) { /** * Class for specifying project options. */ - class ProjectOption extends Option implements Serializable { + class ProjectOption extends Option { private static final long serialVersionUID = 6817937338218847748L; @@ -311,14 +311,14 @@ class ProjectOption extends Option implements Serializable { */ public static ProjectOption fields(ProjectField... fields) { return new ProjectOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); + Helper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request field options. */ - class ChangeRequestOption extends Option implements Serializable { + class ChangeRequestOption extends Option { private static final long serialVersionUID = 1067273695061077782L; @@ -337,14 +337,14 @@ class ChangeRequestOption extends Option implements Serializable { */ public static ChangeRequestOption fields(ChangeRequestField... fields) { return new ChangeRequestOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); + Helper.selector(ChangeRequestField.REQUIRED_FIELDS, fields)); } } /** * Class for specifying change request listing options. */ - class ChangeRequestListOption extends Option implements Serializable { + class ChangeRequestListOption extends Option { private static final long serialVersionUID = -900209143895376089L; @@ -363,7 +363,7 @@ class ChangeRequestListOption extends Option implements Serializable { */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { return new ChangeRequestListOption(DnsRpc.Option.FIELDS, - SelectorHelper.selector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); + Helper.listSelector("changes", ChangeRequestField.REQUIRED_FIELDS, fields)); } /** diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index e000ca69c359..92494a5152fe 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -18,7 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; @@ -50,7 +50,7 @@ enum ProjectField implements FieldSelector { STATE("lifecycleState"), CREATE_TIME("createTime"); - static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); + static final List REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID); private final String selector; @@ -85,7 +85,7 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) { */ public static ProjectGetOption fields(ProjectField... fields) { return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, - SelectorHelper.selector(ProjectField.REQUIRED_FIELDS, fields)); + Helper.selector(ProjectField.REQUIRED_FIELDS, fields)); } } @@ -159,7 +159,7 @@ public static ProjectListOption pageSize(int pageSize) { */ public static ProjectListOption fields(ProjectField... fields) { return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, - SelectorHelper.selector("projects", ProjectField.REQUIRED_FIELDS, fields)); + Helper.listSelector("projects", ProjectField.REQUIRED_FIELDS, fields)); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 87d82aad686b..72d89348f5fa 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -25,7 +25,7 @@ import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.FieldSelector; -import com.google.gcloud.FieldSelector.SelectorHelper; +import com.google.gcloud.FieldSelector.Helper; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; @@ -89,7 +89,7 @@ enum BucketField implements FieldSelector { STORAGE_CLASS("storageClass"), ETAG("etag"); - static final List REQUIRED_FIELDS = ImmutableList.of(NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(NAME); private final String selector; @@ -129,8 +129,7 @@ enum BlobField implements FieldSelector { TIME_DELETED("timeDeleted"), UPDATED("updated"); - static final List REQUIRED_FIELDS = - ImmutableList.of(BUCKET, NAME); + static final List REQUIRED_FIELDS = ImmutableList.of(BUCKET, NAME); private final String selector; @@ -257,7 +256,7 @@ public static BucketGetOption metagenerationNotMatch(long metageneration) { */ public static BucketGetOption fields(BucketField... fields) { return new BucketGetOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector(BucketField.REQUIRED_FIELDS, fields)); + Helper.selector(BucketField.REQUIRED_FIELDS, fields)); } } @@ -598,7 +597,7 @@ public static BlobGetOption metagenerationNotMatch(long metageneration) { */ public static BlobGetOption fields(BlobField... fields) { return new BlobGetOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector(BlobField.REQUIRED_FIELDS, fields)); + Helper.selector(BlobField.REQUIRED_FIELDS, fields)); } } @@ -643,7 +642,7 @@ public static BucketListOption prefix(String prefix) { */ public static BucketListOption fields(BucketField... fields) { return new BucketListOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector("items", BucketField.REQUIRED_FIELDS, fields)); + Helper.listSelector("items", BucketField.REQUIRED_FIELDS, fields)); } } @@ -711,7 +710,7 @@ public static BlobListOption versions(boolean versions) { */ public static BlobListOption fields(BlobField... fields) { return new BlobListOption(StorageRpc.Option.FIELDS, - SelectorHelper.selector("items", BlobField.REQUIRED_FIELDS, fields)); + Helper.listSelector("items", BlobField.REQUIRED_FIELDS, fields)); } } @@ -1529,7 +1528,7 @@ public static Builder builder() { * are merged with metadata in the provided {@code BlobInfo} objects. To replace metadata instead * you first have to unset them. Unsetting metadata can be done by setting the provided * {@code BlobInfo} objects metadata to {@code null}. See - * {@link #update(com.google.gcloud.storage.BlobInfo)} for a code example. + * {@link #update(BlobInfo)} for a code example. * * @param blobInfos blobs to update * @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it