[1146] | 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 | /**
|
---|
[1216] | 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 |
---|
[1146] | 27 | */
|
---|
| 28 | class TaskInstance implements ITaskInstance { |
---|
| 29 |
|
---|
[1216] | 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * default serial version UID |
---|
| 33 | * </p> |
---|
| 34 | */ |
---|
[1146] | 35 | private static final long serialVersionUID = 1L; |
---|
| 36 | |
---|
[1216] | 37 | /** |
---|
| 38 | * <p> |
---|
| 39 | * used as a counter to generate new ids for each newly created task instance. May overflow. |
---|
| 40 | * </p> |
---|
| 41 | */ |
---|
[1146] | 42 | private static int temporalId = 0;
|
---|
| 43 | |
---|
[1216] | 44 | /** |
---|
| 45 | * <p> |
---|
| 46 | * the task instantiated by this task instance |
---|
| 47 | * </p> |
---|
| 48 | */ |
---|
[1146] | 49 | private ITask task; |
---|
| 50 |
|
---|
[1216] | 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 | */ |
---|
[1146] | 57 | private int id;
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
[1216] | 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>
|
---|
[1146] | 64 | */
|
---|
| 65 | TaskInstance(ITask task) {
|
---|
| 66 | this.task = task;
|
---|
| 67 | id = getNewId();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
[1216] | 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 |
---|
[1146] | 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 |
|
---|
[1216] | 106 | /* (non-Javadoc)
|
---|
[1146] | 107 | * @see java.lang.Object#hashCode()
|
---|
| 108 | */
|
---|
| 109 | @Override
|
---|
| 110 | public synchronized int hashCode() {
|
---|
| 111 | return id;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[1216] | 114 | /* (non-Javadoc)
|
---|
[1146] | 115 | * @see java.lang.Object#toString()
|
---|
| 116 | */
|
---|
| 117 | @Override
|
---|
| 118 | public synchronized String toString() { |
---|
| 119 | StringBuffer result = new StringBuffer(); |
---|
| 120 | result.append("task "); |
---|
| 121 | result.append(task.getId()); |
---|
[1255] | 122 | result.append(" (#"); |
---|
[1146] | 123 | result.append(id); |
---|
| 124 | |
---|
| 125 | if (task.getDescription() != null) { |
---|
| 126 | result.append(", "); |
---|
| 127 | result.append(task.getDescription()); |
---|
| 128 | } |
---|
| 129 | |
---|
[1255] | 130 | /*if (children != null) { |
---|
[1146] | 131 | result.append(", "); |
---|
| 132 | result.append(children.size()); |
---|
| 133 | result.append(" children"); |
---|
[1255] | 134 | }*/ |
---|
[1146] | 135 | |
---|
| 136 | result.append(')'); |
---|
| 137 | return result.toString();
|
---|
| 138 | }
|
---|
| 139 | |
---|
[1216] | 140 | /* (non-Javadoc) |
---|
[1146] | 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 | /** |
---|
[1216] | 158 | * <p> |
---|
| 159 | * used to update the task represented through this instance |
---|
| 160 | * </p> |
---|
| 161 | * |
---|
[1146] | 162 | * @param task the task to set |
---|
| 163 | */ |
---|
| 164 | void setTask(ITask task) { |
---|
| 165 | this.task = task; |
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | }
|
---|