// This example is from the book _Java Threads_ by Scott Oaks and Henry Wong.
// Written by Scott Oaks and Henry Wong.
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// Sample CircularList -- Chapter 5, p. 90.
class CircularListNode {
Object o;
CircularListNode next;
CircularListNode prev;
}
public class CircularList {
private CircularListNode current;
public synchronized void insert(Object o) {
CircularListNode tn = new CircularListNode();
tn.o = o;
if (current == null) {
tn.next = tn.prev = tn;
current = tn;
} else { // Add Before Current Node
tn.next = current;
tn.prev = current.prev;
current.prev.next = tn;
current.prev = tn;
}
}
public synchronized void delete(Object o) {
CircularListNode p = find(o);
CircularListNode next = p.next;
CircularListNode prev = p.prev;
if (p == p.next) { // Last Object on the list
current = null;
return;
}
prev.next = next;
next.prev = prev;
if (current == p) current = next;
}
private CircularListNode find(Object o) {
CircularListNode p = current;
if (p == null)
throw new IllegalArgumentException();
do {
if (p.o == o) return p;
p = p.next;
} while (p != current);
throw new IllegalArgumentException();
}
public synchronized Object locate(Object o) {
CircularListNode p = current;
do {
if (p.o.equals(o)) return p.o;
p = p.next;
} while (p != current);
throw new IllegalArgumentException();
}
public synchronized Object getNext() {
if (current == null)
return null;
current = current.next;
return current.o;
}
}
|