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.engine.ast.TypeNode; 18 import com.google.auto.value.AutoValue; 19 import com.google.common.collect.ImmutableList; 20 import java.util.List; 21 22 @AutoValue 23 public abstract class MethodArgument implements Comparable<MethodArgument> { 24 // The method argument name. name()25 public abstract String name(); 26 27 // The type. This can be different from the associated field (e.g. for resource references). type()28 public abstract TypeNode type(); 29 30 // Additional metadata. field()31 public abstract Field field(); 32 33 // Records the path of nested fields in descending order, excluding type() (which would have 34 // appeared as the last element). nestedFields()35 public abstract ImmutableList<Field> nestedFields(); 36 37 // Returns true if this is a resource name helper method argument. isResourceNameHelper()38 public abstract boolean isResourceNameHelper(); 39 40 @Override compareTo(MethodArgument other)41 public int compareTo(MethodArgument other) { 42 int compareVal = type().compareTo(other.type()); 43 if (compareVal == 0) { 44 compareVal = name().compareTo(other.name()); 45 } 46 return compareVal; 47 } 48 49 @Override equals(Object o)50 public boolean equals(Object o) { 51 if (!(o instanceof MethodArgument)) { 52 return false; 53 } 54 55 MethodArgument other = (MethodArgument) o; 56 return name().equals(other.name()) 57 && type().equals(other.type()) 58 && field().equals(other.field()) & nestedFields().equals(other.nestedFields()) 59 && isResourceNameHelper() == other.isResourceNameHelper(); 60 } 61 62 @Override hashCode()63 public int hashCode() { 64 return 17 * name().hashCode() 65 + 19 * type().hashCode() 66 + 23 * field().hashCode() 67 + 29 * nestedFields().hashCode() 68 + (isResourceNameHelper() ? 1 : 0) * 31; 69 } 70 builder()71 public static Builder builder() { 72 return new AutoValue_MethodArgument.Builder() 73 .setNestedFields(ImmutableList.of()) 74 .setIsResourceNameHelper(false); 75 } 76 77 @AutoValue.Builder 78 public abstract static class Builder { setName(String name)79 public abstract Builder setName(String name); 80 setType(TypeNode type)81 public abstract Builder setType(TypeNode type); 82 setField(Field field)83 public abstract Builder setField(Field field); 84 setNestedFields(List<Field> nestedFields)85 public abstract Builder setNestedFields(List<Field> nestedFields); 86 setIsResourceNameHelper(boolean isResourceNameHelper)87 public abstract Builder setIsResourceNameHelper(boolean isResourceNameHelper); 88 build()89 public abstract MethodArgument build(); 90 } 91 } 92