%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.base.util.Tree |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: kamal.govindraj (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.base.util; |
|
23 | ||
24 | import org.apache.log4j.Logger; |
|
25 | ||
26 | import java.io.Serializable; |
|
27 | ||
28 | /** |
|
29 | * Implementation of a Tree data Structure. A node in this tree can represent |
|
30 | * any data object |
|
31 | * |
|
32 | * @author kamal.govindraj |
|
33 | */ |
|
34 | public class Tree implements Serializable { |
|
35 | 10 | private static final Logger log = LoggingFactory.getLogger(Tree.class); |
36 | ||
37 | 50 | private TreeNode root = null; |
38 | ||
39 | /** |
|
40 | * Creates a Tree with null root. |
|
41 | */ |
|
42 | 36 | public Tree() { |
43 | 36 | this.root = null; |
44 | 36 | } |
45 | ||
46 | /** |
|
47 | * Constructor that creates a Tree with a specified Node as root. |
|
48 | * |
|
49 | * @param root - |
|
50 | * root node |
|
51 | */ |
|
52 | 14 | public Tree(TreeNode root) { |
53 | 14 | setRoot(root); |
54 | 14 | } |
55 | ||
56 | /** |
|
57 | * Gets the root node of this tree. |
|
58 | * |
|
59 | * @return root node |
|
60 | */ |
|
61 | public TreeNode getRoot() { |
|
62 | 43 | return this.root; |
63 | } |
|
64 | ||
65 | public void setRoot(TreeNode root) { |
|
66 | 49 | if (this.root != null) { |
67 | 1 | this.root.setDepth(-1); |
68 | 1 | this.root.setPostion(-1); |
69 | 1 | if (log.isDebugEnabled()) { |
70 | 0 | log.debug("Replacing root node of tree"); |
71 | } |
|
72 | } |
|
73 | 49 | this.root = root; |
74 | 49 | if (this.root != null) { |
75 | 49 | this.root.setDepth(0); |
76 | 49 | this.root.setPostion(0); |
77 | } |
|
78 | 49 | } |
79 | ||
80 | public void setDepth() { |
|
81 | 0 | root.setDepth(0); |
82 | 0 | } |
83 | ||
84 | public void traverseBreadthFirst(NodeVisitor visitor) { |
|
85 | 2 | if (log.isDebugEnabled()) { |
86 | 0 | log.debug("Entering method traverseBreadthFirst"); |
87 | } |
|
88 | 2 | visitor.beginTraversal(); |
89 | 2 | if (this.root != null) { |
90 | 2 | visitor.visit(root); |
91 | 2 | visitor.goingDown(); |
92 | 2 | this.root.traverseBreadthFirst(visitor); |
93 | 2 | visitor.climbingUp(); |
94 | } |
|
95 | 2 | visitor.endTraversal(); |
96 | 2 | } |
97 | ||
98 | public TreeNode find(Object value) { |
|
99 | 5 | if (this.root == null) { |
100 | 1 | return null; |
101 | } |
|
102 | 4 | if (this.root.getValue() == value) { |
103 | 1 | return this.root; |
104 | } else { |
|
105 | 3 | return this.root.find(value); |
106 | } |
|
107 | } |
|
108 | ||
109 | public String toString() { |
|
110 | 0 | return root.toString(); |
111 | } |
|
112 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |