
com.samskivert.util.BaseArrayList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of samskivert Show documentation
Show all versions of samskivert Show documentation
A collection of Java utilities.
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2011 Michael Bayne, et al.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.RandomAccess;
import java.lang.reflect.Array;
/**
* Provides a base for extending the standard Java {@link ArrayList}
* functionality (which we'd just extend directly if those pig fuckers hadn't
* made the instance variables private).
*
* Note: Does not support null elements.
*/
public abstract class BaseArrayList extends AbstractList
implements RandomAccess, Cloneable, Serializable
{
@Override
public int size ()
{
return _size;
}
@Override
public boolean isEmpty ()
{
return (_size == 0);
}
@Override
public boolean contains (Object o)
{
return ListUtil.contains(_elements, o);
}
@Override
public Object[] toArray ()
{
return toArray(new Object[_size]);
}
@Override
@SuppressWarnings("unchecked")
public T[] toArray (T[] target)
{
// create the target array if necessary
if (target.length < _size) {
target = (T[])Array.newInstance(
target.getClass().getComponentType(), _size);
} else if (target.length > _size) {
// terminate with null if there is room to spare, per the spec
target[_size] = null;
}
// copy the elements
if (_elements != null) {
System.arraycopy(_elements, 0, target, 0, _size);
}
return target;
}
@Override
public void clear ()
{
_elements = null;
_size = 0;
modCount++;
}
@Override
@SuppressWarnings("unchecked")
public boolean add (E o)
{
_elements = (E[])ListUtil.add(_elements, _size, o);
_size++;
modCount++;
return true;
}
@Override
public boolean remove (Object o)
{
if (ListUtil.remove(_elements, o) != null) {
_size--;
modCount++;
return true;
}
return false;
}
@Override
public E get (int index)
{
rangeCheck(index, false);
return _elements[index];
}
@Override
public E set (int index, E element)
{
rangeCheck(index, false);
E old = _elements[index];
_elements[index] = element;
// do not increment modCount: this is not a structural modification
return old;
}
@Override
@SuppressWarnings("unchecked")
public void add (int index, E element)
{
rangeCheck(index, true);
_elements = (E[])ListUtil.insert(_elements, index, element);
_size++;
modCount++;
}
@Override
@SuppressWarnings("unchecked")
public E remove (int index)
{
rangeCheck(index, false);
E oval = (E)ListUtil.remove(_elements, index);
_size--;
modCount++;
return oval;
}
@Override
public int indexOf (Object o)
{
return ListUtil.indexOf(_elements, o); // non-optimal: will check indexes >= _size
}
// // documentation inherited from interface
// public int lastIndexOf (Object o)
// {
// }
/**
* Check the range of a passed-in index to make sure it's valid.
*
* @param insert if true, an index equal to our size is valid.
*/
protected final void rangeCheck (int index, boolean insert)
{
if ((index < 0) || (insert ? (index > _size) : (index >= _size))) {
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + _size);
}
}
@Override
public BaseArrayList clone ()
{
try {
@SuppressWarnings("unchecked")
BaseArrayList dup = (BaseArrayList)super.clone();
if (_elements != null) {
dup._elements = _elements.clone();
}
return dup;
} catch (CloneNotSupportedException cnse) {
throw new AssertionError(cnse); // won't happen; we are Cloneable
}
}
/** The array of elements in our list. */
protected E[] _elements;
/** The number of elements in our list. */
protected int _size;
/** Avoid serialization annoyance. */
private static final long serialVersionUID = 1;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy