[1113] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl;
|
---|
[439] | 16 |
|
---|
[1294] | 17 | import java.util.Collection; |
---|
| 18 | import java.util.Collections; |
---|
| 19 | import java.util.HashSet; |
---|
| 20 | |
---|
[1146] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1294] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1157] | 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor; |
---|
[439] | 24 |
|
---|
| 25 | /**
|
---|
[1215] | 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 |
---|
[439] | 32 | */
|
---|
[1146] | 33 | class Task implements ITask {
|
---|
| 34 | |
---|
[1215] | 35 | /** |
---|
| 36 | * <p> |
---|
| 37 | * default serial version UID |
---|
| 38 | * </p> |
---|
| 39 | */ |
---|
[1146] | 40 | private static final long serialVersionUID = 1L; |
---|
| 41 | |
---|
[1215] | 42 | /** |
---|
| 43 | * <p> |
---|
| 44 | * used as a counter to generate new ids for each newly created task. May overflow. |
---|
| 45 | * </p> |
---|
| 46 | */
|
---|
[557] | 47 | private static int temporalId = 0;
|
---|
[439] | 48 |
|
---|
[1215] | 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 | */
|
---|
[1146] | 55 | private int id;
|
---|
[439] | 56 |
|
---|
[1215] | 57 | /** |
---|
| 58 | * <p> |
---|
| 59 | * a human readable description of the task |
---|
| 60 | * </p> |
---|
| 61 | */
|
---|
[1294] | 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>();
|
---|
[439] | 70 |
|
---|
[557] | 71 | /**
|
---|
[1215] | 72 | * <p> |
---|
| 73 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
| 74 | * methdod |
---|
| 75 | * </p>
|
---|
[557] | 76 | */
|
---|
[1146] | 77 | Task() {
|
---|
[557] | 78 | id = getNewId();
|
---|
[439] | 79 | }
|
---|
| 80 |
|
---|
[557] | 81 | /**
|
---|
[1215] | 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>
|
---|
[557] | 86 | *
|
---|
[1215] | 87 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
[557] | 88 | */
|
---|
| 89 | private static synchronized int getNewId() {
|
---|
| 90 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 91 | temporalId = 0;
|
---|
| 92 | }
|
---|
[439] | 93 |
|
---|
[557] | 94 | return temporalId++;
|
---|
| 95 | }
|
---|
[1215] | 96 | |
---|
| 97 | /* (non-Javadoc) |
---|
| 98 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
| 99 | */ |
---|
| 100 | @Override
|
---|
[1146] | 101 | public int getId() {
|
---|
| 102 | return id;
|
---|
[439] | 103 | }
|
---|
[557] | 104 |
|
---|
[1215] | 105 | /* (non-Javadoc)
|
---|
| 106 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
[557] | 107 | */
|
---|
| 108 | @Override
|
---|
| 109 | public String getDescription() {
|
---|
| 110 | return description;
|
---|
[439] | 111 | }
|
---|
[557] | 112 |
|
---|
[1215] | 113 | /* (non-Javadoc) |
---|
[1294] | 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) |
---|
[1215] | 122 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
| 123 | */ |
---|
[557] | 124 | @Override
|
---|
[1146] | 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());
|
---|
[439] | 129 | }
|
---|
[557] | 130 |
|
---|
[1215] | 131 | /* (non-Javadoc) |
---|
| 132 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
| 133 | */ |
---|
[557] | 134 | @Override
|
---|
| 135 | public synchronized int hashCode() {
|
---|
[1146] | 136 | return id;
|
---|
[439] | 137 | }
|
---|
[557] | 138 |
|
---|
[1215] | 139 | /* (non-Javadoc)
|
---|
[557] | 140 | * @see java.lang.Object#toString()
|
---|
| 141 | */
|
---|
| 142 | @Override
|
---|
[1126] | 143 | public synchronized String toString() { |
---|
| 144 | StringBuffer result = new StringBuffer(); |
---|
[1146] | 145 | result.append("task "); |
---|
[1126] | 146 | result.append(id); |
---|
| 147 | |
---|
| 148 | if (description != null) { |
---|
[1146] | 149 | result.append(" ("); |
---|
[1126] | 150 | result.append(description); |
---|
[1146] | 151 | result.append(')'); |
---|
[1126] | 152 | } |
---|
| 153 | |
---|
| 154 | return result.toString();
|
---|
[439] | 155 | }
|
---|
| 156 |
|
---|
[1215] | 157 | /* (non-Javadoc) |
---|
| 158 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
| 159 | */ |
---|
[557] | 160 | @Override
|
---|
[1146] | 161 | public synchronized ITask clone() {
|
---|
| 162 | Task clone = null;
|
---|
[557] | 163 | try {
|
---|
[1146] | 164 | clone = (Task) super.clone();
|
---|
[470] | 165 | }
|
---|
[557] | 166 | catch (CloneNotSupportedException e) {
|
---|
| 167 | // this should never happen. Therefore simply dump the exception
|
---|
| 168 | e.printStackTrace();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | return clone;
|
---|
[467] | 172 | }
|
---|
[439] | 173 |
|
---|
[1146] | 174 | /** |
---|
[1215] | 175 | * <p> |
---|
| 176 | * internally used to set the human readable description of the task |
---|
| 177 | * </p> |
---|
[1146] | 178 | * |
---|
[1215] | 179 | * @param description the new human readable description of the task |
---|
[1146] | 180 | */ |
---|
| 181 | void setDescription(String description) { |
---|
| 182 | this.description = description; |
---|
| 183 | } |
---|
| 184 | |
---|
[1294] | 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 | |
---|
[1157] | 196 | /* (non-Javadoc) |
---|
[1215] | 197 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
[1157] | 198 | */ |
---|
| 199 | @Override |
---|
| 200 | public void accept(ITaskVisitor visitor) { |
---|
| 201 | visitor.visit(this); |
---|
| 202 | } |
---|
| 203 | |
---|
[439] | 204 | }
|
---|