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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * @deprecated since 2.0.0
28   */
29  @Deprecated
30  public class Node {
31  
32    private List<Node> children;
33  
34    private Node parent;
35  
36    public void add(final Node node) {
37      initChildren();
38      children.add(node);
39      node.setParent(this);
40    }
41  
42    public List<Node> getChildren() {
43      initChildren();
44      return children;
45    }
46  
47    private void initChildren() {
48      if (children == null) {
49        children = new ArrayList<Node>();
50      }
51    }
52  
53    public Node getChildAt(final int index) {
54      initChildren();
55      return children.get(index);
56    }
57  
58    public boolean isRoot() {
59      return parent == null;
60    }
61  
62    public int getIndex(final Node node) {
63      for (int i = 0; i < children.size(); i++) {
64        final Node child = children.get(i);
65        if (child.equals(node)) {
66          return i;
67        }
68      }
69      return -1;
70    }
71  
72    public int getChildCount() {
73      return children == null ? 0 : children.size();
74    }
75  
76    public boolean hasNextSibling() {
77      return parent != null && parent.getIndex(this) + 1 < parent.getChildCount();
78    }
79  
80    public Node nextSibling() {
81      if (parent == null) {
82        return null;
83      }
84      final int nextIndex = parent.getIndex(this) + 1;
85      return nextIndex < parent.getChildCount() ? parent.getChildAt(nextIndex) : null;
86    }
87  
88    public TreePath getPath() {
89      final List<Integer> result = new ArrayList<Integer>();
90      Node node = this;
91      Node parent = this.parent;
92      while (parent != null) {
93        final int index = parent.getIndex(node);
94        result.add(index);
95        node = parent;
96        parent = node.getParent();
97      }
98      result.add(0);
99      Collections.reverse(result);
100     return new TreePath(result);
101   }
102 
103   public Node getParent() {
104     return parent;
105   }
106 
107   public void setParent(final Node parent) {
108     this.parent = parent;
109   }
110 }