• 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.privacy.dlp.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 DlpContentName implements ResourceName {
34   private static final PathTemplate PROJECT =
35       PathTemplate.createWithoutUrlEncoding("projects/{project}/dlpContent");
36   private static final PathTemplate PROJECT_LOCATION =
37       PathTemplate.createWithoutUrlEncoding("projects/{project}/locations/{location}/dlpContent");
38   private volatile Map<String, String> fieldValuesMap;
39   private PathTemplate pathTemplate;
40   private String fixedValue;
41   private final String project;
42   private final String location;
43 
44   @Deprecated
DlpContentName()45   protected DlpContentName() {
46     project = null;
47     location = null;
48   }
49 
DlpContentName(Builder builder)50   private DlpContentName(Builder builder) {
51     project = Preconditions.checkNotNull(builder.getProject());
52     location = null;
53     pathTemplate = PROJECT;
54   }
55 
DlpContentName(ProjectLocationBuilder builder)56   private DlpContentName(ProjectLocationBuilder builder) {
57     project = Preconditions.checkNotNull(builder.getProject());
58     location = Preconditions.checkNotNull(builder.getLocation());
59     pathTemplate = PROJECT_LOCATION;
60   }
61 
getProject()62   public String getProject() {
63     return project;
64   }
65 
getLocation()66   public String getLocation() {
67     return location;
68   }
69 
newBuilder()70   public static Builder newBuilder() {
71     return new Builder();
72   }
73 
74   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newProjectBuilder()75   public static Builder newProjectBuilder() {
76     return new Builder();
77   }
78 
79   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
newProjectLocationBuilder()80   public static ProjectLocationBuilder newProjectLocationBuilder() {
81     return new ProjectLocationBuilder();
82   }
83 
toBuilder()84   public Builder toBuilder() {
85     return new Builder(this);
86   }
87 
of(String project)88   public static DlpContentName of(String project) {
89     return newBuilder().setProject(project).build();
90   }
91 
92   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofProjectName(String project)93   public static DlpContentName ofProjectName(String project) {
94     return newBuilder().setProject(project).build();
95   }
96 
97   @BetaApi("The static create methods are not stable yet and may be changed in the future.")
ofProjectLocationName(String project, String location)98   public static DlpContentName ofProjectLocationName(String project, String location) {
99     return newProjectLocationBuilder().setProject(project).setLocation(location).build();
100   }
101 
format(String project)102   public static String format(String project) {
103     return newBuilder().setProject(project).build().toString();
104   }
105 
106   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatProjectName(String project)107   public static String formatProjectName(String project) {
108     return newBuilder().setProject(project).build().toString();
109   }
110 
111   @BetaApi("The static format methods are not stable yet and may be changed in the future.")
formatProjectLocationName(String project, String location)112   public static String formatProjectLocationName(String project, String location) {
113     return newProjectLocationBuilder().setProject(project).setLocation(location).build().toString();
114   }
115 
parse(String formattedString)116   public static DlpContentName parse(String formattedString) {
117     if (formattedString.isEmpty()) {
118       return null;
119     }
120     if (PROJECT.matches(formattedString)) {
121       Map<String, String> matchMap = PROJECT.match(formattedString);
122       return ofProjectName(matchMap.get("project"));
123     } else if (PROJECT_LOCATION.matches(formattedString)) {
124       Map<String, String> matchMap = PROJECT_LOCATION.match(formattedString);
125       return ofProjectLocationName(matchMap.get("project"), matchMap.get("location"));
126     }
127     throw new ValidationException("DlpContentName.parse: formattedString not in valid format");
128   }
129 
parseList(List<String> formattedStrings)130   public static List<DlpContentName> parseList(List<String> formattedStrings) {
131     List<DlpContentName> list = new ArrayList<>(formattedStrings.size());
132     for (String formattedString : formattedStrings) {
133       list.add(parse(formattedString));
134     }
135     return list;
136   }
137 
toStringList(List<DlpContentName> values)138   public static List<String> toStringList(List<DlpContentName> values) {
139     List<String> list = new ArrayList<>(values.size());
140     for (DlpContentName value : values) {
141       if (value == null) {
142         list.add("");
143       } else {
144         list.add(value.toString());
145       }
146     }
147     return list;
148   }
149 
isParsableFrom(String formattedString)150   public static boolean isParsableFrom(String formattedString) {
151     return PROJECT.matches(formattedString) || PROJECT_LOCATION.matches(formattedString);
152   }
153 
154   @Override
getFieldValuesMap()155   public Map<String, String> getFieldValuesMap() {
156     if (fieldValuesMap == null) {
157       synchronized (this) {
158         if (fieldValuesMap == null) {
159           ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
160           if (project != null) {
161             fieldMapBuilder.put("project", project);
162           }
163           if (location != null) {
164             fieldMapBuilder.put("location", location);
165           }
166           fieldValuesMap = fieldMapBuilder.build();
167         }
168       }
169     }
170     return fieldValuesMap;
171   }
172 
getFieldValue(String fieldName)173   public String getFieldValue(String fieldName) {
174     return getFieldValuesMap().get(fieldName);
175   }
176 
177   @Override
toString()178   public String toString() {
179     return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap());
180   }
181 
182   @Override
equals(Object o)183   public boolean equals(Object o) {
184     if (o == this) {
185       return true;
186     }
187     if (o != null || getClass() == o.getClass()) {
188       DlpContentName that = ((DlpContentName) o);
189       return Objects.equals(this.project, that.project)
190           && Objects.equals(this.location, that.location);
191     }
192     return false;
193   }
194 
195   @Override
hashCode()196   public int hashCode() {
197     int h = 1;
198     h *= 1000003;
199     h ^= Objects.hashCode(fixedValue);
200     h *= 1000003;
201     h ^= Objects.hashCode(project);
202     h *= 1000003;
203     h ^= Objects.hashCode(location);
204     return h;
205   }
206 
207   /** Builder for projects/{project}/dlpContent. */
208   public static class Builder {
209     private String project;
210 
Builder()211     protected Builder() {}
212 
getProject()213     public String getProject() {
214       return project;
215     }
216 
setProject(String project)217     public Builder setProject(String project) {
218       this.project = project;
219       return this;
220     }
221 
Builder(DlpContentName dlpContentName)222     private Builder(DlpContentName dlpContentName) {
223       Preconditions.checkArgument(
224           Objects.equals(dlpContentName.pathTemplate, PROJECT),
225           "toBuilder is only supported when DlpContentName has the pattern of projects/{project}/dlpContent");
226       this.project = dlpContentName.project;
227     }
228 
build()229     public DlpContentName build() {
230       return new DlpContentName(this);
231     }
232   }
233 
234   /** Builder for projects/{project}/locations/{location}/dlpContent. */
235   @BetaApi("The per-pattern Builders are not stable yet and may be changed in the future.")
236   public static class ProjectLocationBuilder {
237     private String project;
238     private String location;
239 
ProjectLocationBuilder()240     protected ProjectLocationBuilder() {}
241 
getProject()242     public String getProject() {
243       return project;
244     }
245 
getLocation()246     public String getLocation() {
247       return location;
248     }
249 
setProject(String project)250     public ProjectLocationBuilder setProject(String project) {
251       this.project = project;
252       return this;
253     }
254 
setLocation(String location)255     public ProjectLocationBuilder setLocation(String location) {
256       this.location = location;
257       return this;
258     }
259 
build()260     public DlpContentName build() {
261       return new DlpContentName(this);
262     }
263   }
264 }
265