LimeWire Collection Component API

org.limewire.collection
Class DoublyLinkedList<E>

java.lang.Object
  extended by org.limewire.collection.DoublyLinkedList<E>
All Implemented Interfaces:
Iterable<DoublyLinkedList.ListElement<E>>

public class DoublyLinkedList<E>
extends Object
implements Iterable<DoublyLinkedList.ListElement<E>>

Provides a doubly-linked list. Unlike the LinkedList class in the JDK, DoublyLinkedList provides a way to refer to elements of the list (each of type ListElement) directly, avoiding linear-time searches when you wish to remove an element.

This class is not thread-safe.

    DoublyLinkedList dll = new DoublyLinkedList();
    dll.addLast("Abby");
    dll.addLast("Bob");
    dll.addLast("Chris");
    ListElement<String> dan = dll.addLast("Dan");

    for(DoublyLinkedList.ListElement<String> e : dll)
        System.out.println(e.getKey());
    dll.remove(dan);
    System.out.println("");
    for(DoublyLinkedList.ListElement<String> e : dll)
        System.out.println(e.getKey());     

    Output:
        Abby
        Bob
        Chris
        Dan

        Abby
        Bob
        Chris
 

Author:
Anurag Singla initial revision, Christopher Rohrs bug fix, specification cleanup, and unit tests

Nested Class Summary
static class DoublyLinkedList.ListElement<E>
          Represents an immutable element of the linked list.
 
Constructor Summary
DoublyLinkedList()
          Creates new empty DoublyLinkedList
 
Method Summary
 DoublyLinkedList.ListElement<E> addLast(E value)
          Inserts an object at the end of the list, returning its corresponding element.
 void clear()
          Removes all entries from this list
 boolean contains(DoublyLinkedList.ListElement<E> e)
          Returns true if this contains the given ListElement.
 Iterator<DoublyLinkedList.ListElement<E>> iterator()
           
 void remove(DoublyLinkedList.ListElement<E> element)
          Removes the specified element from the list
 DoublyLinkedList.ListElement<E> removeFirst()
          Removes and returns the first element from the list
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DoublyLinkedList

public DoublyLinkedList()
Creates new empty DoublyLinkedList

Method Detail

addLast

public DoublyLinkedList.ListElement<E> addLast(E value)
Inserts an object at the end of the list, returning its corresponding element.

Parameters:
value - the value of the new element.
Returns:
the element holding value.

removeFirst

public DoublyLinkedList.ListElement<E> removeFirst()
Removes and returns the first element from the list

Returns:
The element removed, or null if none present

remove

public void remove(DoublyLinkedList.ListElement<E> element)
Removes the specified element from the list

Parameters:
element - The element to be removed. This must be an element of this.

clear

public void clear()
Removes all entries from this list


iterator

public Iterator<DoublyLinkedList.ListElement<E>> iterator()
Specified by:
iterator in interface Iterable<DoublyLinkedList.ListElement<E>>

contains

public boolean contains(DoublyLinkedList.ListElement<E> e)
Returns true if this contains the given ListElement.


LimeWire Collection Component API

Copyright © 2009 Lime Wire LLC. All Rights Reserved.