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.treeifc; |
---|
16 | |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | /** |
---|
21 | * <p> |
---|
22 | * TODO comment |
---|
23 | * </p> |
---|
24 | * |
---|
25 | * @author Patrick Harms |
---|
26 | */ |
---|
27 | public class TaskPath { |
---|
28 | |
---|
29 | /** */ |
---|
30 | private List<Entry> taskList; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * |
---|
35 | */ |
---|
36 | public TaskPath() { |
---|
37 | super(); |
---|
38 | taskList = new ArrayList<Entry>(); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * |
---|
43 | */ |
---|
44 | public TaskPath(TaskPath path) { |
---|
45 | this(); |
---|
46 | taskList.addAll(path.taskList); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * |
---|
51 | */ |
---|
52 | public TaskPath subPath(int fromIndex, int toIndex) { |
---|
53 | TaskPath result = new TaskPath(); |
---|
54 | result.taskList.addAll(taskList.subList(fromIndex, toIndex)); |
---|
55 | return result; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | */ |
---|
61 | public ITask removeLast() { |
---|
62 | Entry last = taskList.remove(taskList.size() - 1); |
---|
63 | if (last != null) { |
---|
64 | return last.getTask(); |
---|
65 | } |
---|
66 | else { |
---|
67 | return null; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * |
---|
73 | */ |
---|
74 | public ITask getLast() { |
---|
75 | Entry last = taskList.get(taskList.size() - 1); |
---|
76 | if (last != null) { |
---|
77 | return last.getTask(); |
---|
78 | } |
---|
79 | else { |
---|
80 | return null; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /* (non-Javadoc) |
---|
85 | * @see java.lang.Object#toString() |
---|
86 | */ |
---|
87 | @Override |
---|
88 | public String toString() { |
---|
89 | return taskList.toString(); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * |
---|
94 | */ |
---|
95 | public int size() { |
---|
96 | return taskList.size(); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * |
---|
101 | */ |
---|
102 | public boolean isEmpty() { |
---|
103 | return taskList.isEmpty(); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * |
---|
108 | */ |
---|
109 | public void add(ITask task, int index) { |
---|
110 | taskList.add(new Entry(task, index)); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * |
---|
115 | */ |
---|
116 | public ITask getTask(int index) { |
---|
117 | Entry last = taskList.get(index); |
---|
118 | if (last != null) { |
---|
119 | return last.getTask(); |
---|
120 | } |
---|
121 | else { |
---|
122 | return null; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * |
---|
128 | */ |
---|
129 | public Entry get(int index) { |
---|
130 | return taskList.get(index); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /* (non-Javadoc) |
---|
135 | * @see java.lang.Object#hashCode() |
---|
136 | */ |
---|
137 | @Override |
---|
138 | public int hashCode() { |
---|
139 | ITask last = getLast(); |
---|
140 | if (last != null) { |
---|
141 | return last.hashCode(); |
---|
142 | } |
---|
143 | else { |
---|
144 | return 0; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | /* (non-Javadoc) |
---|
149 | * @see java.lang.Object#equals(java.lang.Object) |
---|
150 | */ |
---|
151 | @Override |
---|
152 | public boolean equals(Object obj) { |
---|
153 | if (this == obj) { |
---|
154 | return true; |
---|
155 | } |
---|
156 | else if (obj instanceof TaskPath) { |
---|
157 | TaskPath other = (TaskPath) obj; |
---|
158 | if (this.size() == other.size()) { |
---|
159 | for (int i = 0; i < this.size(); i++) { |
---|
160 | if (!this.get(i).equals(other.get(i))) { |
---|
161 | return false; |
---|
162 | } |
---|
163 | } |
---|
164 | return true; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | * |
---|
174 | */ |
---|
175 | public static class Entry { |
---|
176 | |
---|
177 | /** */ |
---|
178 | private ITask task; |
---|
179 | |
---|
180 | /** */ |
---|
181 | private int index; |
---|
182 | |
---|
183 | /** |
---|
184 | * |
---|
185 | */ |
---|
186 | private Entry(ITask task, int index) { |
---|
187 | this.task = task; |
---|
188 | this.index = index; |
---|
189 | } |
---|
190 | |
---|
191 | /* (non-Javadoc) |
---|
192 | * @see java.lang.Object#toString() |
---|
193 | */ |
---|
194 | @Override |
---|
195 | public String toString() { |
---|
196 | return "(" + task + "," + index + ")"; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * @return the task |
---|
201 | */ |
---|
202 | public ITask getTask() { |
---|
203 | return task; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * @return the index |
---|
208 | */ |
---|
209 | public int getIndex() { |
---|
210 | return index; |
---|
211 | } |
---|
212 | |
---|
213 | /* (non-Javadoc) |
---|
214 | * @see java.lang.Object#hashCode() |
---|
215 | */ |
---|
216 | @Override |
---|
217 | public int hashCode() { |
---|
218 | return task.hashCode(); |
---|
219 | } |
---|
220 | |
---|
221 | /* (non-Javadoc) |
---|
222 | * @see java.lang.Object#equals(java.lang.Object) |
---|
223 | */ |
---|
224 | @Override |
---|
225 | public boolean equals(Object obj) { |
---|
226 | if (this == obj) { |
---|
227 | return true; |
---|
228 | } |
---|
229 | else if (obj instanceof TaskPath.Entry) { |
---|
230 | return (this.getIndex() == ((TaskPath.Entry) obj).getIndex()) && |
---|
231 | (this.getTask().equals(((TaskPath.Entry) obj).getTask())); |
---|
232 | } |
---|
233 | |
---|
234 | return false; |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|