Source code: com/virtuosotechnologies/lib/base/LinkedObject.java
1 /*
2 ================================================================================
3
4 FILE: LinkedObject.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 Base class for elements in a "naked" circular linked list.
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.lib.base;
43
44
45 /**
46 * Base class for elements in a "naked" circular linked list, i.e. it doesn't
47 * support the collections interface, but instead exposes the list cells.
48 * Useful for building larger data structures involving links without having
49 * the object and search overhead of using java.util.LinkedList internally.
50 * <p>
51 * The list is circular, e.g. there are never null pointers involved. You
52 * can create a "bounded" list using a delimiter element. The element whose
53 * next pointer points to the delimiter is the end, and the element whose
54 * prev pointer points to the delimiter is the beginning. As a corollary,
55 * an element that isn't linked up has both its next and prev pointers
56 * pointing to this.
57 * <p>
58 * Not synchronized. This is intended only to be a building block for larger
59 * data structures, so the synchronization policy is deferred to the client.
60 */
61 public class LinkedObject
62 {
63 private LinkedObject next_;
64 private LinkedObject prev_;
65
66
67 /**
68 * Constructor
69 */
70 public LinkedObject()
71 {
72 next_ = this;
73 prev_ = this;
74 }
75
76
77 /**
78 * Get the next element.
79 *
80 * @return non-null next element.
81 */
82 public LinkedObject getNext()
83 {
84 return next_;
85 }
86
87
88 /**
89 * Get the previous element.
90 *
91 * @return non-null previous element.
92 */
93 public LinkedObject getPrevious()
94 {
95 return prev_;
96 }
97
98
99 /**
100 * Inserts this element after the given one. Removes this element
101 * from any link it is already part of first.
102 *
103 * @return element element for this one to follow.
104 */
105 public void linkThisAfter(
106 LinkedObject element)
107 {
108 unlinkThis();
109 LinkedObject after = element.next_;
110 prev_ = element;
111 next_ = after;
112 element.next_ = this;
113 after.prev_ = this;
114 }
115
116
117 /**
118 * Inserts this element before the given one. Removes this element
119 * from any link it is already part of first.
120 *
121 * @return element element for this one to precede.
122 */
123 public void linkThisBefore(
124 LinkedObject element)
125 {
126 unlinkThis();
127 LinkedObject before = element.prev_;
128 next_ = element;
129 prev_ = before;
130 element.prev_ = this;
131 before.next_ = this;
132 }
133
134
135 /**
136 * Remove this element from its current link. Does nothing if it
137 * is not in a link.
138 */
139 public void unlinkThis()
140 {
141 if (next_ != this)
142 {
143 next_.prev_ = prev_;
144 prev_.next_ = next_;
145 next_ = this;
146 prev_ = this;
147 }
148 }
149 }