1 | package de.ugoe.cs.autoquest.tasktrees.treeimpl; |
---|
2 | |
---|
3 | import java.util.HashMap; |
---|
4 | import java.util.Map; |
---|
5 | |
---|
6 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; |
---|
7 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
8 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeInfo; |
---|
9 | |
---|
10 | /** |
---|
11 | * TODO comment |
---|
12 | * |
---|
13 | * @version $Revision: $ $Date: 21.02.2012$ |
---|
14 | * @author 2012, last modified by $Author: patrick$ |
---|
15 | */ |
---|
16 | public class TaskTree implements ITaskTree { |
---|
17 | |
---|
18 | /** the map of nodes */ |
---|
19 | private Map<ITaskTreeNode, ITaskTreeNodeInfo> taskMap; |
---|
20 | |
---|
21 | /** the root node of the task tree */ |
---|
22 | private ITaskTreeNode rootNode; |
---|
23 | |
---|
24 | /** |
---|
25 | * TODO: comment |
---|
26 | * |
---|
27 | */ |
---|
28 | TaskTree(ITaskTreeNode rootNode) { |
---|
29 | this.rootNode = rootNode; |
---|
30 | } |
---|
31 | |
---|
32 | /* |
---|
33 | * (non-Javadoc) |
---|
34 | * |
---|
35 | * @see de.ugoe.cs.tasktree.treeifc.TaskTree#getRoot() |
---|
36 | */ |
---|
37 | @Override |
---|
38 | public ITaskTreeNode getRoot() { |
---|
39 | return rootNode; |
---|
40 | } |
---|
41 | |
---|
42 | /* |
---|
43 | * (non-Javadoc) |
---|
44 | * |
---|
45 | * @see de.ugoe.cs.tasktree.treeifc.TaskTree#getTaskMap() |
---|
46 | */ |
---|
47 | @Override |
---|
48 | public synchronized Map<ITaskTreeNode, ITaskTreeNodeInfo> getTaskMap() { |
---|
49 | if (taskMap == null) { |
---|
50 | taskMap = new HashMap<ITaskTreeNode, ITaskTreeNodeInfo>(); |
---|
51 | addNodeToMap(rootNode, null); |
---|
52 | } |
---|
53 | return taskMap; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * TODO: comment |
---|
58 | * |
---|
59 | * @param rootNode |
---|
60 | */ |
---|
61 | private void addNodeToMap(ITaskTreeNode node, ITaskTreeNode parent) { |
---|
62 | NodeInfo nodeInfo = (NodeInfo) taskMap.get(node); |
---|
63 | |
---|
64 | if (nodeInfo == null) { |
---|
65 | nodeInfo = new NodeInfo(node); |
---|
66 | taskMap.put(node, nodeInfo); |
---|
67 | } |
---|
68 | |
---|
69 | if (parent != null) { |
---|
70 | // through first removing an existing parent it is assured, that a parent is recorded |
---|
71 | // only once. This is needed, because parent may be reused in a tree as well, but we |
---|
72 | // always iterate the whole tree |
---|
73 | nodeInfo.removeParent(parent); |
---|
74 | nodeInfo.addParent(parent); |
---|
75 | } |
---|
76 | |
---|
77 | for (ITaskTreeNode child : node.getChildren()) { |
---|
78 | addNodeToMap(child, node); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /* |
---|
83 | * (non-Javadoc) |
---|
84 | * |
---|
85 | * @see java.lang.Object#clone() |
---|
86 | */ |
---|
87 | @Override |
---|
88 | public TaskTree clone() { |
---|
89 | TaskTree clone = null; |
---|
90 | try { |
---|
91 | clone = (TaskTree) super.clone(); |
---|
92 | |
---|
93 | clone.rootNode = rootNode.clone(); |
---|
94 | |
---|
95 | // the clone will create the task map itself, when it is first retrieved |
---|
96 | clone.taskMap = null; |
---|
97 | |
---|
98 | } |
---|
99 | catch (CloneNotSupportedException e) { |
---|
100 | // this should never happen. Therefore simply dump the exception |
---|
101 | e.printStackTrace(); |
---|
102 | } |
---|
103 | |
---|
104 | return clone; |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|