NiceQueue.java

jmud-0.11/src/util/nice/NiceQueue.java · 1.7 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 NiceQueue extends NiceList {

  private NiceListLink tail = null;

  public NiceQueue() {
  }

  private void add(NiceListLink aLink) {
    if (tail != null)
      tail.setNext(aLink);
    tail = aLink;
    if (head == null)
      head = tail;
  }

  public void insert(Niceable info) {
    add(new NiceListLink(info, null));
  }

  NiceListLink getTail() {
    return tail;
  }

  void setTail(NiceListLink t) {
    tail = t;
  }

  private NiceListLink findLastLink() {
    if (head == null)
      return null;

    NiceListLink curr = head;

    while (curr.getNext() != null)
      curr = curr.getNext();

    return curr;
  }

  public void remove(Niceable info) throws NoSuchElementException {
    super.remove(info);

    if (head == null)
      tail = null;
    else if (tail.getInfo() == info) 
      tail = findLastLink();
  }

  public Niceable remove() throws NoSuchElementException {
    if (head == null)
      throw new NoSuchElementException();

    Niceable info = head.getInfo();
    head = head.getNext();

    if (head == null)
      tail = null;

    return info;
  }

  public Object clone() {

    NiceQueue cloneQueue = new NiceQueue();
    NiceEnumeration myElements = elements();

    if (myElements.hasMoreElements()) {

      NiceListLink prevLink = new NiceListLink(myElements.nextElement(), null);
      cloneQueue.add(prevLink);

      while (myElements.hasMoreElements()) {
	prevLink.setNext(new NiceListLink(myElements.nextElement(), null));
	prevLink = prevLink.getNext();
      }

    }

    return cloneQueue;
  }

  public NiceIterator getIterator() {
    return new NiceQueueIterator(this);
  }
}


Site Stats

Snippets: 1791
Codebases: 571