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

xapi.collect.impl.SimpleLinkedList Maven / Gradle / Ivy

Go to download

This module exists solely to package all other gwt modules into a single uber jar. This makes deploying to non-mavenized targets much easier. Of course, you would be wise to inherit your dependencies individually; the uber jar is intended for projects like collide, which have complex configuration, and adding many jars would be a pain.

The newest version!
/**
 *
 */
package xapi.collect.impl;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.function.Consumer;

/**
 * @author "James X. Nelson ([email protected])"
 *
 */
public class SimpleLinkedList
  extends
AbstractLinkedList, SimpleLinkedList> {

  protected static class LinkedListNode extends
    AbstractLinkedList.Node> {
    LinkedListNode previous;
  }

  protected final class ListIter implements ListIterator {

    private LinkedListNode node;
    int pos;

    public ListIter(final LinkedListNode start, final int pos) {
      this.node = start;
      this.pos = pos;
    }

    @Override
    public void add(final T e) {
      final LinkedListNode newNode = newNode(e);
      newNode.value = e;
      newNode.next = node.next;
      newNode.previous = node;
      if (node.next == null) {
        tail = newNode;
      } else {
        node.next.previous = newNode;
      }
      node.next = newNode;
      onAdd(node, newNode);
      node = newNode;
    }

    @Override
    public boolean hasNext() {
      return node != null && node.next != null;
    }

    @Override
    public boolean hasPrevious() {
      return node == null ? tail != null : node.previous != null;
    }

    @Override
    public T next() {
      try {
        return node.next.value;
      } finally {
        pos++;
        node = node.next;
      }
    }

    @Override
    public int nextIndex() {
      return pos + 1;
    }

    @Override
    public T previous() {
      try {
        return node == null ? tail.value : node.value;
      } finally {
        pos--;
        node = node == null ? tail.previous : node.previous == null ? head
          : node.previous;
      }
    }

    @Override
    public int previousIndex() {
      return pos - 1;
    }

    @Override
    public void remove() {
      final LinkedListNode next = node.next;
      node.next = next.next;
      if (next.next == null) {
        tail = node;
      } else {
        next.next.previous = node;
      }
      node = next;
    }

    @Override
    public void set(final T e) {
      node.value = e;
    }

  }

  protected final class NodeIteratorReverse implements Iterator {
    private LinkedListNode next = tail;

    @Override
    public boolean hasNext() {
      return next != head;
    }

    @Override
    public T next() {
      try {
        return next.value;
      } finally {
        next = next.previous;
      }
    }

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

  }

  public void forEachReverse(final Consumer consumer) {
    assert consumer != null;
    final Iterator reverse = iteratorReverse();
    while (reverse.hasNext()) {
      consumer.accept(reverse.next());
    }
  }

  public Iterator iteratorReverse() {
    return new NodeIteratorReverse();
  }

  public ListIterator listIterator() {
    return new ListIter(head, 0);
  }

  @Override
  protected LinkedListNode newNode(final T item) {
    return new LinkedListNode();
  }

  @Override
  protected void onAdd(final LinkedListNode previous,
    final LinkedListNode next) {
    next.previous = previous;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy