001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit;
025:
026: import java.util.Iterator;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.NoSuchElementException;
030:
031: /**
032: * List for elements which have a priority.
033: *
034: * <p>
035: * If o1 and o2 are objects in the priority list with the priorities p(o1) and p(o2), the following
036: * statement is true: p(o1) < p(o2) => index(o1) > index(02). Objects with the same
037: * priority are ordered randomly.
038: * </p>
039: * If an object is added with no priority or a priority less than zero, then its priority is set to
040: * -1 (which stands for the lowest priority).
041: *
042: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
043: * @version 1.0
044: */
045: public class PriorityList {
046: /** The internal list. */
047: private List list;
048:
049: /**
050: * Creates a new Priority List.
051: */
052: public PriorityList() {
053: list = new LinkedList();
054: }
055:
056: /**
057: * Add an element with a specified priority.
058: *
059: * @param element the element to add
060: * @param priority the priority
061: */
062: public void add(Object element, int priority) {
063: PriorityNode p = new PriorityNode(element, priority);
064:
065: if (priority < 0) {
066: priority = -1;
067: }
068:
069: if (list.isEmpty()) {
070: list.add(p);
071: } else {
072: for (int i = 0; i < list.size(); i++) {
073: PriorityNode t = (PriorityNode) list.get(i);
074:
075: if (t.getPriority() < p.getPriority()) {
076: list.add(i, p);
077:
078: return;
079: }
080: }
081:
082: list.add(p);
083: }
084: }
085:
086: /**
087: * Add an element with the lowest priority.
088: *
089: * @param element the element to add
090: */
091: public void add(Object element) {
092: add(element, -1);
093: }
094:
095: /**
096: * Removes an element.
097: *
098: * @param element the element to remove
099: */
100: public void remove(Object element) {
101: for (int i = 0; i < list.size(); i++) {
102: if (get(i) == element) {
103: list.remove(i);
104:
105: break;
106: }
107: }
108: }
109:
110: /**
111: * Returns the priority of a given object.
112: *
113: * @param object the object
114: *
115: * @return the priority, or <code>-2</code> if the object is not contained.
116: */
117: public int getPriority(Object object) {
118: int result = -2;
119: Iterator it = list.iterator();
120:
121: while (it.hasNext()) {
122: PriorityNode node = (PriorityNode) it.next();
123:
124: if (node.getElement() == object) {
125: result = node.getPriority();
126:
127: break;
128: }
129: }
130:
131: return result;
132: }
133:
134: /**
135: * Returns an element at the specified position.
136: *
137: * @param position the position of the desired element
138: *
139: * @return the element
140: */
141: public Object get(int position) {
142: return ((PriorityNode) list.get(position)).getElement();
143: }
144:
145: /**
146: * Returns the size of the list.
147: *
148: * @return the size
149: */
150: public int size() {
151: return list.size();
152: }
153:
154: /**
155: * Returns whether an object is contained in the list or not.
156: *
157: * @param element the element to be tested
158: *
159: * @return <code>true</code> if the element is contained in the list, otherwise
160: * <code>false</code>
161: */
162: public boolean contains(Object element) {
163: for (int i = 0; i < list.size(); i++) {
164: if (get(i) == element) {
165: return true;
166: }
167: }
168:
169: return false;
170: }
171:
172: /**
173: * Returns an iterator on the list. The element with the highest priority comes first.
174: *
175: * @return the iterator
176: */
177: public Iterator iterator() {
178: return new PriorityIterator();
179: }
180:
181: /**
182: * A list node.
183: */
184: static class PriorityNode {
185: //~ Instance fields ------------------------------------------------------------------------
186:
187: private Object element;
188: private int priority;
189:
190: //~ Constructors ---------------------------------------------------------------------------
191:
192: public PriorityNode(Object element, int priority) {
193: setElement(element);
194: setPriority(priority);
195: }
196:
197: //~ Methods --------------------------------------------------------------------------------
198:
199: private void setElement(Object element) {
200: this .element = element;
201: }
202:
203: private void setPriority(int priority) {
204: this .priority = priority;
205: }
206:
207: public Object getElement() {
208: return element;
209: }
210:
211: public int getPriority() {
212: return priority;
213: }
214: }
215:
216: /**
217: * An iterator.
218: */
219: public class PriorityIterator implements Iterator {
220: //~ Instance fields ------------------------------------------------------------------------
221:
222: private int index;
223:
224: //~ Constructors ---------------------------------------------------------------------------
225:
226: public PriorityIterator() {
227: this .index = 0;
228: }
229:
230: //~ Methods --------------------------------------------------------------------------------
231:
232: public boolean hasNext() {
233: return index < list.size();
234: }
235:
236: public Object next() throws NoSuchElementException {
237: if (index >= list.size()) {
238: throw new NoSuchElementException();
239: }
240:
241: return ((PriorityNode) list.get(index++)).getElement();
242: }
243:
244: public void remove() {
245: throw new UnsupportedOperationException();
246: }
247: }
248: }
|