[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 java.util.Collections; |
---|
| 18 | import java.util.Iterator; |
---|
| 19 | import java.util.LinkedList; |
---|
| 20 | import java.util.List; |
---|
| 21 | |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * <p> |
---|
[1216] | 27 | * this is the default implementation of the interface {@link IUserSession}. It |
---|
| 28 | * does not do anything fancy except implementing the interface. |
---|
[1146] | 29 | * </p> |
---|
| 30 | * |
---|
| 31 | * @author Patrick Harms |
---|
| 32 | */ |
---|
| 33 | class UserSession implements IUserSession { |
---|
| 34 | |
---|
[1216] | 35 | /** |
---|
| 36 | * <p> |
---|
| 37 | * default serial version UID |
---|
| 38 | * </p> |
---|
| 39 | */ |
---|
[1146] | 40 | private static final long serialVersionUID = 1L; |
---|
[1216] | 41 | |
---|
[1146] | 42 | /** |
---|
[1216] | 43 | * <p> |
---|
| 44 | * the task instances belonging to the user session |
---|
| 45 | * </p> |
---|
[1146] | 46 | */ |
---|
| 47 | private List<ITaskInstance> executedTasks = new LinkedList<ITaskInstance>(); |
---|
| 48 | |
---|
| 49 | /* (non-Javadoc) |
---|
| 50 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int) |
---|
| 51 | */ |
---|
| 52 | @Override |
---|
| 53 | public ITaskInstance get(int index) { |
---|
| 54 | return executedTasks.get(index); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /* (non-Javadoc) |
---|
| 58 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size() |
---|
| 59 | */ |
---|
| 60 | @Override |
---|
| 61 | public int size() { |
---|
| 62 | return executedTasks.size(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /* (non-Javadoc) |
---|
| 66 | * @see java.lang.Iterable#iterator() |
---|
| 67 | */ |
---|
| 68 | @Override |
---|
| 69 | public Iterator<ITaskInstance> iterator() { |
---|
| 70 | return executedTasks.iterator(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /* (non-Javadoc) |
---|
| 74 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession#getExecutedTasks() |
---|
| 75 | */ |
---|
| 76 | @Override |
---|
| 77 | public List<ITaskInstance> getExecutedTasks() { |
---|
| 78 | return Collections.unmodifiableList(executedTasks); |
---|
| 79 | } |
---|
| 80 | |
---|
[1216] | 81 | /* (non-Javadoc) |
---|
| 82 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession#equals(IUserSession) |
---|
[1146] | 83 | */ |
---|
| 84 | @Override |
---|
| 85 | public boolean equals(IUserSession userSession) { |
---|
| 86 | // task instances are only equal if they are identical or if they have the same id |
---|
| 87 | // (may happen, if they are cloned) |
---|
| 88 | return (this == userSession) || (this.hashCode() == userSession.hashCode()); |
---|
| 89 | } |
---|
| 90 | |
---|
[1216] | 91 | /* (non-Javadoc) |
---|
[1146] | 92 | * @see java.lang.Object#hashCode() |
---|
| 93 | */ |
---|
| 94 | @Override |
---|
| 95 | public synchronized int hashCode() { |
---|
| 96 | return super.hashCode(); |
---|
| 97 | } |
---|
| 98 | |
---|
[1216] | 99 | /* (non-Javadoc) |
---|
[1146] | 100 | * @see java.lang.Object#toString() |
---|
| 101 | */ |
---|
| 102 | @Override |
---|
| 103 | public synchronized String toString() { |
---|
| 104 | return "session (" + executedTasks.size() + " task instances)"; |
---|
| 105 | } |
---|
| 106 | |
---|
[1216] | 107 | /* (non-Javadoc) |
---|
[1146] | 108 | * @see java.lang.Object#clone() |
---|
| 109 | */ |
---|
| 110 | @Override |
---|
| 111 | public synchronized IUserSession clone() { |
---|
| 112 | UserSession clone = null; |
---|
| 113 | try { |
---|
| 114 | clone = (UserSession) super.clone(); |
---|
| 115 | |
---|
| 116 | clone.executedTasks = new LinkedList<ITaskInstance>(); |
---|
| 117 | |
---|
| 118 | for (ITaskInstance child : executedTasks) { |
---|
| 119 | clone.executedTasks.add(child.clone()); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | } |
---|
| 123 | catch (CloneNotSupportedException e) { |
---|
| 124 | // this should never happen. Therefore simply dump the exception |
---|
| 125 | e.printStackTrace(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return clone; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[1216] | 132 | * <p> |
---|
| 133 | * used internally to add a task instance to the user session |
---|
| 134 | * </p> |
---|
| 135 | * |
---|
| 136 | * @param taskInstance the task instance to be added |
---|
[1146] | 137 | */ |
---|
| 138 | void addExecutedTask(ITaskInstance taskInstance) { |
---|
| 139 | executedTasks.add(taskInstance); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
[1216] | 143 | * <p> |
---|
| 144 | * used internally to add a task instance to the user session at a specific position |
---|
| 145 | * </p> |
---|
| 146 | * |
---|
| 147 | * @param index the index the task instance shall be added to |
---|
| 148 | * @param taskInstance the task instance to be added |
---|
[1146] | 149 | */ |
---|
| 150 | void addExecutedTask(int index, ITaskInstance taskInstance) { |
---|
| 151 | executedTasks.add(index, taskInstance); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
[1216] | 155 | * <p> |
---|
| 156 | * used internally to remove a task instance from the user session |
---|
| 157 | * </p> |
---|
| 158 | * |
---|
| 159 | * @param index the index of the task instance to be removed |
---|
[1146] | 160 | */ |
---|
| 161 | void removeExecutedTask(int index) { |
---|
| 162 | executedTasks.remove(index); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | } |
---|