• 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.devtools.cloudtrace.v2;
18 
19 import com.google.api.pathtemplate.PathTemplate;
20 import com.google.api.resourcenames.ResourceName;
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.ImmutableMap;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import javax.annotation.Generated;
28 
29 // AUTO-GENERATED DOCUMENTATION AND CLASS.
30 @Generated("by gapic-generator-java")
31 public class SpanName implements ResourceName {
32   private static final PathTemplate PROJECT_TRACE_SPAN =
33       PathTemplate.createWithoutUrlEncoding("projects/{project}/traces/{trace}/spans/{span}");
34   private volatile Map<String, String> fieldValuesMap;
35   private final String project;
36   private final String trace;
37   private final String span;
38 
39   @Deprecated
SpanName()40   protected SpanName() {
41     project = null;
42     trace = null;
43     span = null;
44   }
45 
SpanName(Builder builder)46   private SpanName(Builder builder) {
47     project = Preconditions.checkNotNull(builder.getProject());
48     trace = Preconditions.checkNotNull(builder.getTrace());
49     span = Preconditions.checkNotNull(builder.getSpan());
50   }
51 
getProject()52   public String getProject() {
53     return project;
54   }
55 
getTrace()56   public String getTrace() {
57     return trace;
58   }
59 
getSpan()60   public String getSpan() {
61     return span;
62   }
63 
newBuilder()64   public static Builder newBuilder() {
65     return new Builder();
66   }
67 
toBuilder()68   public Builder toBuilder() {
69     return new Builder(this);
70   }
71 
of(String project, String trace, String span)72   public static SpanName of(String project, String trace, String span) {
73     return newBuilder().setProject(project).setTrace(trace).setSpan(span).build();
74   }
75 
format(String project, String trace, String span)76   public static String format(String project, String trace, String span) {
77     return newBuilder().setProject(project).setTrace(trace).setSpan(span).build().toString();
78   }
79 
parse(String formattedString)80   public static SpanName parse(String formattedString) {
81     if (formattedString.isEmpty()) {
82       return null;
83     }
84     Map<String, String> matchMap =
85         PROJECT_TRACE_SPAN.validatedMatch(
86             formattedString, "SpanName.parse: formattedString not in valid format");
87     return of(matchMap.get("project"), matchMap.get("trace"), matchMap.get("span"));
88   }
89 
parseList(List<String> formattedStrings)90   public static List<SpanName> parseList(List<String> formattedStrings) {
91     List<SpanName> list = new ArrayList<>(formattedStrings.size());
92     for (String formattedString : formattedStrings) {
93       list.add(parse(formattedString));
94     }
95     return list;
96   }
97 
toStringList(List<SpanName> values)98   public static List<String> toStringList(List<SpanName> values) {
99     List<String> list = new ArrayList<>(values.size());
100     for (SpanName value : values) {
101       if (value == null) {
102         list.add("");
103       } else {
104         list.add(value.toString());
105       }
106     }
107     return list;
108   }
109 
isParsableFrom(String formattedString)110   public static boolean isParsableFrom(String formattedString) {
111     return PROJECT_TRACE_SPAN.matches(formattedString);
112   }
113 
114   @Override
getFieldValuesMap()115   public Map<String, String> getFieldValuesMap() {
116     if (fieldValuesMap == null) {
117       synchronized (this) {
118         if (fieldValuesMap == null) {
119           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
120           if (project != null) {
121             fieldMapBuilder.put("project", project);
122           }
123           if (trace != null) {
124             fieldMapBuilder.put("trace", trace);
125           }
126           if (span != null) {
127             fieldMapBuilder.put("span", span);
128           }
129           fieldValuesMap = fieldMapBuilder.build();
130         }
131       }
132     }
133     return fieldValuesMap;
134   }
135 
getFieldValue(String fieldName)136   public String getFieldValue(String fieldName) {
137     return getFieldValuesMap().get(fieldName);
138   }
139 
140   @Override
toString()141   public String toString() {
142     return PROJECT_TRACE_SPAN.instantiate("project", project, "trace", trace, "span", span);
143   }
144 
145   @Override
equals(Object o)146   public boolean equals(Object o) {
147     if (o == this) {
148       return true;
149     }
150     if (o != null || getClass() == o.getClass()) {
151       SpanName that = ((SpanName) o);
152       return Objects.equals(this.project, that.project)
153           && Objects.equals(this.trace, that.trace)
154           && Objects.equals(this.span, that.span);
155     }
156     return false;
157   }
158 
159   @Override
hashCode()160   public int hashCode() {
161     int h = 1;
162     h *= 1000003;
163     h ^= Objects.hashCode(project);
164     h *= 1000003;
165     h ^= Objects.hashCode(trace);
166     h *= 1000003;
167     h ^= Objects.hashCode(span);
168     return h;
169   }
170 
171   /** Builder for projects/{project}/traces/{trace}/spans/{span}. */
172   public static class Builder {
173     private String project;
174     private String trace;
175     private String span;
176 
Builder()177     protected Builder() {}
178 
getProject()179     public String getProject() {
180       return project;
181     }
182 
getTrace()183     public String getTrace() {
184       return trace;
185     }
186 
getSpan()187     public String getSpan() {
188       return span;
189     }
190 
setProject(String project)191     public Builder setProject(String project) {
192       this.project = project;
193       return this;
194     }
195 
setTrace(String trace)196     public Builder setTrace(String trace) {
197       this.trace = trace;
198       return this;
199     }
200 
setSpan(String span)201     public Builder setSpan(String span) {
202       this.span = span;
203       return this;
204     }
205 
Builder(SpanName spanName)206     private Builder(SpanName spanName) {
207       this.project = spanName.project;
208       this.trace = spanName.trace;
209       this.span = spanName.span;
210     }
211 
build()212     public SpanName build() {
213       return new SpanName(this);
214     }
215   }
216 }
217