• 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.dialogflow.v2;
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 DocumentName implements ResourceName {
34   private static final PathTemplate PROJECT_KNOWLEDGE_BASE_DOCUMENT =
35       PathTemplate.createWithoutUrlEncoding(
36           "projects/{project}/knowledgeBases/{knowledge_base}/documents/{document}");
37   private static final PathTemplate PROJECT_LOCATION_KNOWLEDGE_BASE_DOCUMENT =
38       PathTemplate.createWithoutUrlEncoding(
39           "projects/{project}/locations/{location}/knowledgeBases/{knowledge_base}/documents/{document}");
40   private volatile Map<String, String> fieldValuesMap;
41   private PathTemplate pathTemplate;
42   private String fixedValue;
43   private final String project;
44   private final String knowledgeBase;
45   private final String document;
46   private final String location;
47 
48   @Deprecated
DocumentName()49   protected DocumentName() {
50     project = null;
51     knowledgeBase = null;
52     document = null;
53     location = null;
54   }
55 
DocumentName(Builder builder)56   private DocumentName(Builder builder) {
57     project = Preconditions.checkNotNull(builder.getProject());
58     knowledgeBase = Preconditions.checkNotNull(builder.getKnowledgeBase());
59     document = Preconditions.checkNotNull(builder.getDocument());
60     location = null;
61     pathTemplate = PROJECT_KNOWLEDGE_BASE_DOCUMENT;
62   }
63 
DocumentName(ProjectLocationKnowledgeBaseDocumentBuilder builder)64   private DocumentName(ProjectLocationKnowledgeBaseDocumentBuilder builder) {
65     project = Preconditions.checkNotNull(builder.getProject());
66     location = Preconditions.checkNotNull(builder.getLocation());
67     knowledgeBase = Preconditions.checkNotNull(builder.getKnowledgeBase());
68     document = Preconditions.checkNotNull(builder.getDocument());
69     pathTemplate = PROJECT_LOCATION_KNOWLEDGE_BASE_DOCUMENT;
70   }
71 
getProject()72   public String getProject() {
73     return project;
74   }
75 
getKnowledgeBase()76   public String getKnowledgeBase() {
77     return knowledgeBase;
78   }
79 
getDocument()80   public String getDocument() {
81     return document;
82   }
83 
getLocation()84   public String getLocation() {
85     return location;
86   }
87 
newBuilder()88   public static Builder newBuilder() {
89     return new Builder();
90   }
91 
92   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newProjectKnowledgeBaseDocumentBuilder()93   public static Builder newProjectKnowledgeBaseDocumentBuilder() {
94     return new Builder();
95   }
96 
97   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
98   public static ProjectLocationKnowledgeBaseDocumentBuilder
newProjectLocationKnowledgeBaseDocumentBuilder()99       newProjectLocationKnowledgeBaseDocumentBuilder() {
100     return new ProjectLocationKnowledgeBaseDocumentBuilder();
101   }
102 
toBuilder()103   public Builder toBuilder() {
104     return new Builder(this);
105   }
106 
of(String project, String knowledgeBase, String document)107   public static DocumentName of(String project, String knowledgeBase, String document) {
108     return newBuilder()
109         .setProject(project)
110         .setKnowledgeBase(knowledgeBase)
111         .setDocument(document)
112         .build();
113   }
114 
115   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofProjectKnowledgeBaseDocumentName( String project, String knowledgeBase, String document)116   public static DocumentName ofProjectKnowledgeBaseDocumentName(
117       String project, String knowledgeBase, String document) {
118     return newBuilder()
119         .setProject(project)
120         .setKnowledgeBase(knowledgeBase)
121         .setDocument(document)
122         .build();
123   }
124 
125   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofProjectLocationKnowledgeBaseDocumentName( String project, String location, String knowledgeBase, String document)126   public static DocumentName ofProjectLocationKnowledgeBaseDocumentName(
127       String project, String location, String knowledgeBase, String document) {
128     return newProjectLocationKnowledgeBaseDocumentBuilder()
129         .setProject(project)
130         .setLocation(location)
131         .setKnowledgeBase(knowledgeBase)
132         .setDocument(document)
133         .build();
134   }
135 
format(String project, String knowledgeBase, String document)136   public static String format(String project, String knowledgeBase, String document) {
137     return newBuilder()
138         .setProject(project)
139         .setKnowledgeBase(knowledgeBase)
140         .setDocument(document)
141         .build()
142         .toString();
143   }
144 
145   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatProjectKnowledgeBaseDocumentName( String project, String knowledgeBase, String document)146   public static String formatProjectKnowledgeBaseDocumentName(
147       String project, String knowledgeBase, String document) {
148     return newBuilder()
149         .setProject(project)
150         .setKnowledgeBase(knowledgeBase)
151         .setDocument(document)
152         .build()
153         .toString();
154   }
155 
156   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatProjectLocationKnowledgeBaseDocumentName( String project, String location, String knowledgeBase, String document)157   public static String formatProjectLocationKnowledgeBaseDocumentName(
158       String project, String location, String knowledgeBase, String document) {
159     return newProjectLocationKnowledgeBaseDocumentBuilder()
160         .setProject(project)
161         .setLocation(location)
162         .setKnowledgeBase(knowledgeBase)
163         .setDocument(document)
164         .build()
165         .toString();
166   }
167 
parse(String formattedString)168   public static DocumentName parse(String formattedString) {
169     if (formattedString.isEmpty()) {
170       return null;
171     }
172     if (PROJECT_KNOWLEDGE_BASE_DOCUMENT.matches(formattedString)) {
173       Map<String, String> matchMap = PROJECT_KNOWLEDGE_BASE_DOCUMENT.match(formattedString);
174       return ofProjectKnowledgeBaseDocumentName(
175           matchMap.get("project"), matchMap.get("knowledge_base"), matchMap.get("document"));
176     } else if (PROJECT_LOCATION_KNOWLEDGE_BASE_DOCUMENT.matches(formattedString)) {
177       Map<String, String> matchMap =
178           PROJECT_LOCATION_KNOWLEDGE_BASE_DOCUMENT.match(formattedString);
179       return ofProjectLocationKnowledgeBaseDocumentName(
180           matchMap.get("project"),
181           matchMap.get("location"),
182           matchMap.get("knowledge_base"),
183           matchMap.get("document"));
184     }
185     throw new ValidationException("DocumentName.parse: formattedString not in valid format");
186   }
187 
parseList(List<String> formattedStrings)188   public static List<DocumentName> parseList(List<String> formattedStrings) {
189     List<DocumentName> list = new ArrayList<>(formattedStrings.size());
190     for (String formattedString : formattedStrings) {
191       list.add(parse(formattedString));
192     }
193     return list;
194   }
195 
toStringList(List<DocumentName> values)196   public static List<String> toStringList(List<DocumentName> values) {
197     List<String> list = new ArrayList<>(values.size());
198     for (DocumentName value : values) {
199       if (value == null) {
200         list.add("");
201       } else {
202         list.add(value.toString());
203       }
204     }
205     return list;
206   }
207 
isParsableFrom(String formattedString)208   public static boolean isParsableFrom(String formattedString) {
209     return PROJECT_KNOWLEDGE_BASE_DOCUMENT.matches(formattedString)
210         || PROJECT_LOCATION_KNOWLEDGE_BASE_DOCUMENT.matches(formattedString);
211   }
212 
213   @Override
getFieldValuesMap()214   public Map<String, String> getFieldValuesMap() {
215     if (fieldValuesMap == null) {
216       synchronized (this) {
217         if (fieldValuesMap == null) {
218           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
219           if (project != null) {
220             fieldMapBuilder.put("project", project);
221           }
222           if (knowledgeBase != null) {
223             fieldMapBuilder.put("knowledge_base", knowledgeBase);
224           }
225           if (document != null) {
226             fieldMapBuilder.put("document", document);
227           }
228           if (location != null) {
229             fieldMapBuilder.put("location", location);
230           }
231           fieldValuesMap = fieldMapBuilder.build();
232         }
233       }
234     }
235     return fieldValuesMap;
236   }
237 
getFieldValue(String fieldName)238   public String getFieldValue(String fieldName) {
239     return getFieldValuesMap().get(fieldName);
240   }
241 
242   @Override
toString()243   public String toString() {
244     return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap());
245   }
246 
247   @Override
equals(Object o)248   public boolean equals(Object o) {
249     if (o == this) {
250       return true;
251     }
252     if (o != null || getClass() == o.getClass()) {
253       DocumentName that = ((DocumentName) o);
254       return Objects.equals(this.project, that.project)
255           && Objects.equals(this.knowledgeBase, that.knowledgeBase)
256           && Objects.equals(this.document, that.document)
257           && Objects.equals(this.location, that.location);
258     }
259     return false;
260   }
261 
262   @Override
hashCode()263   public int hashCode() {
264     int h = 1;
265     h *= 1000003;
266     h ^= Objects.hashCode(fixedValue);
267     h *= 1000003;
268     h ^= Objects.hashCode(project);
269     h *= 1000003;
270     h ^= Objects.hashCode(knowledgeBase);
271     h *= 1000003;
272     h ^= Objects.hashCode(document);
273     h *= 1000003;
274     h ^= Objects.hashCode(location);
275     return h;
276   }
277 
278   /** Builder for projects/{project}/knowledgeBases/{knowledge_base}/documents/{document}. */
279   public static class Builder {
280     private String project;
281     private String knowledgeBase;
282     private String document;
283 
Builder()284     protected Builder() {}
285 
getProject()286     public String getProject() {
287       return project;
288     }
289 
getKnowledgeBase()290     public String getKnowledgeBase() {
291       return knowledgeBase;
292     }
293 
getDocument()294     public String getDocument() {
295       return document;
296     }
297 
setProject(String project)298     public Builder setProject(String project) {
299       this.project = project;
300       return this;
301     }
302 
setKnowledgeBase(String knowledgeBase)303     public Builder setKnowledgeBase(String knowledgeBase) {
304       this.knowledgeBase = knowledgeBase;
305       return this;
306     }
307 
setDocument(String document)308     public Builder setDocument(String document) {
309       this.document = document;
310       return this;
311     }
312 
Builder(DocumentName documentName)313     private Builder(DocumentName documentName) {
314       Preconditions.checkArgument(
315           Objects.equals(documentName.pathTemplate, PROJECT_KNOWLEDGE_BASE_DOCUMENT),
316           "toBuilder is only supported when DocumentName has the pattern of projects/{project}/knowledgeBases/{knowledge_base}/documents/{document}");
317       this.project = documentName.project;
318       this.knowledgeBase = documentName.knowledgeBase;
319       this.document = documentName.document;
320     }
321 
build()322     public DocumentName build() {
323       return new DocumentName(this);
324     }
325   }
326 
327   /**
328    * Builder for
329    * projects/{project}/locations/{location}/knowledgeBases/{knowledge_base}/documents/{document}.
330    */
331   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
332   public static class ProjectLocationKnowledgeBaseDocumentBuilder {
333     private String project;
334     private String location;
335     private String knowledgeBase;
336     private String document;
337 
ProjectLocationKnowledgeBaseDocumentBuilder()338     protected ProjectLocationKnowledgeBaseDocumentBuilder() {}
339 
getProject()340     public String getProject() {
341       return project;
342     }
343 
getLocation()344     public String getLocation() {
345       return location;
346     }
347 
getKnowledgeBase()348     public String getKnowledgeBase() {
349       return knowledgeBase;
350     }
351 
getDocument()352     public String getDocument() {
353       return document;
354     }
355 
setProject(String project)356     public ProjectLocationKnowledgeBaseDocumentBuilder setProject(String project) {
357       this.project = project;
358       return this;
359     }
360 
setLocation(String location)361     public ProjectLocationKnowledgeBaseDocumentBuilder setLocation(String location) {
362       this.location = location;
363       return this;
364     }
365 
setKnowledgeBase(String knowledgeBase)366     public ProjectLocationKnowledgeBaseDocumentBuilder setKnowledgeBase(String knowledgeBase) {
367       this.knowledgeBase = knowledgeBase;
368       return this;
369     }
370 
setDocument(String document)371     public ProjectLocationKnowledgeBaseDocumentBuilder setDocument(String document) {
372       this.document = document;
373       return this;
374     }
375 
build()376     public DocumentName build() {
377       return new DocumentName(this);
378     }
379   }
380 }
381