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