Iterator Union of Iterators : Iterator « Collections Data Structure « Java






Iterator Union of Iterators

    
/*
    GNU LESSER GENERAL PUBLIC LICENSE
    Copyright (C) 2006 The Lobo Project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Contact info: lobochief@users.sourceforge.net
*/
/*
 * Created on Jun 9, 2005
 */

import java.util.*;

/**
 * @author J. H. S.
 */
 class CollectionUtilities {
  /**
   * 
   */
  private CollectionUtilities() {
    super();
  }
  
  public static Enumeration getIteratorEnumeration(final Iterator i) {
    return new Enumeration() {
      public boolean hasMoreElements() {
        return i.hasNext();
      }
      
      public Object nextElement() {
        return i.next();
      }
    };
  }

  public static Iterator iteratorUnion(final Iterator[] iterators) {
    return new Iterator() {
      private int iteratorIndex = 0;
      private Iterator current = iterators.length > 0 ? iterators[0] : null; 
      
      public boolean hasNext() {  
        for(;;) {
          if(current == null) {
            return false;
          }
          if(current.hasNext()) {
            return true;
          }
          iteratorIndex++;
          current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex];
        }       
      }
      
      public Object next() {
        for(;;) {
          if(this.current == null) {
            throw new NoSuchElementException();
          }
          try {
            return this.current.next();
          } catch(NoSuchElementException nse) {
            this.iteratorIndex++;
            this.current = this.iteratorIndex >= iterators.length ? null : iterators[this.iteratorIndex];           
          }
        }
      }
      
      public void remove() {
        if(this.current == null) {
          throw new NoSuchElementException();
        }
        this.current.remove();
      }
    };
  }
  
  public static Collection reverse(Collection collection) {
    LinkedList newCollection = new LinkedList();
    Iterator i = collection.iterator();
    while(i.hasNext()) {
      newCollection.addFirst(i.next());
    }
    return newCollection;
  }
  
  public static Iterator singletonIterator(final Object item) {
    return new Iterator() {
      private boolean gotItem = false;
      
      public boolean hasNext() {
        return !this.gotItem;
      }

      public Object next() {
        if(this.gotItem) {
          throw new NoSuchElementException();
        }
        this.gotItem = true;
        return item;
      }

      public void remove() {
        if(!this.gotItem) {
          this.gotItem = true;
        }
        else {
          throw new NoSuchElementException();
        }
      }
    };
  }
}

   
    
    
    
  








Related examples in the same category

1.Listing the Elements of a Collection
2.De-mystify the Iterator interface, showing how to write a simple Iterator for an Array of Objects
3.Iterate over Set
4.Demonstrate iterators.
5.Use the for-each for loop to cycle through a collection.
6.List IteratorList Iterator
7.Iterate a Collection and remove an item (Exception, wrong version)
8.Use an Iterator and remove the item with Iterator.remove()
9.An Iterator wrapper for an Enumeration.
10.EmptyIterator is an iterator which is empty.
11.Implements an java.util.Iterator over any array
12.Treat an Iterator as an Iterable
13.Iterator class for sparse values in an array.
14.Iterator class for values contained in an array range.
15.Array Iterator
16.Cyclic Iteration
17.Create singleton Iterator
18.Empty Iterator
19.An Iterator that wraps a number of Iterators
20.An Iterator to iterate over the elements of an array
21.Sorted Iterator
22.Iterator Utils
23.Linked Iterator
24.Prefetch Iterator
25.Protects an given iterator by preventing calls to remove().
26.An Iterator that returns the elements of a specified array, or other iterators etc.
27.An Iterator wrapper for an Object[], allow us to deal with all array like structures in a consistent manner.
28.An array iterator
29.Adapt iterator to iterable
30.Static utility methods, classes, and abstract classes for iteration.
31.Iterator Collection
32.Convert Iterable to List
33.A singleton null object Iterator implementation.