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 | /* (non-Javadoc) |
---|
51 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int) |
---|
52 | */ |
---|
53 | @Override |
---|
54 | public ITaskInstance get(int index) { |
---|
55 | return executedTasks.get(index); |
---|
56 | } |
---|
57 | |
---|
58 | /* (non-Javadoc) |
---|
59 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size() |
---|
60 | */ |
---|
61 | @Override |
---|
62 | public int size() { |
---|
63 | return executedTasks.size(); |
---|
64 | } |
---|
65 | |
---|
66 | /* (non-Javadoc) |
---|
67 | * @see java.lang.Iterable#iterator() |
---|
68 | */ |
---|
69 | @Override |
---|
70 | public Iterator<ITaskInstance> iterator() { |
---|
71 | return executedTasks.iterator(); |
---|
72 | } |
---|
73 | |
---|
74 | /* (non-Javadoc) |
---|
75 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession#getExecutedTasks() |
---|
76 | */ |
---|
77 | @Override |
---|
78 | public List<ITaskInstance> getExecutedTasks() { |
---|
79 | return Collections.unmodifiableList(executedTasks); |
---|
80 | } |
---|
81 | |
---|
82 | /* (non-Javadoc) |
---|
83 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession#equals(IUserSession) |
---|
84 | */ |
---|
85 | @Override |
---|
86 | public boolean equals(IUserSession userSession) { |
---|
87 | // task instances are only equal if they are identical or if they have the same id |
---|
88 | // (may happen, if they are cloned) |
---|
89 | return (this == userSession) || (this.hashCode() == userSession.hashCode()); |
---|
90 | } |
---|
91 | |
---|
92 | /* (non-Javadoc) |
---|
93 | * @see java.lang.Object#hashCode() |
---|
94 | */ |
---|
95 | @Override |
---|
96 | public synchronized int hashCode() { |
---|
97 | return super.hashCode(); |
---|
98 | } |
---|
99 | |
---|
100 | /* (non-Javadoc) |
---|
101 | * @see java.lang.Object#toString() |
---|
102 | */ |
---|
103 | @Override |
---|
104 | public synchronized String toString() { |
---|
105 | return "session (" + executedTasks.size() + " task instances)"; |
---|
106 | } |
---|
107 | |
---|
108 | /* (non-Javadoc) |
---|
109 | * @see java.lang.Object#clone() |
---|
110 | */ |
---|
111 | @Override |
---|
112 | public synchronized IUserSession clone() { |
---|
113 | UserSession clone = null; |
---|
114 | try { |
---|
115 | clone = (UserSession) super.clone(); |
---|
116 | |
---|
117 | clone.executedTasks = new LinkedList<ITaskInstance>(); |
---|
118 | |
---|
119 | for (ITaskInstance child : executedTasks) { |
---|
120 | clone.executedTasks.add(child.clone()); |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | catch (CloneNotSupportedException e) { |
---|
125 | // this should never happen. Therefore simply dump the exception |
---|
126 | e.printStackTrace(); |
---|
127 | } |
---|
128 | |
---|
129 | return clone; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * <p> |
---|
134 | * used internally to add a task instance to the user session |
---|
135 | * </p> |
---|
136 | * |
---|
137 | * @param taskInstance the task instance to be added |
---|
138 | */ |
---|
139 | void addExecutedTask(ITaskInstance taskInstance) { |
---|
140 | executedTasks.add(taskInstance); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * <p> |
---|
145 | * used internally to add a task instance to the user session at a specific position |
---|
146 | * </p> |
---|
147 | * |
---|
148 | * @param index the index the task instance shall be added to |
---|
149 | * @param taskInstance the task instance to be added |
---|
150 | */ |
---|
151 | void addExecutedTask(int index, ITaskInstance taskInstance) { |
---|
152 | executedTasks.add(index, taskInstance); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * <p> |
---|
157 | * used internally to remove a task instance from the user session |
---|
158 | * </p> |
---|
159 | * |
---|
160 | * @param index the index of the task instance to be removed |
---|
161 | */ |
---|
162 | void removeExecutedTask(int index) { |
---|
163 | executedTasks.remove(index); |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|