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.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 }