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