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