// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.model;

import com.google.api.generator.engine.ast.TypeNode;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.util.List;

@AutoValue
public abstract class MethodArgument implements Comparable<MethodArgument> {
  // The method argument name.
  public abstract String name();

  // The type. This can be different from the associated field (e.g. for resource references).
  public abstract TypeNode type();

  // Additional metadata.
  public abstract Field field();

  // Records the path of nested fields in descending order, excluding type() (which would have
  // appeared as the last element).
  public abstract ImmutableList<Field> nestedFields();

  // Returns true if this is a resource name helper method argument.
  public abstract boolean isResourceNameHelper();

  @Override
  public int compareTo(MethodArgument other) {
    int compareVal = type().compareTo(other.type());
    if (compareVal == 0) {
      compareVal = name().compareTo(other.name());
    }
    return compareVal;
  }

  public static Builder builder() {
    return new AutoValue_MethodArgument.Builder()
        .setNestedFields(ImmutableList.of())
        .setIsResourceNameHelper(false);
  }

  @AutoValue.Builder
  public abstract static class Builder {
    public abstract Builder setName(String name);

    public abstract Builder setType(TypeNode type);

    public abstract Builder setField(Field field);

    public abstract Builder setNestedFields(List<Field> nestedFields);

    public abstract Builder setIsResourceNameHelper(boolean isResourceNameHelper);

    public abstract MethodArgument build();
  }
}

@@ -1669,16 +1669,651 @@
 l;%0A  %7D%0A%0A
+  @Override%0A  public boolean equals(Object o) %7B%0A    if (!(o instanceof MethodArgument)) %7B%0A      return false;%0A    %7D%0A%0A    MethodArgument other = (MethodArgument) o;%0A    return name().equals(other.name())%0A        && type().equals(other.type())%0A        && field().equals(other.field()) & nestedFields().equals(other.nestedFields())%0A        && isResourceNameHelper() == other.isResourceNameHelper();%0A  %7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    return 17 * name().hashCode()%0A        + 19 * type().hashCode()%0A        + 23 * field().hashCode()%0A        + 29 * nestedFields().hashCode()%0A        + (isResourceNameHelper() ? 1 : 0) * 31;%0A  %7D%0A%0A
   public

add equals() and hashCode() for MethodArgument (#623)