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