/*******************************************************************************
 * Copyright (c) 2002 - 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.ibm.wala.util.collections;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A <code>FilterIterator</code> filters an <code>Iterator</code> to generate a new one.
 */
public class FilterIterator<T> implements java.util.Iterator<T> {
  final Iterator<?> i;

  @SuppressWarnings("rawtypes")
  final Filter f;

  private T next = null;

  private boolean done = false;

  /**
   * @param i the original iterator
   * @param f a filter which defines which elements belong to the generated iterator
   */
  @SuppressWarnings("rawtypes")
  public FilterIterator(Iterator<?> i, Filter f) {
    if (i == null) {
      throw new IllegalArgumentException("null i");
    }
    if (f == null) {
      throw new IllegalArgumentException("null f");
    }
    this.i = i;
    this.f = f;
    advance();
  }

  /**
   * update the internal state to prepare for the next access to this iterator
   */
  @SuppressWarnings("unchecked")
  private void advance() {
    while (i.hasNext()) {
      Object o = i.next();
      if (f.accepts(o)) {
        next = (T) o;
        return;
      }
    }
    done = true;
  }

  public T next() throws NoSuchElementException {
    if (done) {
      throw new java.util.NoSuchElementException();
    }
    T o = next;
    advance();
    return o;
  }

  public boolean hasNext() {
    return !done;
  }

  public void remove() throws UnsupportedOperationException {
    throw new java.lang.UnsupportedOperationException();
  }

  @Override
  public String toString() {
    return "filter " + f + " of " + i;
  }
}