View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security.visibility.expression;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  
26  @InterfaceAudience.Private
27  public class NonLeafExpressionNode implements ExpressionNode {
28    private Operator op;
29    private List<ExpressionNode> childExps = new ArrayList<ExpressionNode>(2);
30  
31    public NonLeafExpressionNode() {
32  
33    }
34  
35    public NonLeafExpressionNode(Operator op) {
36      this.op = op;
37    }
38  
39    public NonLeafExpressionNode(Operator op, List<ExpressionNode> exps) {
40      this.op = op;
41      if (op == Operator.NOT && exps.size() > 1) {
42        throw new IllegalArgumentException(Operator.NOT + " should be on 1 child expression");
43      }
44      this.childExps = exps;
45    }
46  
47    public NonLeafExpressionNode(Operator op, ExpressionNode... exps) {
48      this.op = op;
49      List<ExpressionNode> expLst = new ArrayList<ExpressionNode>();
50      Collections.addAll(expLst, exps);
51      this.childExps = expLst;
52    }
53  
54    public Operator getOperator() {
55      return op;
56    }
57  
58    public List<ExpressionNode> getChildExps() {
59      return childExps;
60    }
61  
62    public void addChildExp(ExpressionNode exp) {
63      if (op == Operator.NOT && this.childExps.size() == 1) {
64        throw new IllegalStateException(Operator.NOT + " should be on 1 child expression");
65      }
66      this.childExps.add(exp);
67    }
68  
69    public void addChildExps(List<ExpressionNode> exps) {
70      this.childExps.addAll(exps);
71    }
72  
73    @Override
74    public String toString() {
75      StringBuilder sb = new StringBuilder("(");
76      if (this.op == Operator.NOT) {
77        sb.append(this.op);
78      }
79      for (int i = 0; i < this.childExps.size(); i++) {
80        sb.append(childExps.get(i));
81        if (i < this.childExps.size() - 1) {
82          sb.append(" " + this.op + " ");
83        }
84      }
85      sb.append(")");
86      return sb.toString();
87    }
88  
89    @Override
90    public boolean isSingleNode() {
91      return this.op == Operator.NOT;
92    }
93  
94    public NonLeafExpressionNode deepClone() {
95      NonLeafExpressionNode clone = new NonLeafExpressionNode(this.op);
96      for (ExpressionNode exp : this.childExps) {
97        clone.addChildExp(exp.deepClone());
98      }
99      return clone;
100   }
101 }