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;
023
024import org.apache.yetus.audience.InterfaceAudience;
025
026@InterfaceAudience.Private
027public class NonLeafExpressionNode implements ExpressionNode {
028  private Operator op;
029  private List<ExpressionNode> childExps = new ArrayList<>(2);
030
031  public NonLeafExpressionNode() {
032
033  }
034
035  public NonLeafExpressionNode(Operator op) {
036    this.op = op;
037  }
038
039  public NonLeafExpressionNode(Operator op, List<ExpressionNode> exps) {
040    this.op = op;
041    if (op == Operator.NOT && exps.size() > 1) {
042      throw new IllegalArgumentException(Operator.NOT + " should be on 1 child expression");
043    }
044    this.childExps = exps;
045  }
046
047  public NonLeafExpressionNode(Operator op, ExpressionNode... exps) {
048    this.op = op;
049    List<ExpressionNode> expLst = new ArrayList<>();
050    Collections.addAll(expLst, exps);
051    this.childExps = expLst;
052  }
053
054  public Operator getOperator() {
055    return op;
056  }
057
058  public List<ExpressionNode> getChildExps() {
059    return childExps;
060  }
061
062  public void addChildExp(ExpressionNode exp) {
063    if (op == Operator.NOT && this.childExps.size() == 1) {
064      throw new IllegalStateException(Operator.NOT + " should be on 1 child expression");
065    }
066    this.childExps.add(exp);
067  }
068
069  public void addChildExps(List<ExpressionNode> exps) {
070    this.childExps.addAll(exps);
071  }
072
073  @Override
074  public String toString() {
075    StringBuilder sb = new StringBuilder("(");
076    if (this.op == Operator.NOT) {
077      sb.append(this.op);
078    }
079    for (int i = 0; i < this.childExps.size(); i++) {
080      sb.append(childExps.get(i));
081      if (i < this.childExps.size() - 1) {
082        sb.append(" " + this.op + " ");
083      }
084    }
085    sb.append(")");
086    return sb.toString();
087  }
088
089  @Override
090  public boolean isSingleNode() {
091    return this.op == Operator.NOT;
092  }
093
094  @Override
095  public NonLeafExpressionNode deepClone() {
096    NonLeafExpressionNode clone = new NonLeafExpressionNode(this.op);
097    for (ExpressionNode exp : this.childExps) {
098      clone.addChildExp(exp.deepClone());
099    }
100    return clone;
101  }
102}