LinkedList.java 3.93 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22 23 24 25 26 27 28 29
 */

package org.jivesoftware.util;


/**
 * Simple LinkedList implementation. The main feature is that list nodes
 * are public, which allows very fast delete operations when one has a
 * reference to the node that is to be deleted.<p>
 *
 * @author Jive Software
30
 * @param <E>
Matt Tucker's avatar
Matt Tucker committed
31
 */
32
public class LinkedList<E> {
Matt Tucker's avatar
Matt Tucker committed
33 34 35 36 37

    /**
     * The root of the list keeps a reference to both the first and last
     * elements of the list.
     */
38
    private LinkedListNode<E> head;
Matt Tucker's avatar
Matt Tucker committed
39 40 41 42 43

    /**
     * Creates a new linked list.
     */
    public LinkedList() {
44
    	head = new LinkedListNode<E>();
Matt Tucker's avatar
Matt Tucker committed
45 46 47 48 49 50 51
    }

    /**
     * Returns the first linked list node in the list.
     *
     * @return the first element of the list.
     */
52 53
    public LinkedListNode<E> getFirst() {
        LinkedListNode<E> node = head.next;
Matt Tucker's avatar
Matt Tucker committed
54 55 56 57 58 59 60 61 62 63 64
        if (node == head) {
            return null;
        }
        return node;
    }

    /**
     * Returns the last linked list node in the list.
     *
     * @return the last element of the list.
     */
65 66
    public LinkedListNode<E> getLast() {
        LinkedListNode<E> node = head.previous;
Matt Tucker's avatar
Matt Tucker committed
67 68 69 70 71 72 73 74 75 76 77
        if (node == head) {
            return null;
        }
        return node;
    }

    /**
     * Adds a node to the beginning of the list.
     *
     * @param node the node to add to the beginning of the list.
     */
78
    public LinkedListNode<E> addFirst(LinkedListNode<E> node) {
79
    	return node.insert(head.next, head);
Matt Tucker's avatar
Matt Tucker committed
80 81 82 83 84 85 86 87 88
    }

    /**
     * Adds an object to the beginning of the list by automatically creating a
     * a new node and adding it to the beginning of the list.
     *
     * @param object the object to add to the beginning of the list.
     * @return the node created to wrap the object.
     */
89 90
    public LinkedListNode<E> addFirst(E object) {
        return new LinkedListNode<E>(object, head.next, head);
91 92 93 94 95 96 97
    }

    /**
     * Adds a node to the end of the list.
     *
     * @param node the node to add to the beginning of the list.
     */
98
    public LinkedListNode<E> addLast(LinkedListNode<E> node) {
99
    	return node.insert(head, head.previous);
Matt Tucker's avatar
Matt Tucker committed
100 101 102 103 104 105 106 107 108
    }

    /**
     * Adds an object to the end of the list by automatically creating a
     * a new node and adding it to the end of the list.
     *
     * @param object the object to add to the end of the list.
     * @return the node created to wrap the object.
     */
109 110
    public LinkedListNode<E> addLast(E object) {
        return new LinkedListNode<E>(object, head, head.previous);
Matt Tucker's avatar
Matt Tucker committed
111 112 113 114 115 116 117
    }

    /**
     * Erases all elements in the list and re-initializes it.
     */
    public void clear() {
        //Remove all references in the list.
118
        LinkedListNode<E> node = getLast();
Matt Tucker's avatar
Matt Tucker committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        while (node != null) {
            node.remove();
            node = getLast();
        }

        //Re-initialize.
        head.next = head.previous = head;
    }

    /**
     * Returns a String representation of the linked list with a comma
     * delimited list of all the elements in the list.
     *
     * @return a String representation of the LinkedList.
     */
134 135
    @Override
	public String toString() {
136
        LinkedListNode<E> node = head.next;
137
        StringBuilder buf = new StringBuilder();
Matt Tucker's avatar
Matt Tucker committed
138 139 140 141 142 143 144
        while (node != head) {
            buf.append(node.toString()).append(", ");
            node = node.next;
        }
        return buf.toString();
    }
}