15 package com.cloudera.impala.common;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import com.cloudera.impala.util.Visitor;
22 import com.google.common.base.Predicate;
24 public class TreeNode<NodeType
extends TreeNode<NodeType>> {
28 this.children_ =
new ArrayList<NodeType>();
32 return hasChild(i) ? children_.get(i) : null;
36 return children_.size() > i;
48 children_.set(index, n);
60 public <C extends TreeNode<NodeType>, D extends C>
void collect(
61 Predicate<? super C> predicate, Collection<D> matches) {
67 if (predicate.apply((C)
this) && !matches.contains(
this)) {
68 matches.add((D)
this);
72 for (NodeType child: children_) {
73 child.collect(predicate, matches);
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);
89 for (NodeType child: children_) {
90 child.collect(cl, matches);
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);
105 for (NodeType child: children_) {
106 child.collectAll(predicate, matches);
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);
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);
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;
148 if (cl.equals(getClass()))
return true;
149 for (NodeType child: children_) {
150 if (child.contains(cl))
return true;
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;
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;
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;
193 public <C extends TreeNode<NodeType>>
void accept(Visitor<C> visitor) {
194 visitor.visit((C)
this);
195 for (NodeType p: children_) {
void addChild(NodeType n)
public< C extends TreeNode< NodeType >, D extends C > void collect(Class cl, Collection< D > matches)
boolean contains(Class cl)
static< CextendsTreeNode< C > D extends C void collect(Collection< C > nodeList, Class cl, Collection< D > matches)
ArrayList< NodeType > getChildren()
public< C extends TreeNode< NodeType > > void accept(Visitor< C > visitor)
public< C extends TreeNode< NodeType >, D extends C > void collect(Predicate<?super C > predicate, Collection< D > matches)
static< CextendsTreeNode< C > D extends C boolean contains(Collection< C > nodeList, Predicate<?super C > predicate)
public< C extends TreeNode< NodeType > > boolean contains(Predicate<?super C > predicate)
static< CextendsTreeNode< C > D extends C void collect(Collection< C > nodeList, Predicate<?super C > predicate, Collection< D > matches)
public< C extends NodeType > C findFirstOf(Class< C > cl)
static< CextendsTreeNode< C > boolean contains(List< C > nodeList, Class cl)
public< C extends TreeNode< NodeType >, D extends C > void collectAll(Predicate<?super C > predicate, List< D > matches)
void addChildren(List<?extends NodeType > l)
void setChild(int index, NodeType n)
ArrayList< NodeType > children_