• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud;
18 
19 import com.google.api.core.InternalApi;
20 import com.google.common.base.Function;
21 import com.google.common.base.Joiner;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Sets;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Set;
27 
28 /**
29  * Interface for Google Cloud resource's fields. Implementations of this interface can be used to
30  * select only desired fields from a returned Google Cloud resource.
31  */
32 public interface FieldSelector {
33 
34   /**
35    * Returns a string selector. This selector is passed to a Google Cloud service (possibly with
36    * other field selectors) to specify which resource fields should be returned by an API call.
37    */
getSelector()38   String getSelector();
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   @InternalApi
45   class Helper {
46 
47     private static final String[] EMPTY_FIELDS = {};
48 
Helper()49     private Helper() {}
50 
51     private static final Function<FieldSelector, String> FIELD_TO_STRING_FUNCTION =
52         new Function<FieldSelector, String>() {
53           @Override
54           public String apply(FieldSelector fieldSelector) {
55             return fieldSelector.getSelector();
56           }
57         };
58 
selector( List<? extends FieldSelector> required, FieldSelector[] others, String... extraResourceFields)59     private static String selector(
60         List<? extends FieldSelector> required,
61         FieldSelector[] others,
62         String... extraResourceFields) {
63       Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length);
64       fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION));
65       fieldStrings.addAll(Lists.transform(Arrays.asList(others), FIELD_TO_STRING_FUNCTION));
66       fieldStrings.addAll(Arrays.asList(extraResourceFields));
67       return Joiner.on(',').join(fieldStrings);
68     }
69 
70     /**
71      * Returns a composite selector given a number of resource fields. The string selector returned
72      * by this method can be used for field selection in API calls that return a single resource.
73      * This method is not supposed to be used directly by users.
74      */
selector(List<? extends FieldSelector> required, FieldSelector... others)75     public static String selector(List<? extends FieldSelector> required, FieldSelector... others) {
76       return selector(required, others, new String[] {});
77     }
78 
79     /**
80      * Returns a composite selector given a number of resource fields and a container name. The
81      * string selector returned by this method can be used for field selection in API calls that
82      * return a list of resources. This method is not supposed to be used directly by users.
83      */
listSelector( String containerName, List<? extends FieldSelector> required, FieldSelector... others)84     public static String listSelector(
85         String containerName, List<? extends FieldSelector> required, FieldSelector... others) {
86       return "nextPageToken," + containerName + '(' + selector(required, others) + ')';
87     }
88 
89     /**
90      * Returns a composite selector given a number of resource fields and a container name. This
91      * method also takes an {@code extraResourceFields} parameter to specify some extra resource
92      * fields as strings. The string selector returned by this method can be used for field
93      * selection in API calls that return a list of resources. This method is not supposed to be
94      * used directly by users.
95      */
listSelector( String containerName, List<? extends FieldSelector> required, FieldSelector[] others, String... extraResourceFields)96     public static String listSelector(
97         String containerName,
98         List<? extends FieldSelector> required,
99         FieldSelector[] others,
100         String... extraResourceFields) {
101       return listSelector(EMPTY_FIELDS, containerName, required, others, extraResourceFields);
102     }
103 
104     /**
105      * Returns a composite selector given a number of top level fields as strings, a number of
106      * resource fields and a container name. This method also takes an {@code extraResourceFields}
107      * parameter to specify some extra resource fields as strings. The string selector returned by
108      * this method can be used for field selection in API calls that return a list of resources.
109      * This method is not supposed to be used directly by users.
110      */
listSelector( String[] topLevelFields, String containerName, List<? extends FieldSelector> required, FieldSelector[] others, String... extraResourceFields)111     public static String listSelector(
112         String[] topLevelFields,
113         String containerName,
114         List<? extends FieldSelector> required,
115         FieldSelector[] others,
116         String... extraResourceFields) {
117       Set<String> topLevelStrings = Sets.newHashSetWithExpectedSize(topLevelFields.length + 1);
118       topLevelStrings.addAll(Lists.asList("nextPageToken", topLevelFields));
119       return Joiner.on(',').join(topLevelStrings)
120           + ","
121           + containerName
122           + '('
123           + selector(required, others, extraResourceFields)
124           + ')';
125     }
126   }
127 }
128