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