- Fixed toArray(T[]) to terminate with null if the provided array has

room to spare, per the spec.
- Document that we do not support null elements.
- Increment modCount in our modifying methods, to take advantage of
  fail-fast iterators.
- A few small comments.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2805 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2010-08-25 19:00:38 +00:00
parent 500e107857
commit b78365ea6d
@@ -31,7 +31,9 @@ import java.lang.reflect.Array;
/** /**
* Provides a base for extending the standard Java {@link ArrayList} * Provides a base for extending the standard Java {@link ArrayList}
* functionality (which we'd just extend directly if those pig fuckers hadn't * functionality (which we'd just extend directly if those pig fuckers hadn't
* made the instance variables private). * made the instance variables private).<p/>
*
* <em>Note:</em> Does not support null elements.
*/ */
public abstract class BaseArrayList<E> extends AbstractList<E> public abstract class BaseArrayList<E> extends AbstractList<E>
implements RandomAccess, Cloneable, Serializable implements RandomAccess, Cloneable, Serializable
@@ -74,7 +76,10 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
if (_elements != null) { if (_elements != null) {
System.arraycopy(_elements, 0, target, 0, _size); System.arraycopy(_elements, 0, target, 0, _size);
} }
// terminate with null if there is room to spare, per the spec
if (target.length > _size) {
target[_size] = null;
}
return target; return target;
} }
@@ -83,6 +88,7 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
{ {
_elements = null; _elements = null;
_size = 0; _size = 0;
modCount++;
} }
@Override @Override
@@ -91,6 +97,7 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
{ {
_elements = (E[])ListUtil.add(_elements, _size, o); _elements = (E[])ListUtil.add(_elements, _size, o);
_size++; _size++;
modCount++;
return true; return true;
} }
@@ -99,6 +106,7 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
{ {
if (ListUtil.remove(_elements, o) != null) { if (ListUtil.remove(_elements, o) != null) {
_size--; _size--;
modCount++;
return true; return true;
} }
return false; return false;
@@ -117,6 +125,7 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
rangeCheck(index, false); rangeCheck(index, false);
E old = _elements[index]; E old = _elements[index];
_elements[index] = element; _elements[index] = element;
// do not increment modCount: this is not a structural modification
return old; return old;
} }
@@ -127,6 +136,7 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
rangeCheck(index, true); rangeCheck(index, true);
_elements = (E[])ListUtil.insert(_elements, index, element); _elements = (E[])ListUtil.insert(_elements, index, element);
_size++; _size++;
modCount++;
} }
@Override @Override
@@ -136,13 +146,14 @@ public abstract class BaseArrayList<E> extends AbstractList<E>
rangeCheck(index, false); rangeCheck(index, false);
E oval = (E)ListUtil.remove(_elements, index); E oval = (E)ListUtil.remove(_elements, index);
_size--; _size--;
modCount++;
return oval; return oval;
} }
@Override @Override
public int indexOf (Object o) public int indexOf (Object o)
{ {
return ListUtil.indexOf(_elements, o); return ListUtil.indexOf(_elements, o); // non-optimal: will check indexes >= _size
} }
// // documentation inherited from interface // // documentation inherited from interface