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 de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceVisitor; |
---|
20 | |
---|
21 | // TODO: Auto-generated Javadoc |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * this is the default implementation of the interface {@link ITaskInstance}. It |
---|
25 | * does not do anything fancy except implementing the interface. |
---|
26 | * </p> |
---|
27 | * |
---|
28 | * @author Patrick Harms |
---|
29 | */ |
---|
30 | class TaskInstance implements ITaskInstance { |
---|
31 | |
---|
32 | /** |
---|
33 | * <p> |
---|
34 | * creates a new id for a task instance using {@link #temporalId} by |
---|
35 | * incrementing it an returning its current value. Resets the counter if |
---|
36 | * {@link Integer.MAX_VALUE} is reached. |
---|
37 | * </p> |
---|
38 | * |
---|
39 | * @return a new unique id for a task instance as long as |
---|
40 | * {@link #temporalId} does not overflow |
---|
41 | */ |
---|
42 | private static synchronized int getNewId() { |
---|
43 | if (temporalId == Integer.MAX_VALUE) { |
---|
44 | temporalId = 0; |
---|
45 | } |
---|
46 | |
---|
47 | return temporalId++; |
---|
48 | } |
---|
49 | |
---|
50 | /** <p> default serial version UID </p>. */ |
---|
51 | private static final long serialVersionUID = 1L; |
---|
52 | |
---|
53 | /** |
---|
54 | * <p> |
---|
55 | * used as a counter to generate new ids for each newly created task |
---|
56 | * instance. May overflow. |
---|
57 | * </p> |
---|
58 | */ |
---|
59 | private static int temporalId = 0; |
---|
60 | |
---|
61 | /** <p> the task instantiated by this task instance </p>. */ |
---|
62 | private ITask task; |
---|
63 | |
---|
64 | /** |
---|
65 | * <p> |
---|
66 | * the id of the task instance (unique throughout the system as long as |
---|
67 | * {@link #temporalId} does not overflow. |
---|
68 | * </p> |
---|
69 | */ |
---|
70 | private final int id; |
---|
71 | |
---|
72 | /** |
---|
73 | * <p> |
---|
74 | * instantiated the task instance with the task that is instantiated by the |
---|
75 | * instance. It also assigns a unique id to the instance using |
---|
76 | * {@link #getNewId()}. |
---|
77 | * </p> |
---|
78 | * |
---|
79 | * @param task the task |
---|
80 | */ |
---|
81 | TaskInstance(ITask task) { |
---|
82 | this.task = task; |
---|
83 | id = getNewId(); |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * (non-Javadoc) |
---|
88 | * |
---|
89 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#accept( |
---|
90 | * ITaskInstanceVisitor) |
---|
91 | */ |
---|
92 | @Override |
---|
93 | public void accept(ITaskInstanceVisitor visitor) { |
---|
94 | visitor.visit(this); |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * (non-Javadoc) |
---|
99 | * |
---|
100 | * @see java.lang.Object#clone() |
---|
101 | */ |
---|
102 | @Override |
---|
103 | public synchronized ITaskInstance clone() { |
---|
104 | TaskInstance clone = null; |
---|
105 | try { |
---|
106 | clone = (TaskInstance) super.clone(); |
---|
107 | } catch (final CloneNotSupportedException e) { |
---|
108 | // this should never happen. Therefore simply dump the exception |
---|
109 | e.printStackTrace(); |
---|
110 | } |
---|
111 | |
---|
112 | return clone; |
---|
113 | } |
---|
114 | |
---|
115 | /* |
---|
116 | * (non-Javadoc) |
---|
117 | * |
---|
118 | * @see |
---|
119 | * de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode) |
---|
120 | */ |
---|
121 | @Override |
---|
122 | public boolean equals(ITaskInstance taskInstance) { |
---|
123 | // task instances are only equal if they are identical or if they have |
---|
124 | // the same id |
---|
125 | // (may happen, if they are cloned) |
---|
126 | return (this == taskInstance) |
---|
127 | || (this.hashCode() == taskInstance.hashCode()); |
---|
128 | } |
---|
129 | |
---|
130 | /* |
---|
131 | * (non-Javadoc) |
---|
132 | * |
---|
133 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getTask() |
---|
134 | */ |
---|
135 | @Override |
---|
136 | public ITask getTask() { |
---|
137 | return task; |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * (non-Javadoc) |
---|
142 | * |
---|
143 | * @see java.lang.Object#hashCode() |
---|
144 | */ |
---|
145 | @Override |
---|
146 | public synchronized int hashCode() { |
---|
147 | return id; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * <p> |
---|
152 | * used to update the task represented through this instance |
---|
153 | * </p>. |
---|
154 | * |
---|
155 | * @param task the task to set |
---|
156 | */ |
---|
157 | void setTask(ITask task) { |
---|
158 | this.task = task; |
---|
159 | } |
---|
160 | |
---|
161 | /* |
---|
162 | * (non-Javadoc) |
---|
163 | * |
---|
164 | * @see java.lang.Object#toString() |
---|
165 | */ |
---|
166 | @Override |
---|
167 | public synchronized String toString() { |
---|
168 | final StringBuffer result = new StringBuffer(); |
---|
169 | result.append(task.getType()); |
---|
170 | result.append(" #"); |
---|
171 | result.append(task.getId()); |
---|
172 | |
---|
173 | if (task.getDescription() != null) { |
---|
174 | result.append(" ("); |
---|
175 | result.append(task.getDescription()); |
---|
176 | result.append(')'); |
---|
177 | } |
---|
178 | |
---|
179 | /* |
---|
180 | * if (children != null) { result.append(", "); |
---|
181 | * result.append(children.size()); result.append(" children"); } |
---|
182 | */ |
---|
183 | |
---|
184 | return result.toString(); |
---|
185 | } |
---|
186 | |
---|
187 | } |
---|