|
| 1 | +// Copyright 2022 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.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +@AutoValue |
| 24 | +public abstract class ArrayExpr implements Expr { |
| 25 | + |
| 26 | + public abstract List<Expr> exprs(); |
| 27 | + |
| 28 | + public abstract TypeNode type(); |
| 29 | + |
| 30 | + @Override |
| 31 | + public void accept(AstNodeVisitor visitor) { |
| 32 | + visitor.visit(this); |
| 33 | + } |
| 34 | + |
| 35 | + public static ArrayExpr.Builder builder() { |
| 36 | + return new AutoValue_ArrayExpr.Builder().setExprs(ImmutableList.of()); |
| 37 | + } |
| 38 | + |
| 39 | + public static ArrayExpr withStrings(String... stringValues) { |
| 40 | + ArrayExpr.Builder builder = ArrayExpr.builder(); |
| 41 | + Arrays.asList(stringValues).stream().forEach(s -> builder.addExpr(s)); |
| 42 | + return builder.build(); |
| 43 | + } |
| 44 | + |
| 45 | + public static ArrayExpr withExprs(Expr... exprs) { |
| 46 | + return ArrayExpr.builder().setExprs(Arrays.asList(exprs)).build(); |
| 47 | + } |
| 48 | + |
| 49 | + @AutoValue.Builder |
| 50 | + public abstract static class Builder { |
| 51 | + |
| 52 | + private static final String SAME_TYPE_EXPRS_MESSAGE = |
| 53 | + "All expressions must be of the type" + " specified in this ArrayExpr"; |
| 54 | + private static final String EXPR_ALLOWED_CLASSES_MESSAGE = |
| 55 | + "Only VariableExpr and ValueExpr can be used as elements of ArrayExpr"; |
| 56 | + |
| 57 | + abstract List<Expr> exprs(); |
| 58 | + |
| 59 | + abstract TypeNode type(); |
| 60 | + |
| 61 | + /** |
| 62 | + * To add a string expression same-type validation is performed |
| 63 | + * |
| 64 | + * @param expr |
| 65 | + * @return Builder |
| 66 | + */ |
| 67 | + public ArrayExpr.Builder addExpr(String expr) { |
| 68 | + return addExpr(ValueExpr.withValue(StringObjectValue.withValue(expr))); |
| 69 | + } |
| 70 | + |
| 71 | + public ArrayExpr.Builder addExpr(Expr expr) { |
| 72 | + return setExprs((new ImmutableList.Builder<Expr>().addAll(exprs()).add(expr).build())); |
| 73 | + } |
| 74 | + |
| 75 | + public abstract ArrayExpr.Builder setExprs(List<Expr> exprs); |
| 76 | + |
| 77 | + public abstract ArrayExpr.Builder setType(TypeNode type); |
| 78 | + |
| 79 | + abstract ArrayExpr autoBuild(); |
| 80 | + |
| 81 | + public ArrayExpr build() { |
| 82 | + Preconditions.checkState( |
| 83 | + exprs().stream().allMatch(exp -> exp instanceof ValueExpr || exp instanceof VariableExpr), |
| 84 | + EXPR_ALLOWED_CLASSES_MESSAGE); |
| 85 | + TypeNode elementType = TypeNode.createElementTypeFromArrayType(type()); |
| 86 | + Preconditions.checkState( |
| 87 | + exprs().stream().allMatch(exp -> exp.type().equals(elementType)), |
| 88 | + SAME_TYPE_EXPRS_MESSAGE); |
| 89 | + return autoBuild(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments