Add accessor methods for value field in Node

from Mike Pettypiece


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131670 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-04-20 23:33:54 +00:00
parent 845a2177db
commit 2199ec2fdf
1 changed files with 80 additions and 18 deletions

View File

@ -38,7 +38,7 @@ import org.apache.commons.collections.OrderedIterator;
* is here. * is here.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.8 $ $Date: 2004/02/18 01:12:26 $ * @version $Revision: 1.9 $ $Date: 2004/04/20 23:33:54 $
* *
* @author Rich Dougherty * @author Rich Dougherty
* @author Phil Steitz * @author Phil Steitz
@ -110,7 +110,7 @@ public abstract class AbstractLinkedList implements List {
public Object get(int index) { public Object get(int index) {
Node node = getNode(index, false); Node node = getNode(index, false);
return node.value; return node.getValue();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -130,7 +130,7 @@ public abstract class AbstractLinkedList implements List {
public int indexOf(Object value) { public int indexOf(Object value) {
int i = 0; int i = 0;
for (Node node = header.next; node != header; node = node.next) { for (Node node = header.next; node != header; node = node.next) {
if (isEqualValue(node.value, value)) { if (isEqualValue(node.getValue(), value)) {
return i; return i;
} }
i++; i++;
@ -141,7 +141,7 @@ public abstract class AbstractLinkedList implements List {
public int lastIndexOf(Object value) { public int lastIndexOf(Object value) {
int i = size - 1; int i = size - 1;
for (Node node = header.previous; node != header; node = node.previous) { for (Node node = header.previous; node != header; node = node.previous) {
if (isEqualValue(node.value, value)) { if (isEqualValue(node.getValue(), value)) {
return i; return i;
} }
i--; i--;
@ -177,7 +177,7 @@ public abstract class AbstractLinkedList implements List {
// Copy the values into the array // Copy the values into the array
int i = 0; int i = 0;
for (Node node = header.next; node != header; node = node.next, i++) { for (Node node = header.next; node != header; node = node.next, i++) {
array[i] = node.value; array[i] = node.getValue();
} }
// Set the value after the last value to null // Set the value after the last value to null
if (array.length > size) { if (array.length > size) {
@ -224,14 +224,14 @@ public abstract class AbstractLinkedList implements List {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object remove(int index) { public Object remove(int index) {
Node node = getNode(index, false); Node node = getNode(index, false);
Object oldValue = node.value; Object oldValue = node.getValue();
removeNode(node); removeNode(node);
return oldValue; return oldValue;
} }
public boolean remove(Object value) { public boolean remove(Object value) {
for (Node node = header.next; node != header; node = node.next) { for (Node node = header.next; node != header; node = node.next) {
if (isEqualValue(node.value, value)) { if (isEqualValue(node.getValue(), value)) {
removeNode(node); removeNode(node);
return true; return true;
} }
@ -266,7 +266,7 @@ public abstract class AbstractLinkedList implements List {
public Object set(int index, Object value) { public Object set(int index, Object value) {
Node node = getNode(index, false); Node node = getNode(index, false);
Object oldValue = node.value; Object oldValue = node.getValue();
updateNode(node, value); updateNode(node, value);
return oldValue; return oldValue;
} }
@ -281,7 +281,7 @@ public abstract class AbstractLinkedList implements List {
if (node == header) { if (node == header) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
return node.value; return node.getValue();
} }
public Object getLast() { public Object getLast() {
@ -289,7 +289,7 @@ public abstract class AbstractLinkedList implements List {
if (node == header) { if (node == header) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
return node.value; return node.getValue();
} }
public boolean addFirst(Object o) { public boolean addFirst(Object o) {
@ -307,7 +307,7 @@ public abstract class AbstractLinkedList implements List {
if (node == header) { if (node == header) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
Object oldValue = node.value; Object oldValue = node.getValue();
removeNode(node); removeNode(node);
return oldValue; return oldValue;
} }
@ -317,7 +317,7 @@ public abstract class AbstractLinkedList implements List {
if (node == header) { if (node == header) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
Object oldValue = node.value; Object oldValue = node.getValue();
removeNode(node); removeNode(node);
return oldValue; return oldValue;
} }
@ -399,7 +399,7 @@ public abstract class AbstractLinkedList implements List {
* @param value new value of the node * @param value new value of the node
*/ */
protected void updateNode(Node node, Object value) { protected void updateNode(Node node, Object value) {
node.value = value; node.setValue(value);
} }
/** /**
@ -590,6 +590,9 @@ public abstract class AbstractLinkedList implements List {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* A node within the linked list. * 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 { protected static class Node {
@ -632,6 +635,66 @@ public abstract class AbstractLinkedList implements List {
this.next = next; this.next = next;
this.value = value; 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.
*
* @param previous the previous node
* @since Commons Collections 3.1
*/
protected void setPreviousNode(Node previous) {
this.previous = previous;
}
/**
* Gets the next node.
*
* @return the next node
* @since Commons Collections 3.1
*/
protected Node getNextNode() {
return next;
}
/**
* Sets the next node.
*
* @param next the next node
* @since Commons Collections 3.1
*/
protected void setNextNode(Node next) {
this.next = next;
}
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -720,10 +783,9 @@ public abstract class AbstractLinkedList implements List {
public Object next() { public Object next() {
checkModCount(); checkModCount();
if (!hasNext()) { if (!hasNext()) {
throw new NoSuchElementException("No element at index " + throw new NoSuchElementException("No element at index " + nextIndex + ".");
nextIndex + ".");
} }
Object value = next.value; Object value = next.getValue();
current = next; current = next;
next = next.next; next = next.next;
nextIndex++; nextIndex++;
@ -740,7 +802,7 @@ public abstract class AbstractLinkedList implements List {
throw new NoSuchElementException("Already at start of list."); throw new NoSuchElementException("Already at start of list.");
} }
next = next.previous; next = next.previous;
Object value = next.value; Object value = next.getValue();
current = next; current = next;
nextIndex--; nextIndex--;
return value; return value;
@ -765,7 +827,7 @@ public abstract class AbstractLinkedList implements List {
public void set(Object obj) { public void set(Object obj) {
checkModCount(); checkModCount();
getLastNodeReturned().value = obj; getLastNodeReturned().setValue(obj);
} }
public void add(Object obj) { public void add(Object obj) {