Source Code Cross Referenced for LinkedList.java in  » Cache » shiftone-cache » org » shiftone » cache » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » shiftone cache » org.shiftone.cache.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.shiftone.cache.util;
002:
003:        /**
004:         * This linked list is different from the java.util implementation in that it exposes access to
005:         * the nodes themselves, allowing lower level manipulation.  This ends up being rather critical
006:         * when removing elements from a cache.  Having a reference to the node allows it to be removed
007:         * in constant time - rather than having to search for it first.
008:         *
009:         * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
010:         * @version $Revision: 1.8 $
011:         */
012:        public class LinkedList {
013:
014:            private static Log LOG = new Log(LinkedList.class);
015:            private LinkedListNode header = null;
016:            private int size = 0;
017:
018:            /**
019:             * Constructor LinkedList
020:             *
021:             */
022:            public LinkedList() {
023:
024:                header = new LinkedListNode(null);
025:                header.value = header; // this is how I designate the header node
026:                header.prev = header;
027:                header.next = header;
028:            }
029:
030:            /**
031:             * adding an object to the list, making it the new first node.
032:             *     for the purposes of treating this list as a queue or stack, <b>this</b>
033:             *     is the end of the list that should be used when adding.
034:             */
035:            public final LinkedListNode addFirst(Object obj) {
036:                return addBefore(header.next, obj);
037:            }
038:
039:            /**
040:             * adding an object to the list, making it the new last node.
041:             */
042:            public final LinkedListNode addLast(Object obj) {
043:                return addBefore(header, obj);
044:            }
045:
046:            /**
047:             * remove a node from the end of the list (list is being used like a <b>queue</b>).
048:             */
049:            public final Object dequeue() {
050:                return removeLast();
051:            }
052:
053:            /**
054:             * remove a node from the beginning of the list (list is being
055:             *     used like a <b>stack</b>)
056:             */
057:            public final Object pop() {
058:                return removeFirst();
059:            }
060:
061:            /**
062:             * peek the first element without removing it.  This is a <b>stack</b> style peek
063:             */
064:            public final LinkedListNode peekFirst() {
065:
066:                return (header.next == header) ? null : header.next;
067:            }
068:
069:            /**
070:             * peek the last element without removing it.  This is a <b>queue</b> style peek
071:             */
072:            public final LinkedListNode peekLast() {
073:
074:                return (header.prev == header) ? null : header.prev;
075:            }
076:
077:            /**
078:             * Method removeFirst
079:             */
080:            private final Object removeFirst() {
081:                return remove(header.next);
082:            }
083:
084:            /**
085:             * Method removeLast
086:             */
087:            private final Object removeLast() {
088:                return remove(header.prev);
089:            }
090:
091:            /**
092:             * promotes this node to the front of the the list.
093:             */
094:            public final void moveToFirst(LinkedListNode node) {
095:                remove(node);
096:                addNodeBefore(header.next, node);
097:            }
098:
099:            /**
100:             * demotes this node to the end of the list.
101:             */
102:            public final void moveToLast(LinkedListNode node) {
103:                remove(node);
104:                addNodeBefore(header, node);
105:            }
106:
107:            /**
108:             * Method addBefore (somewhat expensive - alloc)
109:             */
110:            public final LinkedListNode addBefore(LinkedListNode node,
111:                    Object obj) {
112:
113:                LinkedListNode newNode = new LinkedListNode(obj);
114:
115:                addNodeBefore(node, newNode);
116:
117:                return newNode;
118:            }
119:
120:            /**
121:             * Method addNodeBefore
122:             */
123:            public final void addNodeBefore(LinkedListNode nodeToAddBefore,
124:                    LinkedListNode newPreviousNode) {
125:
126:                newPreviousNode.prev = nodeToAddBefore.prev;
127:                newPreviousNode.next = nodeToAddBefore;
128:                newPreviousNode.prev.next = newPreviousNode;
129:                newPreviousNode.next.prev = newPreviousNode;
130:
131:                size++;
132:            }
133:
134:            /**
135:             * Removes the node from the list and returns the value of the former node.
136:             */
137:            public final Object remove(LinkedListNode node) {
138:
139:                if ((node == null) || (node == header)) {
140:                    return null;
141:                }
142:
143:                node.prev.next = node.next;
144:                node.next.prev = node.prev;
145:                node.prev = null;
146:                node.next = null;
147:
148:                size--;
149:
150:                return node.value;
151:            }
152:
153:            /**
154:             * Method size
155:             */
156:            public int size() {
157:                return size;
158:            }
159:
160:            /**
161:             * Method toString
162:             */
163:            public String toString() {
164:
165:                StringBuffer sb = new StringBuffer();
166:                LinkedListNode this Node = header.next;
167:
168:                sb.append("[LIST size=" + size() + "]");
169:
170:                while (this Node != header) {
171:                    sb.append("<->");
172:                    sb.append(String.valueOf(this Node.value));
173:
174:                    this Node = this Node.next;
175:                }
176:
177:                return sb.toString();
178:            }
179:
180:            /**
181:             * Method main
182:             */
183:            public static void main(String[] args) {
184:
185:                LinkedList fifo = new LinkedList();
186:
187:                for (int i = 0; i < 5; i++) {
188:                    fifo.addFirst("#" + i);
189:                }
190:
191:                LinkedListNode a = fifo.addFirst("AAAAA");
192:                LinkedListNode b = fifo.addFirst("BBBBB");
193:
194:                for (int i = 5; i < 10; i++) {
195:                    fifo.addFirst("#" + i);
196:                }
197:
198:                fifo.moveToFirst(a);
199:                fifo.moveToLast(b);
200:
201:                /*
202:                while (fifo.pop() != null)
203:                {
204:                    /// LOG.debug(fifo);
205:                }
206:                 */
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.