• 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.analytics.admin.v1alpha;
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 UserLinkName implements ResourceName {
34   private static final PathTemplate ACCOUNT_USER_LINK =
35       PathTemplate.createWithoutUrlEncoding("accounts/{account}/userLinks/{user_link}");
36   private static final PathTemplate PROPERTY_USER_LINK =
37       PathTemplate.createWithoutUrlEncoding("properties/{property}/userLinks/{user_link}");
38   private volatile Map<String, String> fieldValuesMap;
39   private PathTemplate pathTemplate;
40   private String fixedValue;
41   private final String account;
42   private final String userLink;
43   private final String property;
44 
45   @Deprecated
UserLinkName()46   protected UserLinkName() {
47     account = null;
48     userLink = null;
49     property = null;
50   }
51 
UserLinkName(Builder builder)52   private UserLinkName(Builder builder) {
53     account = Preconditions.checkNotNull(builder.getAccount());
54     userLink = Preconditions.checkNotNull(builder.getUserLink());
55     property = null;
56     pathTemplate = ACCOUNT_USER_LINK;
57   }
58 
UserLinkName(PropertyUserLinkBuilder builder)59   private UserLinkName(PropertyUserLinkBuilder builder) {
60     property = Preconditions.checkNotNull(builder.getProperty());
61     userLink = Preconditions.checkNotNull(builder.getUserLink());
62     account = null;
63     pathTemplate = PROPERTY_USER_LINK;
64   }
65 
getAccount()66   public String getAccount() {
67     return account;
68   }
69 
getUserLink()70   public String getUserLink() {
71     return userLink;
72   }
73 
getProperty()74   public String getProperty() {
75     return property;
76   }
77 
newBuilder()78   public static Builder newBuilder() {
79     return new Builder();
80   }
81 
82   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newAccountUserLinkBuilder()83   public static Builder newAccountUserLinkBuilder() {
84     return new Builder();
85   }
86 
87   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newPropertyUserLinkBuilder()88   public static PropertyUserLinkBuilder newPropertyUserLinkBuilder() {
89     return new PropertyUserLinkBuilder();
90   }
91 
toBuilder()92   public Builder toBuilder() {
93     return new Builder(this);
94   }
95 
of(String account, String userLink)96   public static UserLinkName of(String account, String userLink) {
97     return newBuilder().setAccount(account).setUserLink(userLink).build();
98   }
99 
100   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofAccountUserLinkName(String account, String userLink)101   public static UserLinkName ofAccountUserLinkName(String account, String userLink) {
102     return newBuilder().setAccount(account).setUserLink(userLink).build();
103   }
104 
105   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofPropertyUserLinkName(String property, String userLink)106   public static UserLinkName ofPropertyUserLinkName(String property, String userLink) {
107     return newPropertyUserLinkBuilder().setProperty(property).setUserLink(userLink).build();
108   }
109 
format(String account, String userLink)110   public static String format(String account, String userLink) {
111     return newBuilder().setAccount(account).setUserLink(userLink).build().toString();
112   }
113 
114   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatAccountUserLinkName(String account, String userLink)115   public static String formatAccountUserLinkName(String account, String userLink) {
116     return newBuilder().setAccount(account).setUserLink(userLink).build().toString();
117   }
118 
119   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatPropertyUserLinkName(String property, String userLink)120   public static String formatPropertyUserLinkName(String property, String userLink) {
121     return newPropertyUserLinkBuilder()
122         .setProperty(property)
123         .setUserLink(userLink)
124         .build()
125         .toString();
126   }
127 
parse(String formattedString)128   public static UserLinkName parse(String formattedString) {
129     if (formattedString.isEmpty()) {
130       return null;
131     }
132     if (ACCOUNT_USER_LINK.matches(formattedString)) {
133       Map<String, String> matchMap = ACCOUNT_USER_LINK.match(formattedString);
134       return ofAccountUserLinkName(matchMap.get("account"), matchMap.get("user_link"));
135     } else if (PROPERTY_USER_LINK.matches(formattedString)) {
136       Map<String, String> matchMap = PROPERTY_USER_LINK.match(formattedString);
137       return ofPropertyUserLinkName(matchMap.get("property"), matchMap.get("user_link"));
138     }
139     throw new ValidationException("UserLinkName.parse: formattedString not in valid format");
140   }
141 
parseList(List<String> formattedStrings)142   public static List<UserLinkName> parseList(List<String> formattedStrings) {
143     List<UserLinkName> list = new ArrayList<>(formattedStrings.size());
144     for (String formattedString : formattedStrings) {
145       list.add(parse(formattedString));
146     }
147     return list;
148   }
149 
toStringList(List<UserLinkName> values)150   public static List<String> toStringList(List<UserLinkName> values) {
151     List<String> list = new ArrayList<>(values.size());
152     for (UserLinkName value : values) {
153       if (value == null) {
154         list.add("");
155       } else {
156         list.add(value.toString());
157       }
158     }
159     return list;
160   }
161 
isParsableFrom(String formattedString)162   public static boolean isParsableFrom(String formattedString) {
163     return ACCOUNT_USER_LINK.matches(formattedString)
164         || PROPERTY_USER_LINK.matches(formattedString);
165   }
166 
167   @Override
getFieldValuesMap()168   public Map<String, String> getFieldValuesMap() {
169     if (fieldValuesMap == null) {
170       synchronized (this) {
171         if (fieldValuesMap == null) {
172           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
173           if (account != null) {
174             fieldMapBuilder.put("account", account);
175           }
176           if (userLink != null) {
177             fieldMapBuilder.put("user_link", userLink);
178           }
179           if (property != null) {
180             fieldMapBuilder.put("property", property);
181           }
182           fieldValuesMap = fieldMapBuilder.build();
183         }
184       }
185     }
186     return fieldValuesMap;
187   }
188 
getFieldValue(String fieldName)189   public String getFieldValue(String fieldName) {
190     return getFieldValuesMap().get(fieldName);
191   }
192 
193   @Override
toString()194   public String toString() {
195     return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap());
196   }
197 
198   @Override
equals(Object o)199   public boolean equals(Object o) {
200     if (o == this) {
201       return true;
202     }
203     if (o != null || getClass() == o.getClass()) {
204       UserLinkName that = ((UserLinkName) o);
205       return Objects.equals(this.account, that.account)
206           && Objects.equals(this.userLink, that.userLink)
207           && Objects.equals(this.property, that.property);
208     }
209     return false;
210   }
211 
212   @Override
hashCode()213   public int hashCode() {
214     int h = 1;
215     h *= 1000003;
216     h ^= Objects.hashCode(fixedValue);
217     h *= 1000003;
218     h ^= Objects.hashCode(account);
219     h *= 1000003;
220     h ^= Objects.hashCode(userLink);
221     h *= 1000003;
222     h ^= Objects.hashCode(property);
223     return h;
224   }
225 
226   /** Builder for accounts/{account}/userLinks/{user_link}. */
227   public static class Builder {
228     private String account;
229     private String userLink;
230 
Builder()231     protected Builder() {}
232 
getAccount()233     public String getAccount() {
234       return account;
235     }
236 
getUserLink()237     public String getUserLink() {
238       return userLink;
239     }
240 
setAccount(String account)241     public Builder setAccount(String account) {
242       this.account = account;
243       return this;
244     }
245 
setUserLink(String userLink)246     public Builder setUserLink(String userLink) {
247       this.userLink = userLink;
248       return this;
249     }
250 
Builder(UserLinkName userLinkName)251     private Builder(UserLinkName userLinkName) {
252       Preconditions.checkArgument(
253           Objects.equals(userLinkName.pathTemplate, ACCOUNT_USER_LINK),
254           "toBuilder is only supported when UserLinkName has the pattern of accounts/{account}/userLinks/{user_link}");
255       this.account = userLinkName.account;
256       this.userLink = userLinkName.userLink;
257     }
258 
build()259     public UserLinkName build() {
260       return new UserLinkName(this);
261     }
262   }
263 
264   /** Builder for properties/{property}/userLinks/{user_link}. */
265   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
266   public static class PropertyUserLinkBuilder {
267     private String property;
268     private String userLink;
269 
PropertyUserLinkBuilder()270     protected PropertyUserLinkBuilder() {}
271 
getProperty()272     public String getProperty() {
273       return property;
274     }
275 
getUserLink()276     public String getUserLink() {
277       return userLink;
278     }
279 
setProperty(String property)280     public PropertyUserLinkBuilder setProperty(String property) {
281       this.property = property;
282       return this;
283     }
284 
setUserLink(String userLink)285     public PropertyUserLinkBuilder setUserLink(String userLink) {
286       this.userLink = userLink;
287       return this;
288     }
289 
build()290     public UserLinkName build() {
291       return new UserLinkName(this);
292     }
293   }
294 }
295