rwaldhoff 2003/01/06 16:59:51 Modified: collections/src/java/org/apache/commons/collections/primitives AbstractRandomAccessIntList.java CollectionIntCollection.java IntCollectionCollection.java IntListIteratorListIterator.java IntListList.java Added: collections/src/java/org/apache/commons/collections/primitives ListIntList.java ListIteratorIntListIterator.java Log: * add additional adapters * modify wrappers so that equals compares wrapped types * clarify IntList.equals with javadoc Revision Changes Path 1.2 +24 -28 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java Index: AbstractRandomAccessIntList.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractRandomAccessIntList.java 6 Jan 2003 03:59:12 -0000 1.1 +++ AbstractRandomAccessIntList.java 7 Jan 2003 00:59:51 -0000 1.2 @@ -146,33 +146,29 @@ return new RandomAccessIntSubList(this,fromIndex,toIndex); } + /** + * Returns <code>true</code> iff <i>that</i> is + * an {@link IntList} with the same {@link #size size} + * as me, and whose {@link #iterator iterator} returns the + * same sequence of values as mine. + */ public boolean equals(Object that) { - // handle an easy and somewhat frequent case via a shortcut - if(this == that) { return true; } - - // otherwise, try to get an IntIterator from that object - IntIterator thatIter = null; - if(that instanceof IntList) { - if(size() != ((IntList)that).size()) { return false; } // another quick check - thatIter = ((IntList)that).iterator(); - } else if(that instanceof List) { - if(size() != ((List)that).size()) { return false; } // another quick check - thatIter = IteratorIntIterator.wrap(((List)that).iterator()); - } - - if(null == thatIter) { return false; } - - // walk thru thisIter and thatIter, checking for any differences - for(IntIterator thisIter = iterator(); thisIter.hasNext();) { - if(!thatIter.hasNext()) { return false; } // thatIter has a different length - if(thisIter.next() != thatIter.next()) { - return false; + if(this == that) { + return true; + } else if(that instanceof IntList) { + IntList thatList = (IntList)that; + if(size() != thatList.size()) { + return false; } - } - - if(thatIter.hasNext()) { return false; } // thatIter has a different length - - return true; + for(IntIterator thatIter = thatList.iterator(), thisIter = iterator(); thisIter.hasNext();) { + if(thisIter.next() != thatIter.next()) { + return false; + } + } + return true; + } else { + return false; + } } public int hashCode() { 1.2 +9 -5 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/CollectionIntCollection.java Index: CollectionIntCollection.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/CollectionIntCollection.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CollectionIntCollection.java 5 Jan 2003 03:03:42 -0000 1.1 +++ CollectionIntCollection.java 7 Jan 2003 00:59:51 -0000 1.2 @@ -96,8 +96,12 @@ return _collection.containsAll(IntCollectionCollection.wrap(c)); } - public boolean equals(Object o) { - return _collection.equals(o); + public boolean equals(Object that) { + if(that instanceof IntCollection) { + return _collection.equals(IntCollectionCollection.wrap((IntCollection)that)); + } else { + return _collection.equals(that); + } } public int hashCode() { 1.3 +16 -6 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntCollectionCollection.java Index: IntCollectionCollection.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntCollectionCollection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- IntCollectionCollection.java 6 Jan 2003 03:56:21 -0000 1.2 +++ IntCollectionCollection.java 7 Jan 2003 00:59:51 -0000 1.3 @@ -99,8 +99,18 @@ return _collection.containsAll(CollectionIntCollection.wrap(c)); } - public boolean equals(Object o) { - return _collection.equals(o); + public boolean equals(Object that) { + if(that instanceof Collection) { + try { + return _collection.equals(CollectionIntCollection.wrap((Collection)that)); + } catch(ClassCastException e) { + return false; + } catch(NullPointerException e) { + return false; + } + } else { + return _collection.equals(that); + } } public int hashCode() { 1.2 +5 -5 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntListIteratorListIterator.java Index: IntListIteratorListIterator.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntListIteratorListIterator.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- IntListIteratorListIterator.java 6 Jan 2003 03:57:15 -0000 1.1 +++ IntListIteratorListIterator.java 7 Jan 2003 00:59:51 -0000 1.2 @@ -64,8 +64,8 @@ import java.util.ListIterator; /** - * Adapts an {@link IntIterator} to the - * {@link java.util.Iterator Iterator} interface. + * Adapts an {@link IntListIterator} to the + * {@link java.util.ListIterator ListIterator} interface. * * @version $Revision$ $Date$ * @author Rodney Waldhoff 1.2 +11 -3 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntListList.java Index: IntListList.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntListList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- IntListList.java 6 Jan 2003 03:57:42 -0000 1.1 +++ IntListList.java 7 Jan 2003 00:59:51 -0000 1.2 @@ -119,6 +119,14 @@ return IntListList.wrap(_list.subList(fromIndex,toIndex)); } + public boolean equals(Object that) { + if(that instanceof IntList) { + return _list.equals(ListIntList.wrap((List)that)); + } else { + return super.equals(that); + } + } + public static List wrap(IntList list) { return null == list ? null : new IntListList(list); } 1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/ListIntList.java Index: ListIntList.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/ListIntList.java,v 1.1 2003/01/07 00:59:51 rwaldhoff Exp $ * $Revision: 1.1 $ * $Date: 2003/01/07 00:59:51 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.collections.primitives; import java.util.List; /** * Adapts a {@link Number}-valued {@link java.util.List List} * to the {@link IntList} interface. * * @version $Revision: 1.1 $ $Date: 2003/01/07 00:59:51 $ * @author Rodney Waldhoff */ public class ListIntList extends CollectionIntCollection implements IntList { public ListIntList(List list) { super(list); _list = list; } public void add(int index, int element) { _list.add(index,new Integer(element)); } public boolean addAll(int index, IntCollection collection) { return _list.addAll(index,IntCollectionCollection.wrap(collection)); } public int get(int index) { return ((Number)_list.get(index)).intValue(); } public int indexOf(int element) { return _list.indexOf(new Integer(element)); } public int lastIndexOf(int element) { return _list.lastIndexOf(new Integer(element)); } public IntListIterator listIterator() { return ListIteratorIntListIterator.wrap(_list.listIterator()); } public IntListIterator listIterator(int index) { return ListIteratorIntListIterator.wrap(_list.listIterator(index)); } public int removeElementAt(int index) { return ((Number)_list.remove(index)).intValue(); } public int set(int index, int element) { return ((Number)_list.set(index,new Integer(element))).intValue(); } public IntList subList(int fromIndex, int toIndex) { return ListIntList.wrap(_list.subList(fromIndex,toIndex)); } public boolean equals(Object that) { if(that instanceof List) { try { return _list.equals(ListIntList.wrap((List)that)); } catch(ClassCastException e) { return false; } catch(NullPointerException e) { return false; } } else { return super.equals(that); } } public static IntList wrap(List list) { return null == list ? null : new ListIntList(list); } private List _list = null; } 1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/ListIteratorIntListIterator.java Index: ListIteratorIntListIterator.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/ListIteratorIntListIterator.java,v 1.1 2003/01/07 00:59:51 rwaldhoff Exp $ * $Revision: 1.1 $ * $Date: 2003/01/07 00:59:51 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.collections.primitives; import java.util.ListIterator; /** * Adapts a {@link Number}-valued {@link java.util.ListIterator} * to the {@link IntListIterator} interface. * * @version $Revision: 1.1 $ $Date: 2003/01/07 00:59:51 $ * @author Rodney Waldhoff */ public class ListIteratorIntListIterator implements IntListIterator { public ListIteratorIntListIterator(ListIterator iterator) { _iterator = iterator; } public int nextIndex() { return _iterator.nextIndex(); } public int previousIndex() { return _iterator.previousIndex(); } public boolean hasNext() { return _iterator.hasNext(); } public boolean hasPrevious() { return _iterator.hasPrevious(); } public int next() { return ((Number)_iterator.next()).intValue(); } public int previous() { return ((Number)_iterator.previous()).intValue(); } public void add(int element) { _iterator.add(new Integer(element)); } public void set(int element) { _iterator.set(new Integer(element)); } public void remove() { _iterator.remove(); } public static IntListIterator wrap(ListIterator iterator) { return null == iterator ? null : new ListIteratorIntListIterator(iterator); } private ListIterator _iterator = null; }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>