commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections CursorableLinkedList.java
Date Fri, 21 Jun 2002 03:26:15 GMT
mas         2002/06/20 20:26:15

  Modified:    collections/src/java/org/apache/commons/collections
                        CursorableLinkedList.java
  Log:
  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 )
  
  Revision  Changes    Path
  1.9       +37 -16    jakarta-commons/collections/src/java/org/apache/commons/collections/CursorableLinkedList.java
  
  Index: CursorableLinkedList.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CursorableLinkedList.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CursorableLinkedList.java	16 Jun 2002 03:39:40 -0000	1.8
  +++ CursorableLinkedList.java	21 Jun 2002 03:26:15 -0000	1.9
  @@ -446,13 +446,24 @@
        */
       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 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;
       }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message