NiceQueueIterator.java

jmud-0.11/src/util/nice/NiceQueueIterator.java · 1.2 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

package util.nice;

import java.util.NoSuchElementException;

public class NiceQueueIterator implements NiceIterator {

  private NiceQueue    myQueue = null;
  private NiceListLink cursor  = null;

  NiceQueueIterator(NiceQueue aQueue) {
    myQueue = aQueue;
    start();
  }

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

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

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

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

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

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

  public void insertNext(Niceable info) {
    cursor.setNext(new NiceListLink(info, cursor.getNext()));

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

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

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

    NiceListLink next = cursor.getNext();

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

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

Site Stats

Snippets: 1791
Codebases: 571