01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/util/tags/sakai_2-4-1/util-util/util/src/java/org/sakaiproject/util/SortedIterator.java $
03: * $Id: SortedIterator.java 6836 2006-03-21 21:18:46Z ggolden@umich.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.util;
21:
22: import java.util.Iterator;
23: import java.util.Comparator;
24: import java.util.Vector;
25: import java.util.Collections;
26:
27: /**
28: * <p>
29: * SortedIterator is a wrapper iterator that iterates over the wrapped iterator in a sorted order, the order controlled by a Comparator function provided at construction.
30: * </p>
31: */
32: public class SortedIterator implements Iterator {
33: /** The sorted iterator. */
34: protected Iterator m_iterator = null;
35:
36: /**
37: * Creates new SortedIterator based on the base iterator and the comparator function
38: *
39: * @param aIterator
40: * The original Iterator
41: * @param aComparator
42: * The comparator object
43: */
44: public SortedIterator(Iterator iterator, Comparator comparator) {
45: // construct a collection (Vector) from the base iterator so we can sort
46: Vector collection = new Vector();
47: while (iterator.hasNext()) {
48: collection.add(iterator.next());
49: }
50:
51: // sort the collection based on comparator
52: Collections.sort(collection, comparator);
53:
54: // remember the final sorted iterator
55: m_iterator = collection.iterator();
56: }
57:
58: /**
59: * Returns true if the iteration has more elements.
60: *
61: * @return True if the iteration has more elements; False otherwise.
62: */
63: public boolean hasNext() {
64: return m_iterator.hasNext();
65: }
66:
67: /**
68: * Returns the next element in the iteration.
69: *
70: * @return The next element in the iteration
71: */
72: public Object next() {
73: return m_iterator.next();
74: }
75:
76: /**
77: * Removes from the underlying collection the last element returned by the iterator (optional operation) (not supported).
78: */
79: public void remove() {
80: throw new UnsupportedOperationException();
81: }
82: }
|