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