Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 420DA10A2D for ; Mon, 17 Jun 2013 20:25:45 +0000 (UTC) Received: (qmail 17749 invoked by uid 500); 17 Jun 2013 20:25:45 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 17689 invoked by uid 500); 17 Jun 2013 20:25:45 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 17682 invoked by uid 99); 17 Jun 2013 20:25:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Jun 2013 20:25:45 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Jun 2013 20:25:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E157623889EB; Mon, 17 Jun 2013 20:25:23 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1493922 - in /commons/proper/collections/trunk/src: main/java/org/apache/commons/collections4/iterators/ test/java/org/apache/commons/collections4/iterators/ Date: Mon, 17 Jun 2013 20:25:23 -0000 To: commits@commons.apache.org From: tn@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130617202523.E157623889EB@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tn Date: Mon Jun 17 20:25:23 2013 New Revision: 1493922 URL: http://svn.apache.org/r1493922 Log: [COLLECTIONS-459] Removed setArray methods in ArrayIterator and ObjectArrayIterator, made fields final if possible. Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayIterator.java commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayListIterator.java commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayIterator.java commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayListIterator.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayIterator.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayIterator.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayIterator.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayIterator.java Mon Jun 17 20:25:23 2013 @@ -37,30 +37,18 @@ import org.apache.commons.collections4.R */ public class ArrayIterator implements ResettableIterator { - // TODO Privatise fields? Mainly read-only access - /** The array to iterate over */ - protected Object array; + protected final Object array; /** The start index to loop from */ - protected int startIndex = 0; + protected final int startIndex; /** The end index to loop to */ - protected int endIndex = 0; + protected final int endIndex; /** The current iterator index */ protected int index = 0; // Constructors // ---------------------------------------------------------------------- /** - * Constructor for use with setArray. - *

- * Using this constructor, the iterator is equivalent to an empty iterator - * until {@link #setArray(Object)} is called to establish the array to iterate over. - */ - public ArrayIterator() { - super(); - } - - /** * Constructs an ArrayIterator that will iterate over the values in the * specified array. * @@ -69,8 +57,7 @@ public class ArrayIterator implements * @throws NullPointerException if array is null */ public ArrayIterator(final Object array) { - super(); - setArray(array); + this(array, 0); } /** @@ -84,11 +71,7 @@ public class ArrayIterator implements * @throws IndexOutOfBoundsException if the index is invalid */ public ArrayIterator(final Object array, final int startIndex) { - super(); - setArray(array); - checkBound(startIndex, "start"); - this.startIndex = startIndex; - this.index = startIndex; + this(array, startIndex, Array.getLength(array)); } /** @@ -104,26 +87,30 @@ public class ArrayIterator implements */ public ArrayIterator(final Object array, final int startIndex, final int endIndex) { super(); - setArray(array); - checkBound(startIndex, "start"); - checkBound(endIndex, "end"); - if (endIndex < startIndex) { - throw new IllegalArgumentException("End index must not be less than start index."); - } + + this.array = array; this.startIndex = startIndex; this.endIndex = endIndex; this.index = startIndex; + + final int len = Array.getLength(array); + checkBound(startIndex, len, "start"); + checkBound(endIndex, len, "end"); + if (endIndex < startIndex) { + throw new IllegalArgumentException("End index must not be less than start index."); + } } /** * Checks whether the index is valid or not. * * @param bound the index to check + * @param len the length of the array * @param type the index type (for error messages) * @throws IndexOutOfBoundsException if the index is invalid */ - protected void checkBound(final int bound, final String type ) { - if (bound > this.endIndex) { + protected void checkBound(final int bound, final int len, final String type ) { + if (bound > len) { throw new ArrayIndexOutOfBoundsException( "Attempt to make an ArrayIterator that " + type + "s beyond the end of the array. " @@ -186,28 +173,23 @@ public class ArrayIterator implements } /** - * Sets the array that the ArrayIterator should iterate over. - *

- * If an array has previously been set (using the single-arg constructor - * or this method) then that array is discarded in favour of this one. - * Iteration is restarted at the start of the new array. - * Although this can be used to reset iteration, the {@link #reset()} method - * is a more effective choice. + * Gets the start index to loop from. * - * @param array the array that the iterator should iterate over. - * @throws IllegalArgumentException if array is not an array. - * @throws NullPointerException if array is null + * @return the start index + * @since 4.0 */ - public void setArray(final Object array) { - // Array.getLength throws IllegalArgumentException if the object is not - // an array or NullPointerException if the object is null. This call - // is made before saving the array and resetting the index so that the - // array iterator remains in a consistent state if the argument is not - // an array or is null. - this.endIndex = Array.getLength(array); - this.startIndex = 0; - this.array = array; - this.index = 0; + public int getStartIndex() { + return this.startIndex; + } + + /** + * Gets the end index to loop to. + * + * @return the end index + * @since 4.0 + */ + public int getEndIndex() { + return this.endIndex; } /** Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayListIterator.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayListIterator.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayListIterator.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ArrayListIterator.java Mon Jun 17 20:25:23 2013 @@ -55,16 +55,6 @@ public class ArrayListIterator extend // Constructors // ---------------------------------------------------------------------- /** - * Constructor for use with setArray. - *

- * Using this constructor, the iterator is equivalent to an empty iterator - * until {@link #setArray(Object)} is called to establish the array to iterate over. - */ - public ArrayListIterator() { - super(); - } - - /** * Constructs an ArrayListIterator that will iterate over the values in the * specified array. * @@ -88,7 +78,6 @@ public class ArrayListIterator extend */ public ArrayListIterator(final Object array, final int startIndex) { super(array, startIndex); - this.startIndex = startIndex; } /** @@ -105,7 +94,6 @@ public class ArrayListIterator extend */ public ArrayListIterator(final Object array, final int startIndex, final int endIndex) { super(array, startIndex, endIndex); - this.startIndex = startIndex; } // ListIterator interface Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayIterator.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayIterator.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayIterator.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayIterator.java Mon Jun 17 20:25:23 2013 @@ -36,27 +36,16 @@ import org.apache.commons.collections4.R public class ObjectArrayIterator implements Iterator, ResettableIterator { - // TODO Privatise fields? Mainly read-only access - /** The array */ - protected E[] array = null; + protected final E[] array; /** The start index to loop from */ - protected int startIndex = 0; + protected final int startIndex; /** The end index to loop to */ - protected int endIndex = 0; + protected final int endIndex; /** The current iterator index */ protected int index = 0; - /** - * Constructor for use with setArray. - *

- * Using this constructor, the iterator is equivalent to an empty iterator - * until {@link #setArray} is called to establish the array to iterate over. - */ - public ObjectArrayIterator() { - super(); - } - + //------------------------------------------------------------------------- /** * Constructs an ObjectArrayIterator that will iterate over the values in the * specified array. @@ -153,37 +142,13 @@ public class ObjectArrayIterator /** * Gets the array that this iterator is iterating over. * - * @return the array this iterator iterates over, or null if - * the no-arg constructor was used and {@link #setArray} has never - * been called with a valid array. + * @return the array this iterator iterates over */ public E[] getArray() { return this.array; } /** - * Sets the array that the ArrayIterator should iterate over. - *

- * This method may only be called once, otherwise an IllegalStateException - * will occur. - *

- * The {@link #reset} method can be used to reset the iterator if required. - * - * @param array the array that the iterator should iterate over - * @throws IllegalStateException if the array was set in the constructor - * @throws NullPointerException if array is null - */ - public void setArray(final E[] array) { - if (this.array != null) { - throw new IllegalStateException("The array to iterate over has already been set"); - } - this.array = array; - this.startIndex = 0; - this.endIndex = array.length; - this.index = 0; - } - - /** * Gets the start index to loop from. * * @return the start index Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayListIterator.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayListIterator.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayListIterator.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/iterators/ObjectArrayListIterator.java Mon Jun 17 20:25:23 2013 @@ -48,16 +48,7 @@ public class ObjectArrayListIterator */ private int lastItemIndex = -1; - /** - * Constructor for use with setArray. - *

- * Using this constructor, the iterator is equivalent to an empty iterator - * until {@link #setArray} is called to establish the array to iterate over. - */ - public ObjectArrayListIterator() { - super(); - } - + //------------------------------------------------------------------------- /** * Constructs an ObjectArrayListIterator that will iterate over the values in the * specified array. @@ -106,7 +97,7 @@ public class ObjectArrayListIterator * @return true if there is a previous element to return */ public boolean hasPrevious() { - return this.index > this.startIndex; + return this.index > getStartIndex(); } /** @@ -144,7 +135,7 @@ public class ObjectArrayListIterator * @return the index of the item to be retrieved next */ public int nextIndex() { - return this.index - this.startIndex; + return this.index - getStartIndex(); } /** @@ -153,7 +144,7 @@ public class ObjectArrayListIterator * @return the index of the item to be retrieved next */ public int previousIndex() { - return this.index - this.startIndex - 1; + return this.index - getStartIndex() - 1; } /** Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIterator2Test.java Mon Jun 17 20:25:23 2013 @@ -81,29 +81,6 @@ public class ArrayIterator2Test exten } } - // proves that an ArrayIterator set with the constructor has the same number of elements - // as an ArrayIterator set with setArray(Object) - public void testSetArray() { - final Iterator iter1 = makeArrayIterator(testArray); - int count1 = 0; - while (iter1.hasNext()) { - ++count1; - iter1.next(); - } - - assertEquals("the count should be right using the constructor", count1, testArray.length); - - final ArrayIterator iter2 = makeObject(); - iter2.setArray(testArray); - int count2 = 0; - while (iter2.hasNext()) { - ++count2; - iter2.next(); - } - - assertEquals("the count should be right using setArray(Object)", count2, testArray.length); - } - public void testIndexedArray() { Iterator iter = makeArrayIterator(testArray, 2); int count = 0; Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ArrayIteratorTest.java Mon Jun 17 20:25:23 2013 @@ -77,15 +77,6 @@ public class ArrayIteratorTest extend } catch (final NullPointerException e) { // expected } - - final ArrayIterator iter = new ArrayIterator(); - try { - iter.setArray(null); - - fail("setArray(null) should throw a NullPointerException"); - } catch (final NullPointerException e) { - // expected - } } public void testReset() { Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java?rev=1493922&r1=1493921&r2=1493922&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/iterators/ObjectArrayIteratorTest.java Mon Jun 17 20:25:23 2013 @@ -94,26 +94,6 @@ public class ObjectArrayIteratorTest } catch (final NullPointerException e) { // expected } - - final ObjectArrayIterator iter = makeArrayIterator(); - try { - iter.setArray(null); - - fail("setArray(null) should throw a NullPointerException"); - } catch (final NullPointerException e) { - // expected - } - } - - @SuppressWarnings("unchecked") - public void testDoubleSet() { - final ObjectArrayIterator it = makeArrayIterator(); - it.setArray((E[]) new String[0]); - try { - it.setArray((E[]) new String[0]); - fail(); - } catch (final IllegalStateException ex) { - } } @SuppressWarnings("unchecked")