📄 abstractlinkedlist.java
字号:
}
ListIterator it1 = listIterator();
ListIterator it2 = other.listIterator();
while (it1.hasNext() && it2.hasNext()) {
Object o1 = it1.next();
Object o2 = it2.next();
if (!(o1 == null ? o2 == null : o1.equals(o2)))
return false;
}
return !(it1.hasNext() || it2.hasNext());
}
public int hashCode() {
int hashCode = 1;
Iterator it = iterator();
while (it.hasNext()) {
Object obj = it.next();
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
}
return hashCode;
}
public String toString() {
if (size() == 0) {
return "[]";
}
StringBuffer buf = new StringBuffer(16 * size());
buf.append("[");
Iterator it = iterator();
boolean hasNext = it.hasNext();
while (hasNext) {
Object value = it.next();
buf.append(value == this ? "(this Collection)" : value);
hasNext = it.hasNext();
if (hasNext) {
buf.append(", ");
}
}
buf.append("]");
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* Compares two values for equals.
* This implementation uses the equals method.
* Subclasses can override this to match differently.
*
* @param value1 the first value to compare, may be null
* @param value2 the second value to compare, may be null
* @return true if equal
*/
protected boolean isEqualValue(Object value1, Object value2) {
return (value1 == value2 || (value1 == null ? false : value1.equals(value2)));
}
/**
* Updates the node with a new value.
* This implementation sets the value on the node.
* Subclasses can override this to record the change.
*
* @param node node to update
* @param value new value of the node
*/
protected void updateNode(Node node, Object value) {
node.setValue(value);
}
/**
* Creates a new node with previous, next and element all set to null.
* This implementation creates a new empty Node.
* Subclasses can override this to create a different class.
*
* @return newly created node
*/
protected Node createHeaderNode() {
return new Node();
}
/**
* Creates a new node with the specified properties.
* This implementation creates a new Node with data.
* Subclasses can override this to create a different class.
*
* @param value value of the new node
*/
protected Node createNode(Object value) {
return new Node(value);
}
/**
* Creates a new node with the specified object as its
* <code>value</code> and inserts it before <code>node</code>.
* <p>
* This implementation uses {@link #createNode(Object)} and
* {@link #addNode(AbstractLinkedList.Node,AbstractLinkedList.Node)}.
*
* @param node node to insert before
* @param value value of the newly added node
* @throws NullPointerException if <code>node</code> is null
*/
protected void addNodeBefore(Node node, Object value) {
Node newNode = createNode(value);
addNode(newNode, node);
}
/**
* Creates a new node with the specified object as its
* <code>value</code> and inserts it after <code>node</code>.
* <p>
* This implementation uses {@link #createNode(Object)} and
* {@link #addNode(AbstractLinkedList.Node,AbstractLinkedList.Node)}.
*
* @param node node to insert after
* @param value value of the newly added node
* @throws NullPointerException if <code>node</code> is null
*/
protected void addNodeAfter(Node node, Object value) {
Node newNode = createNode(value);
addNode(newNode, node.next);
}
/**
* Inserts a new node into the list.
*
* @param nodeToInsert new node to insert
* @param insertBeforeNode node to insert before
* @throws NullPointerException if either node is null
*/
protected void addNode(Node nodeToInsert, Node insertBeforeNode) {
nodeToInsert.next = insertBeforeNode;
nodeToInsert.previous = insertBeforeNode.previous;
insertBeforeNode.previous.next = nodeToInsert;
insertBeforeNode.previous = nodeToInsert;
size++;
modCount++;
}
/**
* Removes the specified node from the list.
*
* @param node the node to remove
* @throws NullPointerException if <code>node</code> is null
*/
protected void removeNode(Node node) {
node.previous.next = node.next;
node.next.previous = node.previous;
size--;
modCount++;
}
/**
* Removes all nodes by resetting the circular list marker.
*/
protected void removeAllNodes() {
header.next = header;
header.previous = header;
size = 0;
modCount++;
}
/**
* Gets the node at a particular index.
*
* @param index the index, starting from 0
* @param endMarkerAllowed whether or not the end marker can be returned if
* startIndex is set to the list's size
* @throws IndexOutOfBoundsException if the index is less than 0; equal to
* the size of the list and endMakerAllowed is false; or greater than the
* size of the list
*/
protected Node getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException {
// Check the index is within the bounds
if (index < 0) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") less than zero.");
}
if (!endMarkerAllowed && index == size) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") is the size of the list.");
}
if (index > size) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") greater than the size of the " +
"list (" + size + ").");
}
// Search the list and get the node
Node node;
if (index < (size / 2)) {
// Search forwards
node = header.next;
for (int currentIndex = 0; currentIndex < index; currentIndex++) {
node = node.next;
}
} else {
// Search backwards
node = header;
for (int currentIndex = size; currentIndex > index; currentIndex--) {
node = node.previous;
}
}
return node;
}
//-----------------------------------------------------------------------
/**
* Creates an iterator for the sublist.
*
* @param subList the sublist to get an iterator for
*/
protected Iterator createSubListIterator(LinkedSubList subList) {
return createSubListListIterator(subList, 0);
}
/**
* 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) {
return new LinkedSubListIterator(subList, fromIndex);
}
//-----------------------------------------------------------------------
/**
* Serializes the data held in this object to the stream specified.
* <p>
* The first serializable subclass must call this method from
* <code>writeObject</code>.
*/
protected void doWriteObject(ObjectOutputStream outputStream) throws IOException {
// Write the size so we know how many nodes to read back
outputStream.writeInt(size());
for (Iterator itr = iterator(); itr.hasNext();) {
outputStream.writeObject(itr.next());
}
}
/**
* Deserializes the data held in this object to the stream specified.
* <p>
* The first serializable subclass must call this method from
* <code>readObject</code>.
*/
protected void doReadObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
init();
int size = inputStream.readInt();
for (int i = 0; i < size; i++) {
add(inputStream.readObject());
}
}
//-----------------------------------------------------------------------
/**
* A node within the linked list.
* <p>
* From Commons Collections 3.1, all access to the <code>value</code> property
* is via the methods on this class.
*/
protected static class Node {
/** A pointer to the node before this node */
protected Node previous;
/** A pointer to the node after this node */
protected Node next;
/** The object contained within this node */
protected Object value;
/**
* Constructs a new header node.
*/
protected Node() {
super();
previous = this;
next = this;
}
/**
* Constructs a new node.
*
* @param value the value to store
*/
protected Node(Object value) {
super();
this.value = value;
}
/**
* Constructs a new node.
*
* @param previous the previous node in the list
* @param next the next node in the list
* @param value the value to store
*/
protected Node(Node previous, Node next, Object value) {
super();
this.previous = previous;
this.next = next;
this.value = value;
}
/**
* Gets the value of the node.
*
* @return the value
* @since Commons Collections 3.1
*/
protected Object getValue() {
return value;
}
/**
* Sets the value of the node.
*
* @param value the value
* @since Commons Collections 3.1
*/
protected void setValue(Object value) {
this.value = value;
}
/**
* Gets the previous node.
*
* @return the previous node
* @since Commons Collections 3.1
*/
protected Node getPreviousNode() {
return previous;
}
/**
* Sets the previous node.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -