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.orgpolicy.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 ConstraintName implements ResourceName { 34 private static final PathTemplate PROJECT_CONSTRAINT = 35 PathTemplate.createWithoutUrlEncoding("projects/{project}/constraints/{constraint}"); 36 private static final PathTemplate FOLDER_CONSTRAINT = 37 PathTemplate.createWithoutUrlEncoding("folders/{folder}/constraints/{constraint}"); 38 private static final PathTemplate ORGANIZATION_CONSTRAINT = 39 PathTemplate.createWithoutUrlEncoding( 40 "organizations/{organization}/constraints/{constraint}"); 41 private volatile Map<String, String> fieldValuesMap; 42 private PathTemplate pathTemplate; 43 private String fixedValue; 44 private final String project; 45 private final String constraint; 46 private final String folder; 47 private final String organization; 48 49 @Deprecated ConstraintName()50 protected ConstraintName() { 51 project = null; 52 constraint = null; 53 folder = null; 54 organization = null; 55 } 56 ConstraintName(Builder builder)57 private ConstraintName(Builder builder) { 58 project = Preconditions.checkNotNull(builder.getProject()); 59 constraint = Preconditions.checkNotNull(builder.getConstraint()); 60 folder = null; 61 organization = null; 62 pathTemplate = PROJECT_CONSTRAINT; 63 } 64 ConstraintName(FolderConstraintBuilder builder)65 private ConstraintName(FolderConstraintBuilder builder) { 66 folder = Preconditions.checkNotNull(builder.getFolder()); 67 constraint = Preconditions.checkNotNull(builder.getConstraint()); 68 project = null; 69 organization = null; 70 pathTemplate = FOLDER_CONSTRAINT; 71 } 72 ConstraintName(OrganizationConstraintBuilder builder)73 private ConstraintName(OrganizationConstraintBuilder builder) { 74 organization = Preconditions.checkNotNull(builder.getOrganization()); 75 constraint = Preconditions.checkNotNull(builder.getConstraint()); 76 project = null; 77 folder = null; 78 pathTemplate = ORGANIZATION_CONSTRAINT; 79 } 80 getProject()81 public String getProject() { 82 return project; 83 } 84 getConstraint()85 public String getConstraint() { 86 return constraint; 87 } 88 getFolder()89 public String getFolder() { 90 return folder; 91 } 92 getOrganization()93 public String getOrganization() { 94 return organization; 95 } 96 newBuilder()97 public static Builder newBuilder() { 98 return new Builder(); 99 } 100 101 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newProjectConstraintBuilder()102 public static Builder newProjectConstraintBuilder() { 103 return new Builder(); 104 } 105 106 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newFolderConstraintBuilder()107 public static FolderConstraintBuilder newFolderConstraintBuilder() { 108 return new FolderConstraintBuilder(); 109 } 110 111 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newOrganizationConstraintBuilder()112 public static OrganizationConstraintBuilder newOrganizationConstraintBuilder() { 113 return new OrganizationConstraintBuilder(); 114 } 115 toBuilder()116 public Builder toBuilder() { 117 return new Builder(this); 118 } 119 of(String project, String constraint)120 public static ConstraintName of(String project, String constraint) { 121 return newBuilder().setProject(project).setConstraint(constraint).build(); 122 } 123 124 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofProjectConstraintName(String project, String constraint)125 public static ConstraintName ofProjectConstraintName(String project, String constraint) { 126 return newBuilder().setProject(project).setConstraint(constraint).build(); 127 } 128 129 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofFolderConstraintName(String folder, String constraint)130 public static ConstraintName ofFolderConstraintName(String folder, String constraint) { 131 return newFolderConstraintBuilder().setFolder(folder).setConstraint(constraint).build(); 132 } 133 134 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofOrganizationConstraintName( String organization, String constraint)135 public static ConstraintName ofOrganizationConstraintName( 136 String organization, String constraint) { 137 return newOrganizationConstraintBuilder() 138 .setOrganization(organization) 139 .setConstraint(constraint) 140 .build(); 141 } 142 format(String project, String constraint)143 public static String format(String project, String constraint) { 144 return newBuilder().setProject(project).setConstraint(constraint).build().toString(); 145 } 146 147 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatProjectConstraintName(String project, String constraint)148 public static String formatProjectConstraintName(String project, String constraint) { 149 return newBuilder().setProject(project).setConstraint(constraint).build().toString(); 150 } 151 152 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatFolderConstraintName(String folder, String constraint)153 public static String formatFolderConstraintName(String folder, String constraint) { 154 return newFolderConstraintBuilder() 155 .setFolder(folder) 156 .setConstraint(constraint) 157 .build() 158 .toString(); 159 } 160 161 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatOrganizationConstraintName(String organization, String constraint)162 public static String formatOrganizationConstraintName(String organization, String constraint) { 163 return newOrganizationConstraintBuilder() 164 .setOrganization(organization) 165 .setConstraint(constraint) 166 .build() 167 .toString(); 168 } 169 parse(String formattedString)170 public static ConstraintName parse(String formattedString) { 171 if (formattedString.isEmpty()) { 172 return null; 173 } 174 if (PROJECT_CONSTRAINT.matches(formattedString)) { 175 Map<String, String> matchMap = PROJECT_CONSTRAINT.match(formattedString); 176 return ofProjectConstraintName(matchMap.get("project"), matchMap.get("constraint")); 177 } else if (FOLDER_CONSTRAINT.matches(formattedString)) { 178 Map<String, String> matchMap = FOLDER_CONSTRAINT.match(formattedString); 179 return ofFolderConstraintName(matchMap.get("folder"), matchMap.get("constraint")); 180 } else if (ORGANIZATION_CONSTRAINT.matches(formattedString)) { 181 Map<String, String> matchMap = ORGANIZATION_CONSTRAINT.match(formattedString); 182 return ofOrganizationConstraintName(matchMap.get("organization"), matchMap.get("constraint")); 183 } 184 throw new ValidationException("ConstraintName.parse: formattedString not in valid format"); 185 } 186 parseList(List<String> formattedStrings)187 public static List<ConstraintName> parseList(List<String> formattedStrings) { 188 List<ConstraintName> list = new ArrayList<>(formattedStrings.size()); 189 for (String formattedString : formattedStrings) { 190 list.add(parse(formattedString)); 191 } 192 return list; 193 } 194 toStringList(List<ConstraintName> values)195 public static List<String> toStringList(List<ConstraintName> values) { 196 List<String> list = new ArrayList<>(values.size()); 197 for (ConstraintName value : values) { 198 if (value == null) { 199 list.add(""); 200 } else { 201 list.add(value.toString()); 202 } 203 } 204 return list; 205 } 206 isParsableFrom(String formattedString)207 public static boolean isParsableFrom(String formattedString) { 208 return PROJECT_CONSTRAINT.matches(formattedString) 209 || FOLDER_CONSTRAINT.matches(formattedString) 210 || ORGANIZATION_CONSTRAINT.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 (constraint != null) { 223 fieldMapBuilder.put("constraint", constraint); 224 } 225 if (folder != null) { 226 fieldMapBuilder.put("folder", folder); 227 } 228 if (organization != null) { 229 fieldMapBuilder.put("organization", organization); 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 ConstraintName that = ((ConstraintName) o); 254 return Objects.equals(this.project, that.project) 255 && Objects.equals(this.constraint, that.constraint) 256 && Objects.equals(this.folder, that.folder) 257 && Objects.equals(this.organization, that.organization); 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(constraint); 271 h *= 1000003; 272 h ^= Objects.hashCode(folder); 273 h *= 1000003; 274 h ^= Objects.hashCode(organization); 275 return h; 276 } 277 278 /** Builder for projects/{project}/constraints/{constraint}. */ 279 public static class Builder { 280 private String project; 281 private String constraint; 282 Builder()283 protected Builder() {} 284 getProject()285 public String getProject() { 286 return project; 287 } 288 getConstraint()289 public String getConstraint() { 290 return constraint; 291 } 292 setProject(String project)293 public Builder setProject(String project) { 294 this.project = project; 295 return this; 296 } 297 setConstraint(String constraint)298 public Builder setConstraint(String constraint) { 299 this.constraint = constraint; 300 return this; 301 } 302 Builder(ConstraintName constraintName)303 private Builder(ConstraintName constraintName) { 304 Preconditions.checkArgument( 305 Objects.equals(constraintName.pathTemplate, PROJECT_CONSTRAINT), 306 "toBuilder is only supported when ConstraintName has the pattern of projects/{project}/constraints/{constraint}"); 307 this.project = constraintName.project; 308 this.constraint = constraintName.constraint; 309 } 310 build()311 public ConstraintName build() { 312 return new ConstraintName(this); 313 } 314 } 315 316 /** Builder for folders/{folder}/constraints/{constraint}. */ 317 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") 318 public static class FolderConstraintBuilder { 319 private String folder; 320 private String constraint; 321 FolderConstraintBuilder()322 protected FolderConstraintBuilder() {} 323 getFolder()324 public String getFolder() { 325 return folder; 326 } 327 getConstraint()328 public String getConstraint() { 329 return constraint; 330 } 331 setFolder(String folder)332 public FolderConstraintBuilder setFolder(String folder) { 333 this.folder = folder; 334 return this; 335 } 336 setConstraint(String constraint)337 public FolderConstraintBuilder setConstraint(String constraint) { 338 this.constraint = constraint; 339 return this; 340 } 341 build()342 public ConstraintName build() { 343 return new ConstraintName(this); 344 } 345 } 346 347 /** Builder for organizations/{organization}/constraints/{constraint}. */ 348 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") 349 public static class OrganizationConstraintBuilder { 350 private String organization; 351 private String constraint; 352 OrganizationConstraintBuilder()353 protected OrganizationConstraintBuilder() {} 354 getOrganization()355 public String getOrganization() { 356 return organization; 357 } 358 getConstraint()359 public String getConstraint() { 360 return constraint; 361 } 362 setOrganization(String organization)363 public OrganizationConstraintBuilder setOrganization(String organization) { 364 this.organization = organization; 365 return this; 366 } 367 setConstraint(String constraint)368 public OrganizationConstraintBuilder setConstraint(String constraint) { 369 this.constraint = constraint; 370 return this; 371 } 372 build()373 public ConstraintName build() { 374 return new ConstraintName(this); 375 } 376 } 377 } 378