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.engine.ast;
16 
17 import com.google.auto.value.AutoValue;
18 import com.google.common.base.Preconditions;
19 
20 @AutoValue
21 public abstract class EnumRefExpr implements Expr {
identifier()22   public abstract IdentifierNode identifier();
23 
24   @Override
type()25   public abstract TypeNode type();
26 
27   @Override
accept(AstNodeVisitor visitor)28   public void accept(AstNodeVisitor visitor) {
29     visitor.visit(this);
30   }
31 
name()32   abstract String name();
33 
builder()34   public static Builder builder() {
35     return new AutoValue_EnumRefExpr.Builder();
36   }
37 
38   @AutoValue.Builder
39   public abstract static class Builder {
setType(TypeNode type)40     public abstract Builder setType(TypeNode type);
41 
setName(String name)42     public abstract Builder setName(String name);
43 
name()44     abstract String name();
45 
setIdentifier(IdentifierNode identifier)46     abstract Builder setIdentifier(IdentifierNode identifier);
47 
autoBuild()48     abstract EnumRefExpr autoBuild();
49 
build()50     public EnumRefExpr build() {
51       setIdentifier(IdentifierNode.builder().setName(name()).build());
52       EnumRefExpr enumRefExpr = autoBuild();
53       Preconditions.checkState(
54           TypeNode.isReferenceType(enumRefExpr.type()),
55           "Enum reference type must be a reference type");
56       return enumRefExpr;
57     }
58   }
59 }
60