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