Skip to content

Commit

Permalink
Minor refactoring for SelectorHelper
Browse files Browse the repository at this point in the history
- Rename selector method for lists to listSelector
- Update selectors to accept List<? extends FieldSelector> parameters
- Add missing unit test for listSelector
- Remove redundant extends Serializable from dns' option classes
  • Loading branch information
mziccard committed Mar 31, 2016
1 parent 1710a4c commit 8db4b9b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,8 +55,8 @@ enum DatasetField implements FieldSelector {
LOCATION("location"),
SELF_LINK("selfLink");

static final List<FieldSelector> REQUIRED_FIELDS =
ImmutableList.<FieldSelector>of(DATASET_REFERENCE);
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(DATASET_REFERENCE);

private final String selector;

Expand Down Expand Up @@ -95,8 +95,8 @@ enum TableField implements FieldSelector {
TYPE("type"),
VIEW("view");

static final List<FieldSelector> REQUIRED_FIELDS =
ImmutableList.<FieldSelector>of(TABLE_REFERENCE, TYPE);
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(TABLE_REFERENCE, TYPE);

private final String selector;

Expand Down Expand Up @@ -126,8 +126,8 @@ enum JobField implements FieldSelector {
STATUS("status"),
USER_EMAIL("user_email");

static final List<FieldSelector> REQUIRED_FIELDS =
ImmutableList.<FieldSelector>of(JOB_REFERENCE, CONFIGURATION);
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(JOB_REFERENCE, CONFIGURATION);

private final String selector;

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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"));
}
}

Expand All @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldSelector, String> FIELD_TO_STRING_FUNCTION =
new Function<FieldSelector, String>() {
Expand All @@ -53,7 +53,7 @@ public String apply(FieldSelector fieldSelector) {
}
};

private static String selector(List<FieldSelector> required, FieldSelector[] others,
private static String selector(List<? extends FieldSelector> required, FieldSelector[] others,
String... extraResourceFields) {
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length);
fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION));
Expand All @@ -67,7 +67,7 @@ private static String selector(List<FieldSelector> 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<FieldSelector> required, FieldSelector... others) {
public static String selector(List<? extends FieldSelector> required, FieldSelector... others) {
return selector(required, others, new String[]{});
}

Expand All @@ -76,7 +76,7 @@ public static String selector(List<FieldSelector> 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<FieldSelector> required,
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector... others) {
return "nextPageToken," + containerName + '(' + selector(required, others) + ')';
}
Expand All @@ -87,7 +87,7 @@ public static String selector(String containerName, List<FieldSelector> 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<FieldSelector> required,
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector[] others, String... extraResourceFields) {
return "nextPageToken," + containerName + '('
+ selector(required, others, extraResourceFields) + ')';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
Expand All @@ -60,12 +60,25 @@ 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"));
assertTrue(selector.contains("field3"));
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());
}
}
38 changes: 19 additions & 19 deletions gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +45,7 @@ enum ProjectField implements FieldSelector {
PROJECT_NUMBER("number"),
QUOTA("quota");

static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.<FieldSelector>of(PROJECT_ID);
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID);

private final String selector;

Expand Down Expand Up @@ -75,7 +75,7 @@ enum ZoneField implements FieldSelector {
NAME_SERVER_SET("nameServerSet"),
NAME_SERVERS("nameServers");

static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.<FieldSelector>of(NAME);
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(NAME);

private final String selector;

Expand All @@ -102,7 +102,7 @@ enum RecordSetField implements FieldSelector {
TTL("ttl"),
TYPE("type");

static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.<FieldSelector>of(NAME, TYPE);
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(NAME, TYPE);

private final String selector;

Expand All @@ -120,8 +120,8 @@ public String selector() {
* The fields of a change request.
*
* <p>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"),
Expand All @@ -130,7 +130,7 @@ enum ChangeRequestField implements FieldSelector {
ADDITIONS("additions"),
DELETIONS("deletions");

static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.<FieldSelector>of(ID);
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(ID);

private final String selector;

Expand Down Expand Up @@ -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;

Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +50,7 @@ enum ProjectField implements FieldSelector {
STATE("lifecycleState"),
CREATE_TIME("createTime");

static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.<FieldSelector>of(PROJECT_ID);
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(PROJECT_ID);

private final String selector;

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand Down
Loading

0 comments on commit 8db4b9b

Please sign in to comment.