📄 cursorablelinkedlist.java
字号:
* Informs all of my registered cursors that the specified
* element was changed.
*
* @param node the node that was changed
*/
protected void broadcastNodeChanged(Node node) {
Iterator it = cursors.iterator();
while (it.hasNext()) {
WeakReference ref = (WeakReference) it.next();
Cursor cursor = (Cursor) ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
cursor.nodeChanged(node);
}
}
}
/**
* Informs all of my registered cursors that the specified
* element was just removed from my list.
*
* @param node the node that was changed
*/
protected void broadcastNodeRemoved(Node node) {
Iterator it = cursors.iterator();
while (it.hasNext()) {
WeakReference ref = (WeakReference) it.next();
Cursor cursor = (Cursor) ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
cursor.nodeRemoved(node);
}
}
}
/**
* Informs all of my registered cursors that the specified
* element was just added to my list.
*
* @param node the node that was changed
*/
protected void broadcastNodeInserted(Node node) {
Iterator it = cursors.iterator();
while (it.hasNext()) {
WeakReference ref = (WeakReference) it.next();
Cursor cursor = (Cursor) ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
cursor.nodeInserted(node);
}
}
}
//-----------------------------------------------------------------------
/**
* Serializes the data held in this object to the stream specified.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
doWriteObject(out);
}
/**
* Deserializes the data held in this object to the stream specified.
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
doReadObject(in);
}
//-----------------------------------------------------------------------
/**
* Creates a list iterator for the sublist.
*
* @param subList the sublist to get an iterator for
* @param fromIndex the index to start from, relative to the sublist
*/
protected ListIterator createSubListListIterator(LinkedSubList subList, int fromIndex) {
SubCursor cursor = new SubCursor(subList, fromIndex);
registerCursor(cursor);
return cursor;
}
//-----------------------------------------------------------------------
/**
* An extended <code>ListIterator</code> that allows concurrent changes to
* the underlying list.
*/
public static class Cursor extends AbstractLinkedList.LinkedListIterator {
/** Is the cursor valid (not closed) */
boolean valid = true;
/** Is the next index valid */
boolean nextIndexValid = true;
/** Flag to indicate if the current element was removed by another object. */
boolean currentRemovedByAnother = false;
/**
* Constructs a new cursor.
*
* @param index the index to start from
*/
protected Cursor(CursorableLinkedList parent, int index) {
super(parent, index);
valid = true;
}
/**
* Removes the item last returned by this iterator.
* <p>
* There may have been subsequent alterations to the list
* since you obtained this item, however you can still remove it.
* You can even remove it if the item is no longer in the main list.
* However, you can't call this method on the same iterator more
* than once without calling next() or previous().
*
* @throws IllegalStateException if there is no item to remove
*/
public void remove() {
// overridden, as the nodeRemoved() method updates the iterator
// state in the parent.removeNode() call below
if (current == null && currentRemovedByAnother) {
// quietly ignore, as the last returned node was removed
// by the list or some other iterator
// by ignoring it, we keep this iterator independent from
// other changes as much as possible
} else {
checkModCount();
parent.removeNode(getLastNodeReturned());
}
currentRemovedByAnother = false;
}
/**
* Adds an object to the list.
* The object added here will be the new 'previous' in the iterator.
*
* @param obj the object to add
*/
public void add(Object obj) {
// overridden, as the nodeInserted() method updates the iterator state
super.add(obj);
// matches the (next.previous == node) clause in nodeInserted()
// thus next gets changed - reset it again here
next = next.next;
}
// set is not overridden, as it works ok
// note that we want it to throw an exception if the element being
// set has been removed from the real list (compare this with the
// remove method where we silently ignore this case)
/**
* Gets the index of the next element to be returned.
*
* @return the next index
*/
public int nextIndex() {
if (nextIndexValid == false) {
if (next == parent.header) {
nextIndex = parent.size();
} else {
int pos = 0;
Node temp = parent.header.next;
while (temp != next) {
pos++;
temp = temp.next;
}
nextIndex = pos;
}
nextIndexValid = true;
}
return nextIndex;
}
/**
* Handle event from the list when a node has changed.
*
* @param node the node that changed
*/
protected void nodeChanged(Node node) {
// do nothing
}
/**
* Handle event from the list when a node has been removed.
*
* @param node the node that was removed
*/
protected void nodeRemoved(Node node) {
if (node == next && node == current) {
// state where next() followed by previous()
next = node.next;
current = null;
currentRemovedByAnother = true;
} else if (node == next) {
// state where next() not followed by previous()
// and we are matching next node
next = node.next;
currentRemovedByAnother = false;
} else if (node == current) {
// state where next() not followed by previous()
// and we are matching current (last returned) node
current = null;
currentRemovedByAnother = true;
nextIndex--;
} else {
nextIndexValid = false;
currentRemovedByAnother = false;
}
}
/**
* Handle event from the list when a node has been added.
*
* @param node the node that was added
*/
protected void nodeInserted(Node node) {
if (node.previous == current) {
next = node;
} else if (next.previous == node) {
next = node;
} else {
nextIndexValid = false;
}
}
/**
* Override superclass modCount check, and replace it with our valid flag.
*/
protected void checkModCount() {
if (!valid) {
throw new ConcurrentModificationException("Cursor closed");
}
}
/**
* Mark this cursor as no longer being needed. Any resources
* associated with this cursor are immediately released.
* In previous versions of this class, it was mandatory to close
* all cursor objects to avoid memory leaks. It is <i>no longer</i>
* necessary to call this close method; an instance of this class
* can now be treated exactly like a normal iterator.
*/
public void close() {
if (valid) {
((CursorableLinkedList) parent).unregisterCursor(this);
valid = false;
}
}
}
//-----------------------------------------------------------------------
/**
* A cursor for the sublist based on LinkedSubListIterator.
*
* @since Commons Collections 3.2
*/
protected static class SubCursor extends Cursor {
/** The parent list */
protected final LinkedSubList sub;
/**
* Constructs a new cursor.
*
* @param index the index to start from
*/
protected SubCursor(LinkedSubList sub, int index) {
super((CursorableLinkedList) sub.parent, index + sub.offset);
this.sub = sub;
}
public boolean hasNext() {
return (nextIndex() < sub.size);
}
public boolean hasPrevious() {
return (previousIndex() >= 0);
}
public int nextIndex() {
return (super.nextIndex() - sub.offset);
}
public void add(Object obj) {
super.add(obj);
sub.expectedModCount = parent.modCount;
sub.size++;
}
public void remove() {
super.remove();
sub.expectedModCount = parent.modCount;
sub.size--;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -