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