1 package org.apache.jcs.utils.struct;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /***
26 * This is an basic thread safe single linked list. It provides very limited functionality. It is
27 * small and fast.
28 * <p>
29 * @author Aaron Smuts
30 */
31 public class SingleLinkedList
32 {
33 private static final Log log = LogFactory.getLog( SingleLinkedList.class );
34
35 private Object lock = new Object();
36
37
38 private Node head = new Node();
39
40
41 private Node tail = head;
42
43 private int size = 0;
44
45 /***
46 * Takes the first item off the list.
47 * <p>
48 * @return null if the list is empty.
49 */
50 public Object takeFirst()
51 {
52 synchronized ( lock )
53 {
54
55 if ( head == tail )
56 {
57 return null;
58 }
59
60 Node node = head.next;
61
62 Object value = node.payload;
63
64 if ( log.isDebugEnabled() )
65 {
66 log.debug( "head.payload = " + head.payload );
67 log.debug( "node.payload = " + node.payload );
68 }
69
70
71
72 node.payload = null;
73 head = node;
74
75 size--;
76 return value;
77 }
78 }
79
80 /***
81 * Adds an item to the end of the list.
82 * <p>
83 * @param payload
84 */
85 public void addLast( Object payload )
86 {
87 Node newNode = new Node();
88
89 newNode.payload = payload;
90
91 synchronized ( lock )
92 {
93 size++;
94 tail.next = newNode;
95 tail = newNode;
96 }
97 }
98
99 /***
100 * Removes everything.
101 */
102 public void clear()
103 {
104 synchronized ( lock )
105 {
106 head = tail;
107 size = 0;
108 }
109 }
110
111 /***
112 * The list is composed of nodes.
113 * <p>
114 * @author Aaron Smuts
115 */
116 private static class Node
117 {
118 Node next = null;
119
120 Object payload;
121 }
122
123 /***
124 * Returns the number of elements in the list.
125 * <p>
126 * @return number of items in the list.
127 */
128 public int size()
129 {
130 return size;
131 }
132 }