The indexOf(Object) and lastIndexOf(Object) were incorrectly

raising a NullPointerException if null were passed as the argument.
This patch file fixes the behavior so that these methods will
correctly find a null element.

Submitted by: Paul Jack ( pjack at sfaf dot org )


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130730 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Smith 2002-06-21 03:26:15 +00:00
parent f405dbaea6
commit 4ab1ffccbe
1 changed files with 37 additions and 16 deletions

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CursorableLinkedList.java,v 1.8 2002/06/16 03:39:40 mas Exp $
* $Revision: 1.8 $
* $Date: 2002/06/16 03:39:40 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CursorableLinkedList.java,v 1.9 2002/06/21 03:26:15 mas Exp $
* $Revision: 1.9 $
* $Date: 2002/06/21 03:26:15 $
*
* ====================================================================
*
@ -92,7 +92,7 @@ import java.lang.UnsupportedOperationException; // stops a javadoc warning
*
* @since 1.0
* @author Rodney Waldhoff
* @version $Id: CursorableLinkedList.java,v 1.8 2002/06/16 03:39:40 mas Exp $
* @version $Id: CursorableLinkedList.java,v 1.9 2002/06/21 03:26:15 mas Exp $
* @see java.util.LinkedList
*/
public class CursorableLinkedList implements List, Serializable {
@ -446,13 +446,24 @@ public class CursorableLinkedList implements List, Serializable {
*/
public int indexOf(Object o) {
int ndx = 0;
for(Listable elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) {
if(null == o && null == elt.value()) {
return ndx;
} else if(o.equals(elt.value())) {
return ndx;
// perform the null check outside of the loop to save checking every
// single time through the loop.
if (null == o) {
for(Listable elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) {
if (null == elt.value()) {
return ndx;
}
ndx++;
}
} else {
for(Listable elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) {
if (o.equals(elt.value())) {
return ndx;
}
ndx++;
}
ndx++;
}
return -1;
}
@ -486,13 +497,23 @@ public class CursorableLinkedList implements List, Serializable {
*/
public int lastIndexOf(Object o) {
int ndx = _size-1;
for(Listable elt = _head.prev(), past = null; null != elt && past != _head.next(); elt = (past = elt).prev()) {
if(null == o && null == elt.value()) {
return ndx;
} else if(o.equals(elt.value())) {
return ndx;
// perform the null check outside of the loop to save checking every
// single time through the loop.
if (null == o) {
for(Listable elt = _head.prev(), past = null; null != elt && past != _head.next(); elt = (past = elt).prev()) {
if (null == elt.value()) {
return ndx;
}
ndx--;
}
} else {
for(Listable elt = _head.prev(), past = null; null != elt && past != _head.next(); elt = (past = elt).prev()) {
if (o.equals(elt.value())) {
return ndx;
}
ndx--;
}
ndx--;
}
return -1;
}