NiceList.java

jmud-0.11/src/util/nice/NiceList.java · 1.6 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 NiceList implements NiceEnumerable, NiceIterateable, Cloneable {

  protected NiceListLink head = null;

  NiceListLink getHead() {
    return head;
  }

  public NiceList() {
  }

  private void add(NiceListLink aLink) {
    aLink.setNext(head);
    head = aLink;
  }

  public boolean isEmpty() {
    return head == null;
  }

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

  public void remove(Niceable info) throws NoSuchElementException {
    
    if (head != null) {
      NiceListLink prevLink = head;
      
      if (prevLink.getInfo() == info) {
	head = prevLink.getNext();
	return;
      }
      
      while (prevLink.getNext() != null) {
	NiceListLink currLink = prevLink.getNext();
	
	if (currLink.getInfo() == info) {
	  prevLink.setNext(currLink.getNext());
	  return;
	}
	
	prevLink = currLink;
      }
    }

    throw new NoSuchElementException();
  }

  public NiceEnumeration elements() {
    return new NiceListEnumeration(this);
  }

  public Object clone() {

    NiceList cloneList = new NiceList();
    NiceEnumeration myElements = elements();

    if (myElements.hasMoreElements()) {

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

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

    }

    return cloneList;
  }

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


Site Stats

Snippets: 1791
Codebases: 571