• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import java.util.Collections;
22 import java.util.List;
23 
24 @AutoValue
25 public abstract class IfStatement implements Statement {
conditionExpr()26   public abstract Expr conditionExpr();
27 
body()28   public abstract ImmutableList<Statement> body();
29 
elseIfs()30   public abstract ImmutableMap<Expr, List<Statement>> elseIfs(); // Optional.
31 
elseBody()32   public abstract ImmutableList<Statement> elseBody(); // Optional.
33 
34   @Override
accept(AstNodeVisitor visitor)35   public void accept(AstNodeVisitor visitor) {
36     visitor.visit(this);
37   }
38 
builder()39   public static Builder builder() {
40     return new AutoValue_IfStatement.Builder()
41         .setElseIfs(ImmutableMap.of())
42         .setElseBody(Collections.emptyList());
43   }
44 
45   @AutoValue.Builder
46   public abstract static class Builder {
setConditionExpr(Expr expr)47     public abstract Builder setConditionExpr(Expr expr);
48 
setBody(List<Statement> body)49     public abstract Builder setBody(List<Statement> body);
50 
setElseBody(List<Statement> elseBody)51     public abstract Builder setElseBody(List<Statement> elseBody);
52 
addElseIf(Expr conditionExpr, List<Statement> body)53     public Builder addElseIf(Expr conditionExpr, List<Statement> body) {
54       elseIfsBuilder().put(conditionExpr, body);
55       return this;
56     }
57 
elseIfsBuilder()58     abstract ImmutableMap.Builder<Expr, List<Statement>> elseIfsBuilder();
59 
setElseIfs(ImmutableMap<Expr, List<Statement>> elseIfBlocks)60     abstract Builder setElseIfs(ImmutableMap<Expr, List<Statement>> elseIfBlocks);
61 
autoBuild()62     abstract IfStatement autoBuild();
63 
build()64     public IfStatement build() {
65       IfStatement ifStatement = autoBuild();
66       NodeValidator.checkNoNullElements(ifStatement.body(), "body", "if-statement");
67       NodeValidator.checkNoNullElements(ifStatement.elseIfs(), "else-ifs", "if-statement");
68       NodeValidator.checkNoNullElements(ifStatement.elseBody(), "else-body", "if-statement");
69 
70       Preconditions.checkState(
71           ifStatement.conditionExpr().type().equals(TypeNode.BOOLEAN),
72           "If-condition must be a boolean-typed expression");
73       return ifStatement;
74     }
75   }
76 }
77