source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeifc/TaskPath.java @ 1767

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