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 java.util.List; 21 22 @AutoValue 23 public abstract class ForStatement implements Statement { localVariableExpr()24 public abstract VariableExpr localVariableExpr(); 25 collectionExpr()26 public abstract Expr collectionExpr(); 27 body()28 public abstract ImmutableList<Statement> body(); 29 30 @Override accept(AstNodeVisitor visitor)31 public void accept(AstNodeVisitor visitor) { 32 visitor.visit(this); 33 } 34 builder()35 public static Builder builder() { 36 return new AutoValue_ForStatement.Builder(); 37 } 38 39 @AutoValue.Builder 40 public abstract static class Builder { setLocalVariableExpr(VariableExpr variableExpr)41 public abstract Builder setLocalVariableExpr(VariableExpr variableExpr); 42 setBody(List<Statement> body)43 public abstract Builder setBody(List<Statement> body); 44 setCollectionExpr(Expr collectionExpr)45 public abstract Builder setCollectionExpr(Expr collectionExpr); 46 autoBuild()47 abstract ForStatement autoBuild(); 48 build()49 public ForStatement build() { 50 ForStatement forStatement = autoBuild(); 51 NodeValidator.checkNoNullElements(forStatement.body(), "body", "for-statement"); 52 Preconditions.checkState( 53 forStatement.localVariableExpr().isDecl(), 54 "For-statement variable must be a declaration"); 55 return forStatement; 56 } 57 } 58 } 59