[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 |
|
---|
[1146] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1157] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor; |
---|
[439] | 19 |
|
---|
| 20 | /**
|
---|
[1215] | 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 |
---|
[439] | 27 | */
|
---|
[1146] | 28 | class Task implements ITask {
|
---|
| 29 | |
---|
[1215] | 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * default serial version UID |
---|
| 33 | * </p> |
---|
| 34 | */ |
---|
[1146] | 35 | private static final long serialVersionUID = 1L; |
---|
| 36 | |
---|
[1215] | 37 | /** |
---|
| 38 | * <p> |
---|
| 39 | * used as a counter to generate new ids for each newly created task. May overflow. |
---|
| 40 | * </p> |
---|
| 41 | */
|
---|
[557] | 42 | private static int temporalId = 0;
|
---|
[439] | 43 |
|
---|
[1215] | 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 | */
|
---|
[1146] | 50 | private int id;
|
---|
[439] | 51 |
|
---|
[1215] | 52 | /** |
---|
| 53 | * <p> |
---|
| 54 | * a human readable description of the task |
---|
| 55 | * </p> |
---|
| 56 | */
|
---|
[557] | 57 | private String description;
|
---|
[439] | 58 |
|
---|
[557] | 59 | /**
|
---|
[1215] | 60 | * <p> |
---|
| 61 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
| 62 | * methdod |
---|
| 63 | * </p>
|
---|
[557] | 64 | */
|
---|
[1146] | 65 | Task() {
|
---|
[557] | 66 | id = getNewId();
|
---|
[439] | 67 | }
|
---|
| 68 |
|
---|
[557] | 69 | /**
|
---|
[1215] | 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>
|
---|
[557] | 74 | *
|
---|
[1215] | 75 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
[557] | 76 | */
|
---|
| 77 | private static synchronized int getNewId() {
|
---|
| 78 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 79 | temporalId = 0;
|
---|
| 80 | }
|
---|
[439] | 81 |
|
---|
[557] | 82 | return temporalId++;
|
---|
| 83 | }
|
---|
[1215] | 84 | |
---|
| 85 | /* (non-Javadoc) |
---|
| 86 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
| 87 | */ |
---|
| 88 | @Override
|
---|
[1146] | 89 | public int getId() {
|
---|
| 90 | return id;
|
---|
[439] | 91 | }
|
---|
[557] | 92 |
|
---|
[1215] | 93 | /* (non-Javadoc)
|
---|
| 94 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
[557] | 95 | */
|
---|
| 96 | @Override
|
---|
| 97 | public String getDescription() {
|
---|
| 98 | return description;
|
---|
[439] | 99 | }
|
---|
[557] | 100 |
|
---|
[1215] | 101 | /* (non-Javadoc) |
---|
| 102 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
| 103 | */ |
---|
[557] | 104 | @Override
|
---|
[1146] | 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());
|
---|
[439] | 109 | }
|
---|
[557] | 110 |
|
---|
[1215] | 111 | /* (non-Javadoc) |
---|
| 112 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
| 113 | */ |
---|
[557] | 114 | @Override
|
---|
| 115 | public synchronized int hashCode() {
|
---|
[1146] | 116 | return id;
|
---|
[439] | 117 | }
|
---|
[557] | 118 |
|
---|
[1215] | 119 | /* (non-Javadoc)
|
---|
[557] | 120 | * @see java.lang.Object#toString()
|
---|
| 121 | */
|
---|
| 122 | @Override
|
---|
[1126] | 123 | public synchronized String toString() { |
---|
| 124 | StringBuffer result = new StringBuffer(); |
---|
[1146] | 125 | result.append("task "); |
---|
[1126] | 126 | result.append(id); |
---|
| 127 | |
---|
| 128 | if (description != null) { |
---|
[1146] | 129 | result.append(" ("); |
---|
[1126] | 130 | result.append(description); |
---|
[1146] | 131 | result.append(')'); |
---|
[1126] | 132 | } |
---|
| 133 | |
---|
| 134 | return result.toString();
|
---|
[439] | 135 | }
|
---|
| 136 |
|
---|
[1215] | 137 | /* (non-Javadoc) |
---|
| 138 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
| 139 | */ |
---|
[557] | 140 | @Override
|
---|
[1146] | 141 | public synchronized ITask clone() {
|
---|
| 142 | Task clone = null;
|
---|
[557] | 143 | try {
|
---|
[1146] | 144 | clone = (Task) super.clone();
|
---|
[470] | 145 | }
|
---|
[557] | 146 | catch (CloneNotSupportedException e) {
|
---|
| 147 | // this should never happen. Therefore simply dump the exception
|
---|
| 148 | e.printStackTrace();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return clone;
|
---|
[467] | 152 | }
|
---|
[439] | 153 |
|
---|
[1146] | 154 | /** |
---|
[1215] | 155 | * <p> |
---|
| 156 | * internally used to set the human readable description of the task |
---|
| 157 | * </p> |
---|
[1146] | 158 | * |
---|
[1215] | 159 | * @param description the new human readable description of the task |
---|
[1146] | 160 | */ |
---|
| 161 | void setDescription(String description) { |
---|
| 162 | this.description = description; |
---|
| 163 | } |
---|
| 164 | |
---|
[1157] | 165 | /* (non-Javadoc) |
---|
[1215] | 166 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
[1157] | 167 | */ |
---|
| 168 | @Override |
---|
| 169 | public void accept(ITaskVisitor visitor) { |
---|
| 170 | visitor.visit(this); |
---|
| 171 | } |
---|
| 172 | |
---|
[439] | 173 | }
|
---|