1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.api.generator.gapic.model;
16 
17 import com.google.api.generator.gapic.utils.ResourceNameConstants;
18 import com.google.auto.value.AutoValue;
19 
20 @AutoValue
21 public abstract class ResourceReference {
22 
resourceTypeString()23   public abstract String resourceTypeString();
24 
isChildType()25   public abstract boolean isChildType();
26 
isOnlyWildcard()27   public boolean isOnlyWildcard() {
28     return resourceTypeString().equals(ResourceNameConstants.WILDCARD_PATTERN);
29   }
30 
31   @Override
equals(Object o)32   public boolean equals(Object o) {
33     if (!(o instanceof ResourceReference)) {
34       return false;
35     }
36 
37     ResourceReference other = (ResourceReference) o;
38     return resourceTypeString().equals(other.resourceTypeString())
39         && isChildType() == other.isChildType()
40         && isOnlyWildcard() == other.isOnlyWildcard();
41   }
42 
43   @Override
hashCode()44   public int hashCode() {
45     return 17 * resourceTypeString().hashCode()
46         + (isChildType() ? 1 : 0) * 19
47         + (isOnlyWildcard() ? 1 : 0) * 31;
48   }
49 
withType(String resourceTypeString)50   public static ResourceReference withType(String resourceTypeString) {
51     return builder().setResourceTypeString(resourceTypeString).setIsChildType(false).build();
52   }
53 
withChildType(String resourceTypeString)54   public static ResourceReference withChildType(String resourceTypeString) {
55     return builder().setResourceTypeString(resourceTypeString).setIsChildType(true).build();
56   }
57 
58   // Private.
builder()59   static Builder builder() {
60     return new AutoValue_ResourceReference.Builder();
61   }
62 
63   // Private.
64   @AutoValue.Builder
65   abstract static class Builder {
setResourceTypeString(String resourceTypeString)66     abstract Builder setResourceTypeString(String resourceTypeString);
67 
setIsChildType(boolean isChildType)68     abstract Builder setIsChildType(boolean isChildType);
69 
build()70     abstract ResourceReference build();
71   }
72 }
73