• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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  *      https://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.asset.v1;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.api.pathtemplate.PathTemplate;
21 import com.google.api.pathtemplate.ValidationException;
22 import com.google.api.resourcenames.ResourceName;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import javax.annotation.Generated;
30 
31 // AUTO-GENERATED DOCUMENTATION AND CLASS.
32 @Generated("by gapic-generator-java")
33 public class FeedName implements ResourceName {
34   private static final PathTemplate PROJECT_FEED =
35       PathTemplate.createWithoutUrlEncoding("projects/{project}/feeds/{feed}");
36   private static final PathTemplate FOLDER_FEED =
37       PathTemplate.createWithoutUrlEncoding("folders/{folder}/feeds/{feed}");
38   private static final PathTemplate ORGANIZATION_FEED =
39       PathTemplate.createWithoutUrlEncoding("organizations/{organization}/feeds/{feed}");
40   private volatile Map<String, String> fieldValuesMap;
41   private PathTemplate pathTemplate;
42   private String fixedValue;
43   private final String project;
44   private final String feed;
45   private final String folder;
46   private final String organization;
47 
48   @Deprecated
FeedName()49   protected FeedName() {
50     project = null;
51     feed = null;
52     folder = null;
53     organization = null;
54   }
55 
FeedName(Builder builder)56   private FeedName(Builder builder) {
57     project = Preconditions.checkNotNull(builder.getProject());
58     feed = Preconditions.checkNotNull(builder.getFeed());
59     folder = null;
60     organization = null;
61     pathTemplate = PROJECT_FEED;
62   }
63 
FeedName(FolderFeedBuilder builder)64   private FeedName(FolderFeedBuilder builder) {
65     folder = Preconditions.checkNotNull(builder.getFolder());
66     feed = Preconditions.checkNotNull(builder.getFeed());
67     project = null;
68     organization = null;
69     pathTemplate = FOLDER_FEED;
70   }
71 
FeedName(OrganizationFeedBuilder builder)72   private FeedName(OrganizationFeedBuilder builder) {
73     organization = Preconditions.checkNotNull(builder.getOrganization());
74     feed = Preconditions.checkNotNull(builder.getFeed());
75     project = null;
76     folder = null;
77     pathTemplate = ORGANIZATION_FEED;
78   }
79 
getProject()80   public String getProject() {
81     return project;
82   }
83 
getFeed()84   public String getFeed() {
85     return feed;
86   }
87 
getFolder()88   public String getFolder() {
89     return folder;
90   }
91 
getOrganization()92   public String getOrganization() {
93     return organization;
94   }
95 
newBuilder()96   public static Builder newBuilder() {
97     return new Builder();
98   }
99 
100   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newProjectFeedBuilder()101   public static Builder newProjectFeedBuilder() {
102     return new Builder();
103   }
104 
105   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newFolderFeedBuilder()106   public static FolderFeedBuilder newFolderFeedBuilder() {
107     return new FolderFeedBuilder();
108   }
109 
110   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newOrganizationFeedBuilder()111   public static OrganizationFeedBuilder newOrganizationFeedBuilder() {
112     return new OrganizationFeedBuilder();
113   }
114 
toBuilder()115   public Builder toBuilder() {
116     return new Builder(this);
117   }
118 
of(String project, String feed)119   public static FeedName of(String project, String feed) {
120     return newBuilder().setProject(project).setFeed(feed).build();
121   }
122 
123   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofProjectFeedName(String project, String feed)124   public static FeedName ofProjectFeedName(String project, String feed) {
125     return newBuilder().setProject(project).setFeed(feed).build();
126   }
127 
128   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofFolderFeedName(String folder, String feed)129   public static FeedName ofFolderFeedName(String folder, String feed) {
130     return newFolderFeedBuilder().setFolder(folder).setFeed(feed).build();
131   }
132 
133   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofOrganizationFeedName(String organization, String feed)134   public static FeedName ofOrganizationFeedName(String organization, String feed) {
135     return newOrganizationFeedBuilder().setOrganization(organization).setFeed(feed).build();
136   }
137 
format(String project, String feed)138   public static String format(String project, String feed) {
139     return newBuilder().setProject(project).setFeed(feed).build().toString();
140   }
141 
142   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatProjectFeedName(String project, String feed)143   public static String formatProjectFeedName(String project, String feed) {
144     return newBuilder().setProject(project).setFeed(feed).build().toString();
145   }
146 
147   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatFolderFeedName(String folder, String feed)148   public static String formatFolderFeedName(String folder, String feed) {
149     return newFolderFeedBuilder().setFolder(folder).setFeed(feed).build().toString();
150   }
151 
152   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatOrganizationFeedName(String organization, String feed)153   public static String formatOrganizationFeedName(String organization, String feed) {
154     return newOrganizationFeedBuilder()
155         .setOrganization(organization)
156         .setFeed(feed)
157         .build()
158         .toString();
159   }
160 
parse(String formattedString)161   public static FeedName parse(String formattedString) {
162     if (formattedString.isEmpty()) {
163       return null;
164     }
165     if (PROJECT_FEED.matches(formattedString)) {
166       Map<String, String> matchMap = PROJECT_FEED.match(formattedString);
167       return ofProjectFeedName(matchMap.get("project"), matchMap.get("feed"));
168     } else if (FOLDER_FEED.matches(formattedString)) {
169       Map<String, String> matchMap = FOLDER_FEED.match(formattedString);
170       return ofFolderFeedName(matchMap.get("folder"), matchMap.get("feed"));
171     } else if (ORGANIZATION_FEED.matches(formattedString)) {
172       Map<String, String> matchMap = ORGANIZATION_FEED.match(formattedString);
173       return ofOrganizationFeedName(matchMap.get("organization"), matchMap.get("feed"));
174     }
175     throw new ValidationException("FeedName.parse: formattedString not in valid format");
176   }
177 
parseList(List<String> formattedStrings)178   public static List<FeedName> parseList(List<String> formattedStrings) {
179     List<FeedName> list = new ArrayList<>(formattedStrings.size());
180     for (String formattedString : formattedStrings) {
181       list.add(parse(formattedString));
182     }
183     return list;
184   }
185 
toStringList(List<FeedName> values)186   public static List<String> toStringList(List<FeedName> values) {
187     List<String> list = new ArrayList<>(values.size());
188     for (FeedName value : values) {
189       if (value == null) {
190         list.add("");
191       } else {
192         list.add(value.toString());
193       }
194     }
195     return list;
196   }
197 
isParsableFrom(String formattedString)198   public static boolean isParsableFrom(String formattedString) {
199     return PROJECT_FEED.matches(formattedString)
200         || FOLDER_FEED.matches(formattedString)
201         || ORGANIZATION_FEED.matches(formattedString);
202   }
203 
204   @Override
getFieldValuesMap()205   public Map<String, String> getFieldValuesMap() {
206     if (fieldValuesMap == null) {
207       synchronized (this) {
208         if (fieldValuesMap == null) {
209           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
210           if (project != null) {
211             fieldMapBuilder.put("project", project);
212           }
213           if (feed != null) {
214             fieldMapBuilder.put("feed", feed);
215           }
216           if (folder != null) {
217             fieldMapBuilder.put("folder", folder);
218           }
219           if (organization != null) {
220             fieldMapBuilder.put("organization", organization);
221           }
222           fieldValuesMap = fieldMapBuilder.build();
223         }
224       }
225     }
226     return fieldValuesMap;
227   }
228 
getFieldValue(String fieldName)229   public String getFieldValue(String fieldName) {
230     return getFieldValuesMap().get(fieldName);
231   }
232 
233   @Override
toString()234   public String toString() {
235     return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap());
236   }
237 
238   @Override
equals(Object o)239   public boolean equals(Object o) {
240     if (o == this) {
241       return true;
242     }
243     if (o != null || getClass() == o.getClass()) {
244       FeedName that = ((FeedName) o);
245       return Objects.equals(this.project, that.project)
246           && Objects.equals(this.feed, that.feed)
247           && Objects.equals(this.folder, that.folder)
248           && Objects.equals(this.organization, that.organization);
249     }
250     return false;
251   }
252 
253   @Override
hashCode()254   public int hashCode() {
255     int h = 1;
256     h *= 1000003;
257     h ^= Objects.hashCode(fixedValue);
258     h *= 1000003;
259     h ^= Objects.hashCode(project);
260     h *= 1000003;
261     h ^= Objects.hashCode(feed);
262     h *= 1000003;
263     h ^= Objects.hashCode(folder);
264     h *= 1000003;
265     h ^= Objects.hashCode(organization);
266     return h;
267   }
268 
269   /** Builder for projects/{project}/feeds/{feed}. */
270   public static class Builder {
271     private String project;
272     private String feed;
273 
Builder()274     protected Builder() {}
275 
getProject()276     public String getProject() {
277       return project;
278     }
279 
getFeed()280     public String getFeed() {
281       return feed;
282     }
283 
setProject(String project)284     public Builder setProject(String project) {
285       this.project = project;
286       return this;
287     }
288 
setFeed(String feed)289     public Builder setFeed(String feed) {
290       this.feed = feed;
291       return this;
292     }
293 
Builder(FeedName feedName)294     private Builder(FeedName feedName) {
295       Preconditions.checkArgument(
296           Objects.equals(feedName.pathTemplate, PROJECT_FEED),
297           "toBuilder is only supported when FeedName has the pattern of projects/{project}/feeds/{feed}");
298       this.project = feedName.project;
299       this.feed = feedName.feed;
300     }
301 
build()302     public FeedName build() {
303       return new FeedName(this);
304     }
305   }
306 
307   /** Builder for folders/{folder}/feeds/{feed}. */
308   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
309   public static class FolderFeedBuilder {
310     private String folder;
311     private String feed;
312 
FolderFeedBuilder()313     protected FolderFeedBuilder() {}
314 
getFolder()315     public String getFolder() {
316       return folder;
317     }
318 
getFeed()319     public String getFeed() {
320       return feed;
321     }
322 
setFolder(String folder)323     public FolderFeedBuilder setFolder(String folder) {
324       this.folder = folder;
325       return this;
326     }
327 
setFeed(String feed)328     public FolderFeedBuilder setFeed(String feed) {
329       this.feed = feed;
330       return this;
331     }
332 
build()333     public FeedName build() {
334       return new FeedName(this);
335     }
336   }
337 
338   /** Builder for organizations/{organization}/feeds/{feed}. */
339   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
340   public static class OrganizationFeedBuilder {
341     private String organization;
342     private String feed;
343 
OrganizationFeedBuilder()344     protected OrganizationFeedBuilder() {}
345 
getOrganization()346     public String getOrganization() {
347       return organization;
348     }
349 
getFeed()350     public String getFeed() {
351       return feed;
352     }
353 
setOrganization(String organization)354     public OrganizationFeedBuilder setOrganization(String organization) {
355       this.organization = organization;
356       return this;
357     }
358 
setFeed(String feed)359     public OrganizationFeedBuilder setFeed(String feed) {
360       this.feed = feed;
361       return this;
362     }
363 
build()364     public FeedName build() {
365       return new FeedName(this);
366     }
367   }
368 }
369