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 description of the task |
---|
60 | * </p> |
---|
61 | */
|
---|
62 | private String description; |
---|
63 | |
---|
64 | /** |
---|
65 | * <p> |
---|
66 | * the instances of this task |
---|
67 | * </p> |
---|
68 | */ |
---|
69 | private Collection<ITaskInstance> instances = new HashSet<ITaskInstance>();
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p> |
---|
73 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
74 | * methdod |
---|
75 | * </p>
|
---|
76 | */
|
---|
77 | Task() {
|
---|
78 | id = getNewId();
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * <p> |
---|
83 | * creates a new id for a task using {@link #temporalId} by incrementing it an returning its |
---|
84 | * current value. Resets the counter if {@link Integer.MAX_VALUE} is reached. |
---|
85 | * </p>
|
---|
86 | *
|
---|
87 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
88 | */
|
---|
89 | private static synchronized int getNewId() {
|
---|
90 | if (temporalId == Integer.MAX_VALUE) {
|
---|
91 | temporalId = 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return temporalId++;
|
---|
95 | }
|
---|
96 | |
---|
97 | /* (non-Javadoc) |
---|
98 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
99 | */ |
---|
100 | @Override
|
---|
101 | public int getId() {
|
---|
102 | return id;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* (non-Javadoc)
|
---|
106 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
107 | */
|
---|
108 | @Override
|
---|
109 | public String getDescription() {
|
---|
110 | return description;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* (non-Javadoc) |
---|
114 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getInstances() |
---|
115 | */ |
---|
116 | @Override |
---|
117 | public Collection<ITaskInstance> getInstances() { |
---|
118 | return Collections.unmodifiableCollection(instances); |
---|
119 | } |
---|
120 | |
---|
121 | /* (non-Javadoc) |
---|
122 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
123 | */ |
---|
124 | @Override
|
---|
125 | public final boolean equals(ITask task) { |
---|
126 | // tasks are only equal if they are identical or if they have the same id |
---|
127 | // (may happen, if they are cloned)
|
---|
128 | return (this == task) || (this.hashCode() == task.hashCode());
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* (non-Javadoc) |
---|
132 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
133 | */ |
---|
134 | @Override
|
---|
135 | public synchronized int hashCode() {
|
---|
136 | return id;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* (non-Javadoc)
|
---|
140 | * @see java.lang.Object#toString()
|
---|
141 | */
|
---|
142 | @Override
|
---|
143 | public synchronized String toString() { |
---|
144 | StringBuffer result = new StringBuffer(); |
---|
145 | result.append("task "); |
---|
146 | result.append(id); |
---|
147 | |
---|
148 | if (description != null) { |
---|
149 | result.append(" ("); |
---|
150 | result.append(description); |
---|
151 | result.append(')'); |
---|
152 | } |
---|
153 | |
---|
154 | return result.toString();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* (non-Javadoc) |
---|
158 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
159 | */ |
---|
160 | @Override
|
---|
161 | public synchronized ITask clone() {
|
---|
162 | Task clone = null;
|
---|
163 | try {
|
---|
164 | clone = (Task) super.clone();
|
---|
165 | }
|
---|
166 | catch (CloneNotSupportedException e) {
|
---|
167 | // this should never happen. Therefore simply dump the exception
|
---|
168 | e.printStackTrace();
|
---|
169 | }
|
---|
170 |
|
---|
171 | return clone;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** |
---|
175 | * <p> |
---|
176 | * internally used to set the human readable description of the task |
---|
177 | * </p> |
---|
178 | * |
---|
179 | * @param description the new human readable description of the task |
---|
180 | */ |
---|
181 | void setDescription(String description) { |
---|
182 | this.description = description; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * <p> |
---|
187 | * internally used to add an instance to this task |
---|
188 | * </p> |
---|
189 | * |
---|
190 | * @param instance the instance belonging to this task |
---|
191 | */ |
---|
192 | void addInstance(ITaskInstance instance) { |
---|
193 | this.instances.add(instance); |
---|
194 | } |
---|
195 | |
---|
196 | /* (non-Javadoc) |
---|
197 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
198 | */ |
---|
199 | @Override |
---|
200 | public void accept(ITaskVisitor visitor) { |
---|
201 | visitor.visit(this); |
---|
202 | } |
---|
203 | |
---|
204 | }
|
---|