KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > linkedlist > ExposedNodeLinkedList


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper.linkedlist;
23
24 import java.util.*;
25 import oracle.toplink.essentials.exceptions.ValidationException;
26
27 /**
28  * INTERNAL:
29  * A custom implementation of a linked list. This list exposes the linked nodes
30  * directly to the developer. It allows nodes to be referenced in code for quick
31  * list manipulation (ie reshuffle, remove, or queueing)
32  * It is specifically used in the TopLink cache write lock mechanism in order
33  * to allow quick removal of objects from the list while still providing the getFirst()
34  * addLast() functionality of a queue. The alternative java classes LinkedList, LinkedHashMap
35  * do not provide both functional requirements.
36  * @author Gordon Yorke
37  * @since 10.0.3
38  * @see oracle.toplink.essentials.internal.helper.linkedlist.LinkedNode
39  */

40 public class ExposedNodeLinkedList implements List {
41     private transient LinkedNode header;
42     private transient int size;
43
44     /**
45      * Constructs an empty list.
46      */

47     public ExposedNodeLinkedList() {
48         this.size = 0;
49         this.header = new LinkedNode(null, null, null);
50         header.next = header;
51         header.previous = header;
52     }
53
54     // Bunch of List methods not currently implemented.
55
public Object JavaDoc[] toArray(Object JavaDoc[] array) {
56         throw ValidationException.operationNotSupported("toArray");
57     }
58
59     public Object JavaDoc[] toArray() {
60         throw ValidationException.operationNotSupported("toArray");
61     }
62
63     public Object JavaDoc set(int index, Object JavaDoc value) {
64         throw ValidationException.operationNotSupported("set");
65     }
66
67     public ListIterator listIterator(int index) {
68         throw ValidationException.operationNotSupported("listIterator");
69     }
70
71     public ListIterator listIterator() {
72         throw ValidationException.operationNotSupported("listIterator");
73     }
74
75     public Iterator iterator() {
76         throw ValidationException.operationNotSupported("iterator");
77     }
78
79     public List subList(int start, int end) {
80         throw ValidationException.operationNotSupported("subList");
81     }
82
83     public boolean retainAll(Collection collection) {
84         throw ValidationException.operationNotSupported("retainAll");
85     }
86
87     public boolean removeAll(Collection collection) {
88         throw ValidationException.operationNotSupported("removeAll");
89     }
90
91     public boolean containsAll(Collection collection) {
92         throw ValidationException.operationNotSupported("containsAll");
93     }
94
95     public boolean addAll(Collection collection) {
96         throw ValidationException.operationNotSupported("addAll");
97     }
98
99     public boolean addAll(int index, Collection collection) {
100         throw ValidationException.operationNotSupported("addAll");
101     }
102
103     public boolean remove(Object JavaDoc object) {
104         throw ValidationException.operationNotSupported("remove");
105     }
106
107     public boolean add(Object JavaDoc object) {
108         addLast(object);
109         return true;
110     }
111
112     public int lastIndexOf(Object JavaDoc object) {
113         throw ValidationException.operationNotSupported("lastIndexOf");
114     }
115
116     public void add(int index, Object JavaDoc object) {
117         throw ValidationException.operationNotSupported("add");
118     }
119
120     public Object JavaDoc remove(int index) {
121         throw ValidationException.operationNotSupported("remove");
122     }
123
124     public Object JavaDoc get(int index) {
125         throw ValidationException.operationNotSupported("get");
126     }
127
128     public boolean isEmpty() {
129         return size() == 0;
130     }
131
132     /**
133      * Returns the first contents in this list.
134      *
135      * @return the first contents in this list. Null if this list is empty.
136      */

137     public Object JavaDoc getFirst() {
138         if (size == 0) {
139             return null;
140         }
141         return header.next.contents;
142     }
143
144     /**
145      * Returns the last contents in this list.
146      *
147      * @return the last contents in this list. Null if this list is empty.
148      */

149     public Object JavaDoc getLast() {
150         if (size == 0) {
151             return null;
152         }
153         return header.previous.contents;
154     }
155
156     /**
157      * Removes and returns the first contents from this list.
158      *
159      * @return the first contents from this list.
160      * @throws NoSuchElementException if this list is empty.
161      */

162     public Object JavaDoc removeFirst() {
163         if (size != 0) {
164             Object JavaDoc first = header.next.contents;
165             remove(header.next);
166             return first;
167         }
168         return null;
169     }
170
171     /**
172        * Removes and returns the last contents from this list.
173      *
174      * @return the last contents from this list.
175      * @throws NoSuchElementException if this list is empty.
176      */

177     public Object JavaDoc removeLast() {
178         if (size != 0) {
179             Object JavaDoc last = header.previous.contents;
180             remove(header.previous);
181             return last;
182         }
183         return null;
184     }
185
186     /**
187      * Inserts the given contents at the beginning of this list.
188      *
189      * @param o the contents to be inserted at the beginning of this list.
190      */

191     public LinkedNode addFirst(Object JavaDoc o) {
192         return addAfter(o, header);
193     }
194
195     /**
196      * Appends the given contents to the end of this list. (Identical in
197      * function to the <tt>add</tt> method; included only for consistency.)
198      *
199      * @param o the contents to be inserted at the end of this list.
200      */

201     public LinkedNode addLast(Object JavaDoc o) {
202         return addAfter(o, header.previous);
203     }
204
205     /**
206      * Returns <tt>true</tt> if this list contains the specified contents.
207      * More formally, returns <tt>true</tt> if and only if this list contains
208      * at least one contents <tt>e</tt> such that <tt>(o==null ? e==null
209      * : o.equals(e))</tt>.
210      *
211      * @param o contents whose presence in this list is to be tested.
212      * @return <tt>true</tt> if this list contains the specified contents.
213      */

214     public boolean contains(Object JavaDoc o) {
215         return indexOf(o) != -1;
216     }
217
218     /**
219      * Returns the number of contentss in this list.
220      *
221      * @return the number of contentss in this list.
222      */

223     public int size() {
224         return size;
225     }
226
227     /**
228      * Removes all of the contentss from this list.
229      */

230     public void clear() {
231         header.next = header;
232         header.previous = header;
233         size = 0;
234     }
235
236     /**
237      * Returns the index in this list of the first occurrence of the
238      * specified contents, or -1 if the List does not contain this
239      * contents. More formally, returns the lowest index i such that
240      * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if
241      * there is no such index.
242      *
243      * @param o contents to search for.
244      * @return the index in this list of the first occurrence of the
245      * specified contents, or -1 if the list does not contain this
246      * contents.
247      */

248     public int indexOf(Object JavaDoc o) {
249         int index = 0;
250         if (o == null) {
251             for (LinkedNode n = header.next; n != header; n = n.next) {
252                 if (n.contents == null) {
253                     return index;
254                 }
255                 index++;
256             }
257         } else {
258             for (LinkedNode n = header.next; n != header; n = n.next) {
259                 if (o.equals(n.contents)) {
260                     return index;
261                 }
262                 index++;
263             }
264         }
265         return -1;
266     }
267
268     private LinkedNode addAfter(Object JavaDoc o, LinkedNode n) {
269         LinkedNode newNode = new LinkedNode(o, n.next, n);
270         newNode.previous.next = newNode;
271         newNode.next.previous = newNode;
272         size++;
273         return newNode;
274     }
275
276     /**
277      * Allows a node to be efficiently removed.
278      */

279     public void remove(LinkedNode n) {
280         if (n == header) {
281             throw new NoSuchElementException();
282         } else if ((n.previous == null) || (n.next == null)) {
283             // Handles case of node having already been removed.
284
return;
285         }
286         n.previous.next = n.next;
287         n.next.previous = n.previous;
288         // Also clear the nodes references to know that it has been removed.
289
n.previous = null;
290         n.next = null;
291         n.contents = null;
292         size--;
293     }
294
295     /**
296      * Allows a node to be efficiently moved first.
297      */

298     public void moveFirst(LinkedNode node) {
299         if (node == header) {
300             throw new NoSuchElementException();
301         } else if ((node.previous == null) || (node.next == null)) {
302             // Handles case of node having already been removed.
303
size++;
304         } else {
305             node.previous.next = node.next;
306             node.next.previous = node.previous;
307         }
308         node.next = header.next;
309         node.previous = header;
310         header.next = node;
311         node.next.previous = node;
312     }
313 }
314
Popular Tags