Iterator Collection : Iterator « Collections Data Structure « Java






Iterator Collection

       

// $Id: IteratorCollection.java,v 1.12 2004/11/29 19:10:44 grove Exp $

//package net.ontopia.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * INTERNAL: A wrapper class for presenting a collection view on iterators. The
 * wrapped iterator will be lazily traversed.</p>
 */

public class IteratorCollection<T> implements Collection<T> {

  protected Iterator<T> iterator;
  protected Collection<T> coll;
  protected boolean resolved;
  protected int iter_size = -1;
  protected int max_size = Integer.MAX_VALUE;

  public IteratorCollection(Iterator<T> iterator) {
    this.iterator = iterator;
    coll = new ArrayList<T>();
    if (iterator.hasNext() && max_size > 0)
      resolved = false;
    else
      resolved = true;
  }

  public IteratorCollection(Iterator<T> iterator, int size) {
    this(iterator);
    iter_size = size;
  }

  public IteratorCollection(Iterator<T> iterator, int size, int max_size) {
    this(iterator, size);
    if (max_size < 0)
      this.max_size = 0;
    else
      this.max_size = max_size;
  }

  protected synchronized T nextObject() {
    // Get next object in iterator
    T object = iterator.next();
    // Set resolved flag to true if this was the last object
    int csize = coll.size();
    if (!iterator.hasNext() || csize >= max_size
        || (iter_size > 0 && csize >= iter_size)) {
      resolved = true;
      iterator = null;
    }
    // Add object to collection
    coll.add(object);
    // Return the object
    return object;
  }

  protected synchronized void resolve() {
    while (iterator.hasNext()) {
      int csize = coll.size();
      if (csize >= max_size || (iter_size > 0 && csize >= iter_size))
        break;
      coll.add(iterator.next());
    }
    resolved = true;
    iterator = null;
  }

  public void clear() {
    coll.clear();
    resolved = true;
    iterator = null;
  }

  public boolean contains(Object o) {
    if (coll.contains((T) o))
      return true;
    synchronized (this) {
      while (!resolved) {
        Object object = nextObject();
        if (object == o)
          return true;
      }
    }
    return false;
  }

  public boolean containsAll(Collection<?> c) {
    // FIXME: This one can be improved.
    // If partially resolved collection contains all objects then we're fine
    if (coll.containsAll(c))
      return true;
    // Otherwise resolve iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.containsAll(c);
  }

  public boolean isEmpty() {
    // Collection is empty if; built collection or iterator contains
    // elements
    if (coll.size() > 0)
      return false;
    if (!resolved && iterator.hasNext() && max_size > 0)
      return false;
    return true;
  }

  public Iterator<T> iterator() {
    // FIXME: This can be improved lot by lazily traversing internal
    // iterator.
    if (!resolved)
      resolve();
    return coll.iterator();
  }

  public int size() {
    // If iterator has been resolved return the size of the collection
    if (resolved)
      return coll.size();
    // If iterator has explicit size use that.
    else if (iter_size >= 0) {
      if (iter_size < max_size)
        return iter_size;
      else
        return max_size;
    }
    // Last alternative is to traverse the entire iterator.
    resolve();
    return coll.size();
  }

  // --- Methods that require the entire iterator to be traversed.

  public int hashCode() {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.hashCode();
  }

  public boolean equals(Object o) {
    if (!(o instanceof Collection))
      return false;

    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.equals(o);
  }

  public Object[] toArray() {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.toArray();
  }

  public Object[] toArray(Object[] a) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.toArray(a);
  }

  public boolean add(T o) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.add(o);
  }

  public boolean addAll(Collection c) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.addAll(c);
  }

  public boolean remove(Object o) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.remove(o);
  }

  public boolean removeAll(Collection c) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.removeAll(c);
  }

  public boolean retainAll(Collection c) {
    // Traverse the entire iterator
    if (!resolved)
      resolve();
    // Call method on nested collection
    return coll.retainAll(c);
  }

}

   
    
    
    
    
    
    
  








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 Union of Iterators
23.Iterator Utils
24.Linked Iterator
25.Prefetch Iterator
26.Protects an given iterator by preventing calls to remove().
27.An Iterator that returns the elements of a specified array, or other iterators etc.
28.An Iterator wrapper for an Object[], allow us to deal with all array like structures in a consistent manner.
29.An array iterator
30.Adapt iterator to iterable
31.Static utility methods, classes, and abstract classes for iteration.
32.Convert Iterable to List
33.A singleton null object Iterator implementation.