From 5764a6b6b494e977818f0314a964f809557ac1a1 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 7 Nov 2002 18:40:40 +0000 Subject: [PATCH] moved access to IteratorIterator into Collections.getMetaIterator() git-svn-id: https://samskivert.googlecode.com/svn/trunk@894 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/Collections.java | 66 ++++++++++++++++++- .../com/samskivert/util/IteratorIterator.java | 63 ------------------ 2 files changed, 65 insertions(+), 64 deletions(-) delete mode 100644 projects/samskivert/src/java/com/samskivert/util/IteratorIterator.java diff --git a/projects/samskivert/src/java/com/samskivert/util/Collections.java b/projects/samskivert/src/java/com/samskivert/util/Collections.java index e5fc25ff..643a4306 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Collections.java +++ b/projects/samskivert/src/java/com/samskivert/util/Collections.java @@ -1,5 +1,5 @@ // -// $Id: Collections.java,v 1.3 2002/05/24 21:31:56 mdb Exp $ +// $Id: Collections.java,v 1.4 2002/11/07 18:40:40 ray Exp $ package com.samskivert.util; @@ -14,6 +14,15 @@ import java.util.*; */ public class Collections { + /** + * Returns an iterator that iterates over all the elements contained + * within the collections within the specified collection. + */ + public static Iterator getMetaIterator (Collection metaCollection) + { + return new MetaIterator(metaCollection); + } + /** * Returns a synchronized (thread-safe) int map backed by the * specified int map. In order to guarantee serial access, it is @@ -257,4 +266,59 @@ public class Collections synchronized(mutex) {return c.toString();} } } + + /** + * An iterator that iterates over the union of the iterators provided by a + * collection of collections. + */ + protected static class MetaIterator implements Iterator + { + /** + * @param collections a Collection containing more Collections + * whose elements we are to iterate over. + */ + public MetaIterator (Collection collections) + { + _meta = collections.iterator(); + } + + // documentation inherited from interface Iterator + public boolean hasNext () + { + while ((_current == null) || (!_current.hasNext())) { + if (_meta.hasNext()) { + _current = ((Collection) _meta.next()).iterator(); + } else { + return false; + } + } + return true; + } + + // documentation inherited from interface Iterator + public Object next () + { + if (hasNext()) { + return _current.next(); + } else { + throw new NoSuchElementException(); + } + } + + // documentation inherited from interface Iterator + public void remove () + { + if (_current != null) { + _current.remove(); + } else { + throw new IllegalStateException(); + } + } + + /** The iterator through the collection we were constructed with. */ + protected Iterator _meta; + + /** The current sub-collection's iterator. */ + protected Iterator _current; + } } diff --git a/projects/samskivert/src/java/com/samskivert/util/IteratorIterator.java b/projects/samskivert/src/java/com/samskivert/util/IteratorIterator.java deleted file mode 100644 index 148b93f1..00000000 --- a/projects/samskivert/src/java/com/samskivert/util/IteratorIterator.java +++ /dev/null @@ -1,63 +0,0 @@ -// -// $Id: IteratorIterator.java,v 1.1 2002/11/07 05:58:31 mdb Exp $ - -package com.samskivert.util; - -import java.util.Collection; -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * An iterator that can iterate over the elements in several collections - * contained within a master collection. - */ -public class IteratorIterator implements Iterator -{ - /** - * @param collections a Collection containing more Collections - * whose elements we are to iterate over. - */ - public IteratorIterator (Collection collections) - { - _meta = collections.iterator(); - } - - // documentation inherited from interface Iterator - public boolean hasNext () - { - while ((_current == null) || (!_current.hasNext())) { - if (_meta.hasNext()) { - _current = ((Collection) _meta.next()).iterator(); - } else { - return false; - } - } - return true; - } - - // documentation inherited from interface Iterator - public Object next () - { - if (hasNext()) { - return _current.next(); - } else { - throw new NoSuchElementException(); - } - } - - // documentation inherited from interface Iterator - public void remove () - { - if (_current != null) { - _current.remove(); - } else { - throw new IllegalStateException(); - } - } - - /** The iterator through the collection we were constructed with. */ - protected Iterator _meta; - - /** The current sub-collection's iterator. */ - protected Iterator _current; -}