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