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