[COLLECTIONS-459] Removed setArray methods in ArrayIterator and ObjectArrayIterator, made fields final if possible.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1493922 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-06-17 20:25:23 +00:00
parent 20619b096c
commit 68c0a80d17
7 changed files with 41 additions and 167 deletions

View File

@ -37,29 +37,17 @@ import org.apache.commons.collections4.ResettableIterator;
*/
public class ArrayIterator<E> implements ResettableIterator<E> {
// 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 <code>setArray</code>.
* <p>
* 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<E> implements ResettableIterator<E> {
* @throws NullPointerException if <code>array</code> is <code>null</code>
*/
public ArrayIterator(final Object array) {
super();
setArray(array);
this(array, 0);
}
/**
@ -84,11 +71,7 @@ public class ArrayIterator<E> implements ResettableIterator<E> {
* @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<E> implements ResettableIterator<E> {
*/
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<E> implements ResettableIterator<E> {
}
/**
* Sets the array that the ArrayIterator should iterate over.
* <p>
* 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 <code>array</code> is not an array.
* @throws NullPointerException if <code>array</code> is <code>null</code>
* @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;
}
/**

View File

@ -54,16 +54,6 @@ public class ArrayListIterator<E> extends ArrayIterator<E>
// Constructors
// ----------------------------------------------------------------------
/**
* Constructor for use with <code>setArray</code>.
* <p>
* 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<E> extends ArrayIterator<E>
*/
public ArrayListIterator(final Object array, final int startIndex) {
super(array, startIndex);
this.startIndex = startIndex;
}
/**
@ -105,7 +94,6 @@ public class ArrayListIterator<E> extends ArrayIterator<E>
*/
public ArrayListIterator(final Object array, final int startIndex, final int endIndex) {
super(array, startIndex, endIndex);
this.startIndex = startIndex;
}
// ListIterator interface

View File

@ -36,27 +36,16 @@ import org.apache.commons.collections4.ResettableIterator;
public class ObjectArrayIterator<E>
implements Iterator<E>, ResettableIterator<E> {
// 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 <code>setArray</code>.
* <p>
* 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,36 +142,12 @@ public class ObjectArrayIterator<E>
/**
* Gets the array that this iterator is iterating over.
*
* @return the array this iterator iterates over, or <code>null</code> 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.
* <p>
* This method may only be called once, otherwise an IllegalStateException
* will occur.
* <p>
* 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 <code>array</code> was set in the constructor
* @throws NullPointerException if <code>array</code> is <code>null</code>
*/
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.
*

View File

@ -48,16 +48,7 @@ public class ObjectArrayListIterator<E> extends ObjectArrayIterator<E>
*/
private int lastItemIndex = -1;
/**
* Constructor for use with <code>setArray</code>.
* <p>
* 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<E> extends ObjectArrayIterator<E>
* @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<E> extends ObjectArrayIterator<E>
* @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<E> extends ObjectArrayIterator<E>
* @return the index of the item to be retrieved next
*/
public int previousIndex() {
return this.index - this.startIndex - 1;
return this.index - getStartIndex() - 1;
}
/**

View File

@ -81,29 +81,6 @@ public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> {
}
}
// 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<E> 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<E> 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<E> iter = makeArrayIterator(testArray, 2);
int count = 0;

View File

@ -77,15 +77,6 @@ public class ArrayIteratorTest<E> extends AbstractIteratorTest<E> {
} catch (final NullPointerException e) {
// expected
}
final ArrayIterator<Object> iter = new ArrayIterator<Object>();
try {
iter.setArray(null);
fail("setArray(null) should throw a NullPointerException");
} catch (final NullPointerException e) {
// expected
}
}
public void testReset() {

View File

@ -94,26 +94,6 @@ public class ObjectArrayIteratorTest<E> extends AbstractIteratorTest<E> {
} catch (final NullPointerException e) {
// expected
}
final ObjectArrayIterator<E> 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<E> it = makeArrayIterator();
it.setArray((E[]) new String[0]);
try {
it.setArray((E[]) new String[0]);
fail();
} catch (final IllegalStateException ex) {
}
}
@SuppressWarnings("unchecked")