001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.visibility.expression;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023import org.apache.yetus.audience.InterfaceAudience;
024
025@InterfaceAudience.Private
026public class NonLeafExpressionNode implements ExpressionNode {
027  private Operator op;
028  private List<ExpressionNode> childExps = new ArrayList<>(2);
029
030  public NonLeafExpressionNode() {
031
032  }
033
034  public NonLeafExpressionNode(Operator op) {
035    this.op = op;
036  }
037
038  public NonLeafExpressionNode(Operator op, List<ExpressionNode> exps) {
039    this.op = op;
040    if (op == Operator.NOT && exps.size() > 1) {
041      throw new IllegalArgumentException(Operator.NOT + " should be on 1 child expression");
042    }
043    this.childExps = exps;
044  }
045
046  public NonLeafExpressionNode(Operator op, ExpressionNode... exps) {
047    this.op = op;
048    List<ExpressionNode> expLst = new ArrayList<>();
049    Collections.addAll(expLst, exps);
050    this.childExps = expLst;
051  }
052
053  public Operator getOperator() {
054    return op;
055  }
056
057  public List<ExpressionNode> getChildExps() {
058    return childExps;
059  }
060
061  public void addChildExp(ExpressionNode exp) {
062    if (op == Operator.NOT && this.childExps.size() == 1) {
063      throw new IllegalStateException(Operator.NOT + " should be on 1 child expression");
064    }
065    this.childExps.add(exp);
066  }
067
068  public void addChildExps(List<ExpressionNode> exps) {
069    this.childExps.addAll(exps);
070  }
071
072  @Override
073  public String toString() {
074    StringBuilder sb = new StringBuilder("(");
075    if (this.op == Operator.NOT) {
076      sb.append(this.op);
077    }
078    for (int i = 0; i < this.childExps.size(); i++) {
079      sb.append(childExps.get(i));
080      if (i < this.childExps.size() - 1) {
081        sb.append(" " + this.op + " ");
082      }
083    }
084    sb.append(")");
085    return sb.toString();
086  }
087
088  @Override
089  public boolean isSingleNode() {
090    return this.op == Operator.NOT;
091  }
092
093  @Override
094  public NonLeafExpressionNode deepClone() {
095    NonLeafExpressionNode clone = new NonLeafExpressionNode(this.op);
096    for (ExpressionNode exp : this.childExps) {
097      clone.addChildExp(exp.deepClone());
098    }
099    return clone;
100  }
101}