// 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.engine.ast;

import java.util.Objects;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;

public class Type implements AstNode {
  public enum TypeKind {
    BYTE,
    SHORT,
    INT,
    LONG,
    FLOAT,
    DOUBLE,
    BOOLEAN,
    CHAR,
    OBJECT
  }

  private final TypeKind typeKind;
  private final boolean isArray;
  @Nullable private final Reference reference;

  private Type(@NotNull TypeKind typeKind, @Nullable Reference reference, boolean isArray) {
    this.typeKind = typeKind;
    this.reference = reference;
    this.isArray = isArray;
  }

  // TODO(miraleung): More type creation helpers to come...
  public static Type createReferenceType(Reference reference) {
    return new Type(TypeKind.OBJECT, reference, false);
  }

  public static Type createReferenceArrayType(Reference reference) {
    return new Type(TypeKind.OBJECT, reference, true);
  }

  public static Type createIntType() {
    return createPrimitiveType(TypeKind.INT);
  }

  public static Type createByteArrayType() {
    return createPrimitiveArrayType(TypeKind.BYTE);
  }

  public TypeKind typeKind() {
    return typeKind;
  }

  public Reference reference() {
    return reference;
  }

  public boolean isArray() {
    return isArray;
  }

  public boolean isPrimitiveType() {
    return isPrimitiveType(typeKind);
  }

  @Override
  public void accept(AstNodeVisitor visitor) {
    visitor.visit(this);
  }

  // Java overrides.
  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Type)) {
      return false;
    }

    Type type = (Type) o;
    return typeKind.equals(type.typeKind)
        && (isArray == type.isArray)
        && Objects.equals(reference, type.reference);
  }

  private static Type createPrimitiveType(TypeKind typeKind) {
    if (!isPrimitiveType(typeKind)) {
      throw new IllegalArgumentException("Object is not a primitive type.");
    }
    return new Type(typeKind, null, false);
  }

  private static Type createPrimitiveArrayType(TypeKind typeKind) {
    if (typeKind.equals(TypeKind.OBJECT)) {
      throw new IllegalArgumentException("Object is not a primitive type.");
    }
    return new Type(typeKind, null, true);
  }

  private static boolean isPrimitiveType(TypeKind typeKind) {
    return !typeKind.equals(TypeKind.OBJECT);
  }
}

@@ -628,16 +628,56 @@
 e.ast;%0A%0A
+import com.google.auto.value.AutoValue;%0A
 import j
@@ -732,60 +732,35 @@
 le;%0A
-import javax.validation.constraints.NotNull;%0A%0Apublic
+%0A@AutoValue%0Apublic abstract
  cla
@@ -918,28 +918,30 @@
   %7D%0A%0A  p
-rivate final
+ublic abstract
  TypeKin
@@ -954,25 +954,30 @@
 Kind
-;%0A  private final
+();%0A%0A  public abstract
  boo
@@ -988,17 +988,20 @@
  isArray
-;
+();%0A
 %0A  @Null
@@ -1008,22 +1008,26 @@
 able
- private final
+%0A  public abstract
  Ref
@@ -1046,85 +1046,275 @@
 ence
+()
 ;%0A%0A  p
-rivate Type(@NotNull TypeKind typeKind, @Nullable Reference reference, 
+ublic static Builder builder() %7B%0A    return new AutoValue_Type.Builder().setIsArray(false);%0A  %7D%0A%0A  @AutoValue.Builder%0A  public abstract static class Builder %7B%0A    public abstract Builder setTypeKind(TypeKind typeKind);%0A%0A    public abstract Builder setIsArray(
 bool
@@ -1329,59 +1329,61 @@
 ray)
- %7B
+;%0A
 %0A    
-this.typeKind = typeKind;%0A    this.r
+public abstract Builder setReference(R
 eference
  = r
@@ -1378,18 +1378,16 @@
 eference
- =
  referen
@@ -1392,36 +1392,44 @@
 ence
-;
+);%0A
 %0A    
-this.isArray = isArray
+public abstract Type build()
 ;%0A  
@@ -1562,32 +1562,50 @@
 %0A    return 
-new 
 Type
+.builder().setTypeKind
 (TypeKind.OB
@@ -1600,34 +1600,47 @@
 (TypeKind.OBJECT
-, 
+).setReference(
 reference, false
@@ -1632,23 +1632,24 @@
 eference
-, false
+).build(
 );%0A  %7D%0A%0A
@@ -1732,49 +1732,135 @@
 urn 
-new Type(TypeKind.OBJECT, reference, true
+Type.builder()%0A        .setTypeKind(TypeKind.OBJECT)%0A        .setReference(reference)%0A        .setIsArray(true)%0A        .build(
 );%0A 
@@ -2059,179 +2059,8 @@
  %7D%0A%0A
-  public TypeKind typeKind() %7B%0A    return typeKind;%0A  %7D%0A%0A  public Reference reference() %7B%0A    return reference;%0A  %7D%0A%0A  public boolean isArray() %7B%0A    return isArray;%0A  %7D%0A%0A
   pu
@@ -2127,16 +2127,18 @@
 typeKind
+()
 );%0A  %7D%0A%0A
@@ -2391,32 +2391,34 @@
  return typeKind
+()
 .equals(type.typ
@@ -2422,16 +2422,18 @@
 typeKind
+()
 )%0A      
@@ -2445,16 +2445,18 @@
 (isArray
+()
  == type
@@ -2463,16 +2463,18 @@
 .isArray
+()
 )%0A      
@@ -2502,16 +2502,18 @@
 eference
+()
 , type.r
@@ -2520,16 +2520,18 @@
 eference
+()
 );%0A  %7D%0A%0A
@@ -2729,38 +2729,51 @@
 urn 
-new Type(typeKind, null, false
+Type.builder().setTypeKind(typeKind).build(
 );%0A 
@@ -2986,37 +2986,68 @@
 urn 
-new Type(typeKind, null, true
+Type.builder().setTypeKind(typeKind).setIsArray(true).build(
 );%0A 

convert Type to AutoValue