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,59 @@ 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 static final Function <FieldSelector , String > FIELD_TO_STRING_FUNCTION =
47+ new Function <FieldSelector , String >() {
48+ @ Override
49+ public String apply (FieldSelector fieldSelector ) {
50+ return fieldSelector .selector ();
51+ }
52+ };
53+
54+ private static String selector (List <FieldSelector > required , FieldSelector [] others ,
55+ String ... extraResourceFields ) {
56+ Set <String > fieldStrings = Sets .newHashSetWithExpectedSize (required .size () + others .length );
57+ fieldStrings .addAll (Lists .transform (required , FIELD_TO_STRING_FUNCTION ));
58+ fieldStrings .addAll (Lists .transform (Arrays .asList (others ), FIELD_TO_STRING_FUNCTION ));
59+ fieldStrings .addAll (Arrays .asList (extraResourceFields ));
60+ return Joiner .on (',' ).join (fieldStrings );
61+ }
62+
63+ /**
64+ * Returns a composite selector given a number of fields. The string selector returned by this
65+ * method can be used for field selection in API calls that return a single resource. This
66+ * method is not supposed to be used directly by users.
67+ */
68+ public static String selector (List <FieldSelector > required , FieldSelector ... others ) {
69+ return selector (required , others , new String []{});
70+ }
71+
72+ /**
73+ * Returns a composite selector given a number of fields and a container name. The string
74+ * selector returned by this method can be used for field selection in API calls that return a
75+ * list of resources. This method is not supposed to be used directly by users.
76+ */
77+ public static String selector (String containerName , List <FieldSelector > required ,
78+ FieldSelector ... others ) {
79+ return "nextPageToken," + containerName + '(' + selector (required , others ) + ')' ;
80+ }
81+
82+ /**
83+ * Returns a composite selector given a number of fields and a container name. This methods also
84+ * takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The
85+ * string selector returned by this method can be used for field selection in API calls that
86+ * return a list of resources. This method is not supposed to be used directly by users.
87+ */
88+ public static String selector (String containerName , List <FieldSelector > required ,
89+ FieldSelector [] others , String ... extraResourceFields ) {
90+ return "nextPageToken," + containerName + '('
91+ + selector (required , others , extraResourceFields ) + ')' ;
92+ }
93+ }
3094}
0 commit comments