1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.security.visibility.expression;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24
25 @InterfaceAudience.Private
26 public class NonLeafExpressionNode implements ExpressionNode {
27 private Operator op;
28 private List<ExpressionNode> childExps = new ArrayList<ExpressionNode>(2);
29
30 public NonLeafExpressionNode() {
31
32 }
33
34 public NonLeafExpressionNode(Operator op) {
35 this.op = op;
36 }
37
38 public NonLeafExpressionNode(Operator op, List<ExpressionNode> exps) {
39 this.op = op;
40 if (op == Operator.NOT && exps.size() > 1) {
41 throw new IllegalArgumentException(Operator.NOT + " should be on 1 child expression");
42 }
43 this.childExps = exps;
44 }
45
46 public NonLeafExpressionNode(Operator op, ExpressionNode... exps) {
47 this.op = op;
48 List<ExpressionNode> expLst = new ArrayList<ExpressionNode>();
49 for (ExpressionNode exp : exps) {
50 expLst.add(exp);
51 }
52 this.childExps = expLst;
53 }
54
55 public Operator getOperator() {
56 return op;
57 }
58
59 public List<ExpressionNode> getChildExps() {
60 return childExps;
61 }
62
63 public void addChildExp(ExpressionNode exp) {
64 if (op == Operator.NOT && this.childExps.size() == 1) {
65 throw new IllegalStateException(Operator.NOT + " should be on 1 child expression");
66 }
67 this.childExps.add(exp);
68 }
69
70 public void addChildExps(List<ExpressionNode> exps) {
71 this.childExps.addAll(exps);
72 }
73
74 @Override
75 public String toString() {
76 StringBuilder sb = new StringBuilder("(");
77 if (this.op == Operator.NOT) {
78 sb.append(this.op);
79 }
80 for (int i = 0; i < this.childExps.size(); i++) {
81 sb.append(childExps.get(i));
82 if (i < this.childExps.size() - 1) {
83 sb.append(" " + this.op + " ");
84 }
85 }
86 sb.append(")");
87 return sb.toString();
88 }
89
90 @Override
91 public boolean isSingleNode() {
92 return this.op == Operator.NOT;
93 }
94
95 public NonLeafExpressionNode deepClone() {
96 NonLeafExpressionNode clone = new NonLeafExpressionNode(this.op);
97 for (ExpressionNode exp : this.childExps) {
98 clone.addChildExp(exp.deepClone());
99 }
100 return clone;
101 }
102 }