KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > LinkedList


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26 package org.nemesis.forum.util;
27
28
29 /**
30  * Simple LinkedList implementation. The main feature is that list nodes
31  * are public, which allows very fast delete operations (when one has a
32  * reference to the node that is to be deleted).<p>
33  *
34  * The linked list implementation was specifically written for the CoolServlets
35  * cache system. While it can be used as a general purpose linked list, for
36  * most applications, it is more suitable to use the linked list that is part
37  * of the Java Collections package.
38  */

39 public class LinkedList {
40
41     /**
42      * The root of the list keeps a reference to both the first and last
43      * elements of the list.
44      */

45     private LinkedListNode head = new LinkedListNode("head", null, null);
46
47     /**
48      * Creates a new linked list.
49      */

50     public LinkedList() {
51         head.next = head.previous = head;
52     }
53
54     /**
55      * Returns the first linked list node in the list.
56      *
57      * @return the first element of the list.
58      */

59     public LinkedListNode getFirst() {
60         LinkedListNode node = head.next;
61         if (node == head) {
62             return null;
63         }
64         return node;
65     }
66
67     /**
68      * Returns the last linked list node in the list.
69      *
70      * @return the last element of the list.
71      */

72     public LinkedListNode getLast() {
73         LinkedListNode node = head.previous;
74         if (node == head) {
75             return null;
76         }
77         return node;
78     }
79
80     /**
81      * Adds a node to the beginning of the list.
82      *
83      * @param node the node to add to the beginning of the list.
84      */

85     public LinkedListNode addFirst(LinkedListNode node) {
86         node.next = head.next;
87         node.previous = head;
88         node.previous.next = node;
89         node.next.previous = node;
90         return node;
91     }
92
93     /**
94      * Adds an object to the beginning of the list by automatically creating a
95      * a new node and adding it to the beginning of the list.
96      *
97      * @param object the object to add to the beginning of the list.
98      * @return the node created to wrap the object.
99      */

100     public LinkedListNode addFirst(Object JavaDoc object) {
101         LinkedListNode node = new LinkedListNode(object, head.next, head);
102         node.previous.next = node;
103         node.next.previous = node;
104         return node;
105     }
106
107     /**
108      * Adds an object to the end of the list by automatically creating a
109      * a new node and adding it to the end of the list.
110      *
111      * @param object the object to add to the end of the list.
112      * @return the node created to wrap the object.
113      */

114     public LinkedListNode addLast(Object JavaDoc object) {
115         LinkedListNode node = new LinkedListNode(object, head, head.previous);
116         node.previous.next = node;
117         node.next.previous = node;
118         return node;
119     }
120
121     /**
122      * Erases all elements in the list and re-initializes it.
123      */

124     public void clear() {
125         //Remove all references in the list.
126
LinkedListNode node = getLast();
127         while (node != null) {
128             node.remove();
129             node = getLast();
130         }
131
132         //Re-initialize.
133
head.next = head.previous = head;
134     }
135
136     /**
137      * Returns a String representation of the linked list with a comma
138      * delimited list of all the elements in the list.
139      *
140      * @return a String representation of the LinkedList.
141      */

142     public String JavaDoc toString() {
143         LinkedListNode node = head.next;
144         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
145         while (node != head) {
146             buf.append(node.toString()).append(", ");
147             node = node.next;
148         }
149         return buf.toString();
150     }
151 }
152
Popular Tags