forked from nmcl/JavaSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
205 lines (159 loc) · 4.95 KB
/
Link.java
File metadata and controls
205 lines (159 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 1990-2010, Mark Little, University of Newcastle upon Tyne
* and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 1990-2010,
*/
package org.javasim.simset;
/*
* This class defines the elements of the linked lists within SIMSET.
*/
// Thanks to Jim Bean for converting the C++SIM classes
/**
* @deprecated As of release 2.1 use Java's own linked lists.
*/
public class Link {
public Link () {
this.next = null;
this.prev = null;
this.theList = null;
}
/**
* @return the successor to this list element.
*/
synchronized public Link suc () {
return next;
}
/**
* @return the predecessor to this list element.
*/
synchronized public Link pred () {
return prev;
}
/**
* @return this element in the list and remove it from the list.
*/
synchronized public Link out () {
RemoveElement ();
return this;
}
/**
* Add this entrt to the list.
*
* @param list the list to add this element.
*/
synchronized public void inTo (Head list) {
if (list != null) {
list.addLast (this);
theList = list;
return;
}
}
/**
* Add this list element to the same list as the element provided. Make
* sure elements are only on one list.
*
* @param toPrecede the element to add before in its list. If null, or the element
* is not in a list, then remove this element from any list it may be in to
* match.
*/
synchronized public void precede (Link toPrecede) {
if ((toPrecede == null) || ( ! toPrecede.inList()))
RemoveElement();
else {
if (inList())
RemoveElement();
toPrecede.addBefore(this);
}
};
/**
* Add this list element to the same list as the element provided. Make
* sure elements are only on one list.
*
* @param toFollow the element to add after in its list. If null, or the element
* is not in a list, then remove this element from any list it may be in to
* match.
*/
synchronized public void follow (Link toFollow) {
if ((toFollow == null) || ( ! toFollow.inList()))
RemoveElement();
else {
if (inList())
RemoveElement();
toFollow.addAfter(this);
}
}
/**
* Add this element to the head of a list.
*
* @param list the list to use.
*/
synchronized public void follow (Head list) {
if (list != null)
list.addFirst(this);
}
/**
* @return <code>true</code> if this element is in a list, <code>false</code> otherwise.
*/
synchronized public boolean inList () {
return (boolean) (theList != null);
}
private void RemoveElement () {
// can't have prev and next if we are not on a list
if (theList == null)
return;
if (prev != null)
prev.next = next;
if (next != null)
next.prev = prev;
if (theList.first == this)
theList.first = next;
if (theList.last == this)
theList.last = prev;
theList = null;
prev = null;
next = null;
}
private void addAfter (Link toAdd) {
toAdd.prev = this;
toAdd.theList = theList;
if (next == null)
next = toAdd;
else {
next.prev = toAdd;
toAdd.next = next;
next = toAdd;
}
if (theList.last == this)
theList.last = toAdd;
}
private void addBefore (Link toAdd) {
toAdd.theList = theList;
toAdd.next = this;
if (prev == null)
prev = toAdd;
else {
prev.next = toAdd;
toAdd.prev = prev;
prev = toAdd;
}
if (theList.first == this)
theList.first = toAdd;
}
protected Link next;
protected Link prev;
protected Head theList = new Head();
}