QueueIterator.java

jmud-0.11/src/util/list/QueueIterator.java · 1.2 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

package util.list;

import java.util.NoSuchElementException;

public class QueueIterator implements Iterator {

  private Queue    myQueue = null;
  private ListLink cursor  = null;

  QueueIterator(Queue aQueue) {
    myQueue = aQueue;
    start();
  }

  public void start() {
    cursor = myQueue.getHead();
  }

  public boolean cont() {
    return cursor != null;
  }

  public void next() {
    cursor = cursor.getNext();
  }

  public Object getCurrent() {
    return cursor.getInfo();
  }

  public Object getNext() throws NoSuchElementException {
    if (!hasNext())
      throw new NoSuchElementException();
    
    return cursor.getNext().getInfo();
  }

  public boolean hasNext() {
    return (cont() && cursor.getNext() != null);
  }

  public void insertNext(Object info) {
    cursor.setNext(new ListLink(info, cursor.getNext()));

    ListLink last = myQueue.getTail().getNext();

    if (last != null)
      myQueue.setTail(last);
  }

  public void removeNext() throws NoSuchElementException {
    if (!hasNext())
      throw new NoSuchElementException();

    ListLink next = cursor.getNext();

    if (myQueue.getTail() == next)
      myQueue.setTail(cursor);

    cursor.setNext(next.getNext());
  }
}

Site Stats

Snippets: 1791
Codebases: 571