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.Collection; |
---|
18 | import java.util.Collections; |
---|
19 | import java.util.HashSet; |
---|
20 | |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor; |
---|
24 |
|
---|
25 | /**
|
---|
26 | * <p> |
---|
27 | * this is the default implementation of the interface {@link ITask}. It |
---|
28 | * does not do anything fancy except implementing the interface. |
---|
29 | * </p> |
---|
30 | * |
---|
31 | * @author Patrick Harms |
---|
32 | */
|
---|
33 | class Task implements ITask {
|
---|
34 | |
---|
35 | /** |
---|
36 | * <p> |
---|
37 | * default serial version UID |
---|
38 | * </p> |
---|
39 | */ |
---|
40 | private static final long serialVersionUID = 1L; |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * used as a counter to generate new ids for each newly created task. May overflow. |
---|
45 | * </p> |
---|
46 | */
|
---|
47 | private static int temporalId = 0;
|
---|
48 |
|
---|
49 | /** |
---|
50 | * <p> |
---|
51 | * the id of the task (unique throughout the system as long as {@link #temporalId} does not |
---|
52 | * overflow. |
---|
53 | * </p> |
---|
54 | */
|
---|
55 | private int id; |
---|
56 | |
---|
57 | /** |
---|
58 | * <p> |
---|
59 | * a human readable type of the task (used for visualization purposes) |
---|
60 | * </p> |
---|
61 | */ |
---|
62 | private String type;
|
---|
63 |
|
---|
64 | /** |
---|
65 | * <p> |
---|
66 | * a human readable description of the task |
---|
67 | * </p> |
---|
68 | */
|
---|
69 | private String description; |
---|
70 | |
---|
71 | /** |
---|
72 | * <p> |
---|
73 | * the instances of this task |
---|
74 | * </p> |
---|
75 | */ |
---|
76 | private Collection<ITaskInstance> instances = new HashSet<ITaskInstance>();
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * <p> |
---|
80 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
81 | * method |
---|
82 | * </p> |
---|
83 | * |
---|
84 | * @param type the human readable type of the task |
---|
85 | * |
---|
86 | * @throws IllegalArgumentException in the case the provided type is null
|
---|
87 | */
|
---|
88 | Task(String type) {
|
---|
89 | this.id = getNewId(); |
---|
90 | this.type = type; |
---|
91 | |
---|
92 | if (type == null) { |
---|
93 | throw new IllegalArgumentException("type must not be null"); |
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * <p> |
---|
99 | * creates a new id for a task using {@link #temporalId} by incrementing it an returning its |
---|
100 | * current value. Resets the counter if {@link Integer.MAX_VALUE} is reached. |
---|
101 | * </p>
|
---|
102 | *
|
---|
103 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
104 | */
|
---|
105 | private static synchronized int getNewId() {
|
---|
106 | if (temporalId == Integer.MAX_VALUE) {
|
---|
107 | temporalId = 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return temporalId++;
|
---|
111 | }
|
---|
112 | |
---|
113 | /* (non-Javadoc) |
---|
114 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
115 | */ |
---|
116 | @Override
|
---|
117 | public int getId() {
|
---|
118 | return id;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* (non-Javadoc) |
---|
122 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getType() |
---|
123 | */ |
---|
124 | @Override |
---|
125 | public String getType() { |
---|
126 | return type; |
---|
127 | } |
---|
128 | |
---|
129 | /* (non-Javadoc)
|
---|
130 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
131 | */
|
---|
132 | @Override
|
---|
133 | public String getDescription() {
|
---|
134 | return description;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* (non-Javadoc) |
---|
138 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getInstances() |
---|
139 | */ |
---|
140 | @Override |
---|
141 | public Collection<ITaskInstance> getInstances() { |
---|
142 | return Collections.unmodifiableCollection(instances); |
---|
143 | } |
---|
144 | |
---|
145 | /* (non-Javadoc) |
---|
146 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
147 | */ |
---|
148 | @Override
|
---|
149 | public final boolean equals(ITask task) { |
---|
150 | // tasks are only equal if they are identical or if they have the same id |
---|
151 | // (may happen, if they are cloned)
|
---|
152 | return (this == task) || (this.hashCode() == task.hashCode());
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* (non-Javadoc) |
---|
156 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
157 | */ |
---|
158 | @Override
|
---|
159 | public synchronized int hashCode() {
|
---|
160 | return id;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* (non-Javadoc)
|
---|
164 | * @see java.lang.Object#toString()
|
---|
165 | */
|
---|
166 | @Override
|
---|
167 | public synchronized String toString() { |
---|
168 | StringBuffer result = new StringBuffer(); |
---|
169 | result.append(type); |
---|
170 | result.append(" #"); |
---|
171 | result.append(id); |
---|
172 | |
---|
173 | if (description != null) { |
---|
174 | result.append(" ("); |
---|
175 | result.append(description); |
---|
176 | result.append(')'); |
---|
177 | } |
---|
178 | |
---|
179 | return result.toString();
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* (non-Javadoc) |
---|
183 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
184 | */ |
---|
185 | @Override
|
---|
186 | public synchronized ITask clone() {
|
---|
187 | Task clone = null;
|
---|
188 | try {
|
---|
189 | clone = (Task) super.clone();
|
---|
190 | }
|
---|
191 | catch (CloneNotSupportedException e) {
|
---|
192 | // this should never happen. Therefore simply dump the exception
|
---|
193 | e.printStackTrace();
|
---|
194 | }
|
---|
195 |
|
---|
196 | return clone;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** |
---|
200 | * <p> |
---|
201 | * internally used to set the human readable description of the task |
---|
202 | * </p> |
---|
203 | * |
---|
204 | * @param description the new human readable description of the task |
---|
205 | */ |
---|
206 | void setDescription(String description) { |
---|
207 | this.description = description; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * <p> |
---|
212 | * internally used to remove an instance from this task |
---|
213 | * </p> |
---|
214 | * |
---|
215 | * @param instance the instance to be removed from this task |
---|
216 | */ |
---|
217 | void removeInstance(ITaskInstance instance) { |
---|
218 | this.instances.remove(instance); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * <p> |
---|
223 | * internally used to add an instance to this task |
---|
224 | * </p> |
---|
225 | * |
---|
226 | * @param instance the instance belonging to this task |
---|
227 | */ |
---|
228 | void addInstance(ITaskInstance instance) { |
---|
229 | this.instances.add(instance); |
---|
230 | } |
---|
231 | |
---|
232 | /* (non-Javadoc) |
---|
233 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
234 | */ |
---|
235 | @Override |
---|
236 | public void accept(ITaskVisitor visitor) { |
---|
237 | visitor.visit(this); |
---|
238 | } |
---|
239 | |
---|
240 | }
|
---|