Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TreeNode.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.common;
16 
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 
21 import com.cloudera.impala.util.Visitor;
22 import com.google.common.base.Predicate;
23 
24 public class TreeNode<NodeType extends TreeNode<NodeType>> {
25  protected ArrayList<NodeType> children_;
26 
27  protected TreeNode() {
28  this.children_ = new ArrayList<NodeType>();
29  }
30 
31  public NodeType getChild(int i) {
32  return hasChild(i) ? children_.get(i) : null;
33  }
34 
35  public boolean hasChild(int i) {
36  return children_.size() > i;
37  }
38 
39  public void addChild(NodeType n) {
40  children_.add(n);
41  }
42 
43  public void addChildren(List<? extends NodeType> l) {
44  children_.addAll(l);
45  }
46 
47  public void setChild(int index, NodeType n) {
48  children_.set(index, n);
49  }
50 
51  public ArrayList<NodeType> getChildren() {
52  return children_;
53  }
54 
60  public <C extends TreeNode<NodeType>, D extends C> void collect(
61  Predicate<? super C> predicate, Collection<D> matches) {
62  // TODO: the semantics of this function are very strange. contains()
63  // checks using .equals() on the nodes. In the case of literals, slotrefs
64  // and maybe others, two different tree node objects can be equal and
65  // this function would only return one of them. This is not intuitive.
66  // We rely on these semantics to not have duplicate nodes. Investigate this.
67  if (predicate.apply((C) this) && !matches.contains(this)) {
68  matches.add((D) this);
69  return;
70  }
71 
72  for (NodeType child: children_) {
73  child.collect(predicate, matches);
74  }
75  }
76 
82  public <C extends TreeNode<NodeType>, D extends C> void collect(
83  Class cl, Collection<D> matches) {
84  if (cl.equals(getClass())) {
85  matches.add((D) this);
86  return;
87  }
88 
89  for (NodeType child: children_) {
90  child.collect(cl, matches);
91  }
92  }
93 
99  public <C extends TreeNode<NodeType>, D extends C> void collectAll(
100  Predicate<? super C> predicate, List<D> matches) {
101  if (predicate.apply((C) this)) {
102  matches.add((D) this);
103  }
104 
105  for (NodeType child: children_) {
106  child.collectAll(predicate, matches);
107  }
108  }
109 
114  public static <C extends TreeNode<C>, D extends C> void collect(
115  Collection<C> nodeList, Predicate<? super C> predicate, Collection<D> matches) {
116  for (C node: nodeList) {
117  node.collect(predicate, matches);
118  }
119  }
120 
125  public static <C extends TreeNode<C>, D extends C> void collect(
126  Collection<C> nodeList, Class cl, Collection<D> matches) {
127  for (C node: nodeList) {
128  node.collect(cl, matches);
129  }
130  }
131 
135  public <C extends TreeNode<NodeType>> boolean contains(
136  Predicate<? super C> predicate) {
137  if (predicate.apply((C) this)) return true;
138  for (NodeType child: children_) {
139  if (child.contains(predicate)) return true;
140  }
141  return false;
142  }
143 
147  public boolean contains(Class cl) {
148  if (cl.equals(getClass())) return true;
149  for (NodeType child: children_) {
150  if (child.contains(cl)) return true;
151  }
152  return false;
153  }
154 
159  public static <C extends TreeNode<C>, D extends C> boolean contains(
160  Collection<C> nodeList, Predicate<? super C> predicate) {
161  for (C node: nodeList) {
162  if (node.contains(predicate)) return true;
163  }
164  return false;
165  }
166 
170  public static <C extends TreeNode<C>> boolean contains(
171  List<C> nodeList, Class cl) {
172  for (C node: nodeList) {
173  if (node.contains(cl)) return true;
174  }
175  return false;
176  }
177 
181  public <C extends NodeType> C findFirstOf(Class<C> cl) {
182  if (this.getClass().equals(cl)) return (C) this;
183  for (NodeType child: children_) {
184  NodeType result = child.findFirstOf(cl);
185  if (result != null) return (C) result;
186  }
187  return null;
188  }
189 
193  public <C extends TreeNode<NodeType>> void accept(Visitor<C> visitor) {
194  visitor.visit((C) this);
195  for (NodeType p: children_) {
196  p.accept(visitor);
197  }
198  }
199 }
public< C extends TreeNode< NodeType >, D extends C > void collect(Class cl, Collection< D > matches)
Definition: TreeNode.java:82
static< CextendsTreeNode< C > D extends C void collect(Collection< C > nodeList, Class cl, Collection< D > matches)
Definition: TreeNode.java:125
public< C extends TreeNode< NodeType > > void accept(Visitor< C > visitor)
Definition: TreeNode.java:193
public< C extends TreeNode< NodeType >, D extends C > void collect(Predicate<?super C > predicate, Collection< D > matches)
Definition: TreeNode.java:60
static< CextendsTreeNode< C > D extends C boolean contains(Collection< C > nodeList, Predicate<?super C > predicate)
Definition: TreeNode.java:159
public< C extends TreeNode< NodeType > > boolean contains(Predicate<?super C > predicate)
Definition: TreeNode.java:135
static< CextendsTreeNode< C > D extends C void collect(Collection< C > nodeList, Predicate<?super C > predicate, Collection< D > matches)
Definition: TreeNode.java:114
static< CextendsTreeNode< C > boolean contains(List< C > nodeList, Class cl)
Definition: TreeNode.java:170
public< C extends TreeNode< NodeType >, D extends C > void collectAll(Predicate<?super C > predicate, List< D > matches)
Definition: TreeNode.java:99