All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.wala.util.collections.ComposedIterator Maven / Gradle / Ivy

The newest version!
/*
 * 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 org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

/** A 2-level iterator. has not been tested yet! */
public abstract class ComposedIterator implements Iterator {

  private final Iterator outer;
  private @Nullable Iterator inner;

  public ComposedIterator(Iterator outer) {
    this.outer = outer;
    advanceOuter();
  }

  private void advanceOuter() {
    while (outer.hasNext()) {
      inner = makeInner(outer.next());
      if (inner.hasNext()) {
        break;
      }
    }
    if (inner != null && !inner.hasNext()) {
      inner = null;
    }
  }

  public abstract Iterator makeInner(O outer);

  @Override
  public void remove() throws UnsupportedOperationException {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean hasNext() {
    return (inner != null);
  }

  @NullUnmarked
  @Override
  public I next() {
    I result = inner.next();
    if (!inner.hasNext()) {
      advanceOuter();
    }
    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy