KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > LinkedNode


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 /**
21  * Provides a base class for you to extend when you want object to maintain
22  * a doubly linked list to other objects without using a collection class.
23  *
24  * @author chirino
25  */

26 public class LinkedNode {
27     
28     protected LinkedNode next=this;
29     protected LinkedNode prev=this;
30     protected boolean tail=true;
31     
32
33     public LinkedNode getHeadNode() {
34         if( isHeadNode() ) {
35             return this;
36         }
37         if( isTailNode() ) {
38             return next;
39         }
40         LinkedNode rc = prev;
41         while(!rc.isHeadNode()) {
42             rc = rc.prev;
43         }
44         return rc;
45     }
46     
47     public LinkedNode getTailNode() {
48         if( isTailNode() ) {
49             return this;
50         }
51         if( isHeadNode() ) {
52             return prev;
53         }
54         LinkedNode rc = next;
55         while(!rc.isTailNode()) {
56             rc = rc.next;
57         }
58         return rc;
59     }
60
61     public LinkedNode getNext() {
62         return tail ? null : next;
63     }
64
65     public LinkedNode getPrevious() {
66         return prev.tail ? null : prev;
67     }
68
69     public boolean isHeadNode() {
70         return prev.isTailNode();
71     }
72     
73     public boolean isTailNode() {
74         return tail;
75     }
76
77     /**
78      * @param rightHead the node to link after this node.
79      * @return this
80      */

81     public LinkedNode linkAfter(LinkedNode rightHead) {
82         
83         if( rightHead == this ) {
84             throw new IllegalArgumentException JavaDoc("You cannot link to yourself");
85         }
86         if( !rightHead.isHeadNode() ) {
87             throw new IllegalArgumentException JavaDoc("You only insert nodes that are the first in a list");
88         }
89
90         LinkedNode rightTail = rightHead.prev;
91
92         if( tail ) {
93             tail = false;
94         } else {
95             rightTail.tail=false;
96         }
97                 
98         rightHead.prev = this; // link the head of the right side.
99
rightTail.next = next; // link the tail of the right side
100
next.prev = rightTail; // link the head of the left side
101
next = rightHead; // link the tail of the left side.
102

103         return this;
104     }
105
106     
107     /**
108      * @param leftHead the node to link after this node.
109      * @return
110      * @return this
111      */

112     public LinkedNode linkBefore(LinkedNode leftHead) {
113         
114         
115         if( leftHead == this ) {
116             throw new IllegalArgumentException JavaDoc("You cannot link to yourself");
117         }
118         if( !leftHead.isHeadNode() ) {
119             throw new IllegalArgumentException JavaDoc("You only insert nodes that are the first in a list");
120         }
121
122         // The left side is no longer going to be a tail..
123
LinkedNode leftTail = leftHead.prev;
124         leftTail.tail = false;
125         
126         leftTail.next = this; // link the tail of the left side.
127
leftHead.prev = prev; // link the head of the left side.
128
prev.next = leftHead; // link the tail of the right side.
129
prev = leftTail; // link the head of the right side.
130

131         return leftHead;
132     }
133
134     /**
135      * Removes this node out of the linked list it is chained in.
136      */

137     public void unlink() {
138         // If we are allready unlinked...
139
if( prev==this ) {
140             return;
141         }
142         
143         if( tail ) {
144             prev.tail = true;
145         }
146         
147         // Update the peers links..
148
next.prev = prev;
149         prev.next = next;
150         
151         // Update our links..
152
next = this;
153         prev = this;
154         tail=true;
155     }
156     
157 }
158
Popular Tags