• 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.cloud.kms.v1;
18 
19 import com.google.api.pathtemplate.PathTemplate;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableMap;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import javax.annotation.Generated;
27 
28 // AUTO-GENERATED DOCUMENTATION AND CLASS.
29 @Generated("by gapic-generator-java")
30 public class CryptoKeyName extends KeyName {
31   private static final PathTemplate PROJECT_LOCATION_KEY_RING_CRYPTO_KEY =
32       PathTemplate.createWithoutUrlEncoding(
33           "projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}");
34   private volatile Map<String, String> fieldValuesMap;
35   private final String project;
36   private final String location;
37   private final String keyRing;
38   private final String cryptoKey;
39 
40   @Deprecated
CryptoKeyName()41   protected CryptoKeyName() {
42     project = null;
43     location = null;
44     keyRing = null;
45     cryptoKey = null;
46   }
47 
CryptoKeyName(Builder builder)48   private CryptoKeyName(Builder builder) {
49     project = Preconditions.checkNotNull(builder.getProject());
50     location = Preconditions.checkNotNull(builder.getLocation());
51     keyRing = Preconditions.checkNotNull(builder.getKeyRing());
52     cryptoKey = Preconditions.checkNotNull(builder.getCryptoKey());
53   }
54 
getProject()55   public String getProject() {
56     return project;
57   }
58 
getLocation()59   public String getLocation() {
60     return location;
61   }
62 
getKeyRing()63   public String getKeyRing() {
64     return keyRing;
65   }
66 
getCryptoKey()67   public String getCryptoKey() {
68     return cryptoKey;
69   }
70 
newBuilder()71   public static Builder newBuilder() {
72     return new Builder();
73   }
74 
toBuilder()75   public Builder toBuilder() {
76     return new Builder(this);
77   }
78 
of( String project, String location, String keyRing, String cryptoKey)79   public static CryptoKeyName of(
80       String project, String location, String keyRing, String cryptoKey) {
81     return newBuilder()
82         .setProject(project)
83         .setLocation(location)
84         .setKeyRing(keyRing)
85         .setCryptoKey(cryptoKey)
86         .build();
87   }
88 
format(String project, String location, String keyRing, String cryptoKey)89   public static String format(String project, String location, String keyRing, String cryptoKey) {
90     return newBuilder()
91         .setProject(project)
92         .setLocation(location)
93         .setKeyRing(keyRing)
94         .setCryptoKey(cryptoKey)
95         .build()
96         .toString();
97   }
98 
parse(String formattedString)99   public static CryptoKeyName parse(String formattedString) {
100     if (formattedString.isEmpty()) {
101       return null;
102     }
103     Map<String, String> matchMap =
104         PROJECT_LOCATION_KEY_RING_CRYPTO_KEY.validatedMatch(
105             formattedString, "CryptoKeyName.parse: formattedString not in valid format");
106     return of(
107         matchMap.get("project"),
108         matchMap.get("location"),
109         matchMap.get("key_ring"),
110         matchMap.get("crypto_key"));
111   }
112 
parseList(List<String> formattedStrings)113   public static List<CryptoKeyName> parseList(List<String> formattedStrings) {
114     List<CryptoKeyName> list = new ArrayList<>(formattedStrings.size());
115     for (String formattedString : formattedStrings) {
116       list.add(parse(formattedString));
117     }
118     return list;
119   }
120 
toStringList(List<CryptoKeyName> values)121   public static List<String> toStringList(List<CryptoKeyName> values) {
122     List<String> list = new ArrayList<>(values.size());
123     for (CryptoKeyName value : values) {
124       if (value == null) {
125         list.add("");
126       } else {
127         list.add(value.toString());
128       }
129     }
130     return list;
131   }
132 
isParsableFrom(String formattedString)133   public static boolean isParsableFrom(String formattedString) {
134     return PROJECT_LOCATION_KEY_RING_CRYPTO_KEY.matches(formattedString);
135   }
136 
137   @Override
getFieldValuesMap()138   public Map<String, String> getFieldValuesMap() {
139     if (fieldValuesMap == null) {
140       synchronized (this) {
141         if (fieldValuesMap == null) {
142           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
143           if (project != null) {
144             fieldMapBuilder.put("project", project);
145           }
146           if (location != null) {
147             fieldMapBuilder.put("location", location);
148           }
149           if (keyRing != null) {
150             fieldMapBuilder.put("key_ring", keyRing);
151           }
152           if (cryptoKey != null) {
153             fieldMapBuilder.put("crypto_key", cryptoKey);
154           }
155           fieldValuesMap = fieldMapBuilder.build();
156         }
157       }
158     }
159     return fieldValuesMap;
160   }
161 
getFieldValue(String fieldName)162   public String getFieldValue(String fieldName) {
163     return getFieldValuesMap().get(fieldName);
164   }
165 
166   @Override
toString()167   public String toString() {
168     return PROJECT_LOCATION_KEY_RING_CRYPTO_KEY.instantiate(
169         "project", project, "location", location, "key_ring", keyRing, "crypto_key", cryptoKey);
170   }
171 
172   @Override
equals(Object o)173   public boolean equals(Object o) {
174     if (o == this) {
175       return true;
176     }
177     if (o != null || getClass() == o.getClass()) {
178       CryptoKeyName that = ((CryptoKeyName) o);
179       return Objects.equals(this.project, that.project)
180           && Objects.equals(this.location, that.location)
181           && Objects.equals(this.keyRing, that.keyRing)
182           && Objects.equals(this.cryptoKey, that.cryptoKey);
183     }
184     return false;
185   }
186 
187   @Override
hashCode()188   public int hashCode() {
189     int h = 1;
190     h *= 1000003;
191     h ^= Objects.hashCode(project);
192     h *= 1000003;
193     h ^= Objects.hashCode(location);
194     h *= 1000003;
195     h ^= Objects.hashCode(keyRing);
196     h *= 1000003;
197     h ^= Objects.hashCode(cryptoKey);
198     return h;
199   }
200 
201   /**
202    * Builder for
203    * projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}.
204    */
205   public static class Builder {
206     private String project;
207     private String location;
208     private String keyRing;
209     private String cryptoKey;
210 
Builder()211     protected Builder() {}
212 
getProject()213     public String getProject() {
214       return project;
215     }
216 
getLocation()217     public String getLocation() {
218       return location;
219     }
220 
getKeyRing()221     public String getKeyRing() {
222       return keyRing;
223     }
224 
getCryptoKey()225     public String getCryptoKey() {
226       return cryptoKey;
227     }
228 
setProject(String project)229     public Builder setProject(String project) {
230       this.project = project;
231       return this;
232     }
233 
setLocation(String location)234     public Builder setLocation(String location) {
235       this.location = location;
236       return this;
237     }
238 
setKeyRing(String keyRing)239     public Builder setKeyRing(String keyRing) {
240       this.keyRing = keyRing;
241       return this;
242     }
243 
setCryptoKey(String cryptoKey)244     public Builder setCryptoKey(String cryptoKey) {
245       this.cryptoKey = cryptoKey;
246       return this;
247     }
248 
Builder(CryptoKeyName cryptoKeyName)249     private Builder(CryptoKeyName cryptoKeyName) {
250       this.project = cryptoKeyName.project;
251       this.location = cryptoKeyName.location;
252       this.keyRing = cryptoKeyName.keyRing;
253       this.cryptoKey = cryptoKeyName.cryptoKey;
254     }
255 
build()256     public CryptoKeyName build() {
257       return new CryptoKeyName(this);
258     }
259   }
260 }
261