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.osconfig.v1; 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 InstanceName implements ResourceName { 34 private static final PathTemplate PROJECT_ZONE_INSTANCE = 35 PathTemplate.createWithoutUrlEncoding("projects/{project}/zones/{zone}/instances/{instance}"); 36 private static final PathTemplate PROJECT_LOCATION_INSTANCE = 37 PathTemplate.createWithoutUrlEncoding( 38 "projects/{project}/locations/{location}/instances/{instance}"); 39 private volatile Map<String, String> fieldValuesMap; 40 private PathTemplate pathTemplate; 41 private String fixedValue; 42 private final String project; 43 private final String zone; 44 private final String instance; 45 private final String location; 46 47 @Deprecated InstanceName()48 protected InstanceName() { 49 project = null; 50 zone = null; 51 instance = null; 52 location = null; 53 } 54 InstanceName(Builder builder)55 private InstanceName(Builder builder) { 56 project = Preconditions.checkNotNull(builder.getProject()); 57 zone = Preconditions.checkNotNull(builder.getZone()); 58 instance = Preconditions.checkNotNull(builder.getInstance()); 59 location = null; 60 pathTemplate = PROJECT_ZONE_INSTANCE; 61 } 62 InstanceName(ProjectLocationInstanceBuilder builder)63 private InstanceName(ProjectLocationInstanceBuilder builder) { 64 project = Preconditions.checkNotNull(builder.getProject()); 65 location = Preconditions.checkNotNull(builder.getLocation()); 66 instance = Preconditions.checkNotNull(builder.getInstance()); 67 zone = null; 68 pathTemplate = PROJECT_LOCATION_INSTANCE; 69 } 70 getProject()71 public String getProject() { 72 return project; 73 } 74 getZone()75 public String getZone() { 76 return zone; 77 } 78 getInstance()79 public String getInstance() { 80 return instance; 81 } 82 getLocation()83 public String getLocation() { 84 return location; 85 } 86 newBuilder()87 public static Builder newBuilder() { 88 return new Builder(); 89 } 90 91 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newProjectZoneInstanceBuilder()92 public static Builder newProjectZoneInstanceBuilder() { 93 return new Builder(); 94 } 95 96 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") newProjectLocationInstanceBuilder()97 public static ProjectLocationInstanceBuilder newProjectLocationInstanceBuilder() { 98 return new ProjectLocationInstanceBuilder(); 99 } 100 toBuilder()101 public Builder toBuilder() { 102 return new Builder(this); 103 } 104 of(String project, String zone, String instance)105 public static InstanceName of(String project, String zone, String instance) { 106 return newBuilder().setProject(project).setZone(zone).setInstance(instance).build(); 107 } 108 109 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofProjectZoneInstanceName( String project, String zone, String instance)110 public static InstanceName ofProjectZoneInstanceName( 111 String project, String zone, String instance) { 112 return newBuilder().setProject(project).setZone(zone).setInstance(instance).build(); 113 } 114 115 @BetaApi("The static create methods are not stable yet and may be changed in the future.") ofProjectLocationInstanceName( String project, String location, String instance)116 public static InstanceName ofProjectLocationInstanceName( 117 String project, String location, String instance) { 118 return newProjectLocationInstanceBuilder() 119 .setProject(project) 120 .setLocation(location) 121 .setInstance(instance) 122 .build(); 123 } 124 format(String project, String zone, String instance)125 public static String format(String project, String zone, String instance) { 126 return newBuilder().setProject(project).setZone(zone).setInstance(instance).build().toString(); 127 } 128 129 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatProjectZoneInstanceName(String project, String zone, String instance)130 public static String formatProjectZoneInstanceName(String project, String zone, String instance) { 131 return newBuilder().setProject(project).setZone(zone).setInstance(instance).build().toString(); 132 } 133 134 @BetaApi("The static format methods are not stable yet and may be changed in the future.") formatProjectLocationInstanceName( String project, String location, String instance)135 public static String formatProjectLocationInstanceName( 136 String project, String location, String instance) { 137 return newProjectLocationInstanceBuilder() 138 .setProject(project) 139 .setLocation(location) 140 .setInstance(instance) 141 .build() 142 .toString(); 143 } 144 parse(String formattedString)145 public static InstanceName parse(String formattedString) { 146 if (formattedString.isEmpty()) { 147 return null; 148 } 149 if (PROJECT_ZONE_INSTANCE.matches(formattedString)) { 150 Map<String, String> matchMap = PROJECT_ZONE_INSTANCE.match(formattedString); 151 return ofProjectZoneInstanceName( 152 matchMap.get("project"), matchMap.get("zone"), matchMap.get("instance")); 153 } else if (PROJECT_LOCATION_INSTANCE.matches(formattedString)) { 154 Map<String, String> matchMap = PROJECT_LOCATION_INSTANCE.match(formattedString); 155 return ofProjectLocationInstanceName( 156 matchMap.get("project"), matchMap.get("location"), matchMap.get("instance")); 157 } 158 throw new ValidationException("InstanceName.parse: formattedString not in valid format"); 159 } 160 parseList(List<String> formattedStrings)161 public static List<InstanceName> parseList(List<String> formattedStrings) { 162 List<InstanceName> list = new ArrayList<>(formattedStrings.size()); 163 for (String formattedString : formattedStrings) { 164 list.add(parse(formattedString)); 165 } 166 return list; 167 } 168 toStringList(List<InstanceName> values)169 public static List<String> toStringList(List<InstanceName> values) { 170 List<String> list = new ArrayList<>(values.size()); 171 for (InstanceName value : values) { 172 if (value == null) { 173 list.add(""); 174 } else { 175 list.add(value.toString()); 176 } 177 } 178 return list; 179 } 180 isParsableFrom(String formattedString)181 public static boolean isParsableFrom(String formattedString) { 182 return PROJECT_ZONE_INSTANCE.matches(formattedString) 183 || PROJECT_LOCATION_INSTANCE.matches(formattedString); 184 } 185 186 @Override getFieldValuesMap()187 public Map<String, String> getFieldValuesMap() { 188 if (fieldValuesMap == null) { 189 synchronized (this) { 190 if (fieldValuesMap == null) { 191 ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder(); 192 if (project != null) { 193 fieldMapBuilder.put("project", project); 194 } 195 if (zone != null) { 196 fieldMapBuilder.put("zone", zone); 197 } 198 if (instance != null) { 199 fieldMapBuilder.put("instance", instance); 200 } 201 if (location != null) { 202 fieldMapBuilder.put("location", location); 203 } 204 fieldValuesMap = fieldMapBuilder.build(); 205 } 206 } 207 } 208 return fieldValuesMap; 209 } 210 getFieldValue(String fieldName)211 public String getFieldValue(String fieldName) { 212 return getFieldValuesMap().get(fieldName); 213 } 214 215 @Override toString()216 public String toString() { 217 return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap()); 218 } 219 220 @Override equals(Object o)221 public boolean equals(Object o) { 222 if (o == this) { 223 return true; 224 } 225 if (o != null || getClass() == o.getClass()) { 226 InstanceName that = ((InstanceName) o); 227 return Objects.equals(this.project, that.project) 228 && Objects.equals(this.zone, that.zone) 229 && Objects.equals(this.instance, that.instance) 230 && Objects.equals(this.location, that.location); 231 } 232 return false; 233 } 234 235 @Override hashCode()236 public int hashCode() { 237 int h = 1; 238 h *= 1000003; 239 h ^= Objects.hashCode(fixedValue); 240 h *= 1000003; 241 h ^= Objects.hashCode(project); 242 h *= 1000003; 243 h ^= Objects.hashCode(zone); 244 h *= 1000003; 245 h ^= Objects.hashCode(instance); 246 h *= 1000003; 247 h ^= Objects.hashCode(location); 248 return h; 249 } 250 251 /** Builder for projects/{project}/zones/{zone}/instances/{instance}. */ 252 public static class Builder { 253 private String project; 254 private String zone; 255 private String instance; 256 Builder()257 protected Builder() {} 258 getProject()259 public String getProject() { 260 return project; 261 } 262 getZone()263 public String getZone() { 264 return zone; 265 } 266 getInstance()267 public String getInstance() { 268 return instance; 269 } 270 setProject(String project)271 public Builder setProject(String project) { 272 this.project = project; 273 return this; 274 } 275 setZone(String zone)276 public Builder setZone(String zone) { 277 this.zone = zone; 278 return this; 279 } 280 setInstance(String instance)281 public Builder setInstance(String instance) { 282 this.instance = instance; 283 return this; 284 } 285 Builder(InstanceName instanceName)286 private Builder(InstanceName instanceName) { 287 Preconditions.checkArgument( 288 Objects.equals(instanceName.pathTemplate, PROJECT_ZONE_INSTANCE), 289 "toBuilder is only supported when InstanceName has the pattern of projects/{project}/zones/{zone}/instances/{instance}"); 290 this.project = instanceName.project; 291 this.zone = instanceName.zone; 292 this.instance = instanceName.instance; 293 } 294 build()295 public InstanceName build() { 296 return new InstanceName(this); 297 } 298 } 299 300 /** Builder for projects/{project}/locations/{location}/instances/{instance}. */ 301 @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.") 302 public static class ProjectLocationInstanceBuilder { 303 private String project; 304 private String location; 305 private String instance; 306 ProjectLocationInstanceBuilder()307 protected ProjectLocationInstanceBuilder() {} 308 getProject()309 public String getProject() { 310 return project; 311 } 312 getLocation()313 public String getLocation() { 314 return location; 315 } 316 getInstance()317 public String getInstance() { 318 return instance; 319 } 320 setProject(String project)321 public ProjectLocationInstanceBuilder setProject(String project) { 322 this.project = project; 323 return this; 324 } 325 setLocation(String location)326 public ProjectLocationInstanceBuilder setLocation(String location) { 327 this.location = location; 328 return this; 329 } 330 setInstance(String instance)331 public ProjectLocationInstanceBuilder setInstance(String instance) { 332 this.instance = instance; 333 return this; 334 } 335 build()336 public InstanceName build() { 337 return new InstanceName(this); 338 } 339 } 340 } 341