1616
1717package com .google .gcloud ;
1818
19+ import com .google .common .base .Function ;
20+ import com .google .common .base .Joiner ;
21+ import com .google .common .collect .Lists ;
22+ import com .google .common .collect .Sets ;
23+
24+ import java .util .Arrays ;
25+ import java .util .List ;
26+ import java .util .Set ;
27+
1928/**
2029 * Interface for Google Cloud resource's fields. Implementations of this interface can be used to
21- * select only desired fields when getting or listing Google Cloud resources .
30+ * select only desired fields from a returned Google Cloud resource .
2231 */
2332public interface FieldSelector {
2433
@@ -27,4 +36,61 @@ public interface FieldSelector {
2736 * other field selectors) to specify which resource fields should be returned by an API call.
2837 */
2938 String selector ();
39+
40+ /**
41+ * A helper class used to build composite selectors given a number of fields. This class is not
42+ * supposed to be used directly by users.
43+ */
44+ class SelectorHelper {
45+
46+ private SelectorHelper () {}
47+
48+ private static final Function <FieldSelector , String > FIELD_TO_STRING_FUNCTION =
49+ new Function <FieldSelector , String >() {
50+ @ Override
51+ public String apply (FieldSelector fieldSelector ) {
52+ return fieldSelector .selector ();
53+ }
54+ };
55+
56+ private static String selector (List <FieldSelector > required , FieldSelector [] others ,
57+ String ... extraResourceFields ) {
58+ Set <String > fieldStrings = Sets .newHashSetWithExpectedSize (required .size () + others .length );
59+ fieldStrings .addAll (Lists .transform (required , FIELD_TO_STRING_FUNCTION ));
60+ fieldStrings .addAll (Lists .transform (Arrays .asList (others ), FIELD_TO_STRING_FUNCTION ));
61+ fieldStrings .addAll (Arrays .asList (extraResourceFields ));
62+ return Joiner .on (',' ).join (fieldStrings );
63+ }
64+
65+ /**
66+ * Returns a composite selector given a number of fields. The string selector returned by this
67+ * method can be used for field selection in API calls that return a single resource. This
68+ * method is not supposed to be used directly by users.
69+ */
70+ public static String selector (List <FieldSelector > required , FieldSelector ... others ) {
71+ return selector (required , others , new String []{});
72+ }
73+
74+ /**
75+ * Returns a composite selector given a number of fields and a container name. The string
76+ * selector returned by this method can be used for field selection in API calls that return a
77+ * list of resources. This method is not supposed to be used directly by users.
78+ */
79+ public static String selector (String containerName , List <FieldSelector > required ,
80+ FieldSelector ... others ) {
81+ return "nextPageToken," + containerName + '(' + selector (required , others ) + ')' ;
82+ }
83+
84+ /**
85+ * Returns a composite selector given a number of fields and a container name. This methods also
86+ * takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The
87+ * string selector returned by this method can be used for field selection in API calls that
88+ * return a list of resources. This method is not supposed to be used directly by users.
89+ */
90+ public static String selector (String containerName , List <FieldSelector > required ,
91+ FieldSelector [] others , String ... extraResourceFields ) {
92+ return "nextPageToken," + containerName + '('
93+ + selector (required , others , extraResourceFields ) + ')' ;
94+ }
95+ }
3096}
0 commit comments