* to or removing from the end of a list, checking the size, &c.
*
* @author Jon A. Zeppieri
- * @version $Id: ArrayList.java,v 1.3 2000/11/02 10:08:03 bryce Exp $
+ * @version $Id: ArrayList.java,v 1.4 2000/11/22 11:59:59 bryce Exp $
* @see java.util.AbstractList
* @see java.util.List
*/
public boolean add(Object e)
{
modCount++;
- ensureCapacity(size + 1);
+ if (size == data.length)
+ ensureCapacity(size + 1);
data[size++] = e;
return true;
}
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size:" +
size);
- ensureCapacity(size + 1);
+ if (size == data.length)
+ ensureCapacity(size + 1);
if (index != size)
System.arraycopy(data, index, data, index + 1, size - index);
data[index] = e;
Iterator itr = c.iterator();
int csize = c.size();
- ensureCapacity(size + csize);
+ if (csize + size > data.length)
+ ensureCapacity(size + csize);
int end = index + csize;
if (size > 0 && index != size)
System.arraycopy(data, index, data, end, csize);
*/
public class Collections
{
-
/**
* This class is non-instantiable.
*/
*/
public static final Set EMPTY_SET = new AbstractSet()
{
-
public int size()
{
return 0;
*/
public static final List EMPTY_LIST = new AbstractList()
{
-
public int size()
{
return 0;
*/
public static final Map EMPTY_MAP = new AbstractMap()
{
-
public Set entrySet()
{
return EMPTY_SET;
*/
private static int search(List l, Object key, final Comparator c)
{
-
int pos = 0;
// We use a linear search using an iterator if we can guess that the list
// stated - I just would be amazed if it isn't...
public static List nCopies(final int n, final Object o)
{
-
// Check for insane arguments
if (n < 0)
{
// Iterate backwards over l
while (i.hasPrevious())
{
-
// Obtain a random position to swap with. nextIndex is used so that the
// range of the random number includes the current position.
int swap = r.nextInt(i.nextIndex());
// It's not serializable because the spec is broken.
public static Set singleton(final Object o)
{
-
return new AbstractSet()
{
-
public int size()
{
return 1;
{
return new Iterator()
{
-
private boolean hasNext = true;
public boolean hasNext()
// It's not serializable because the spec is broken.
public static List singletonList(final Object o)
{
-
return new AbstractList()
{
-
public int size()
{
return 1;
// It's not serializable because the spec is broken.
public static Map singletonMap(final Object key, final Object value)
{
-
return new AbstractMap()
{
public Set entrySet()
private static class UnmodifiableListIterator extends UnmodifiableIterator
implements ListIterator
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private ListIterator li;
{
return c.toArray(a);
}
+ public String toString()
+ {
+ return c.toString();
+ }
}
private static class UnmodifiableList extends UnmodifiableCollection
implements List
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
List l;
public void add(int index, Object o)
{
- l.add(index, o);
+ throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection c)
{
- return l.addAll(index, c);
+ throw new UnsupportedOperationException();
}
public boolean equals(Object o)
{
}
public Object remove(int index)
{
- return l.remove(index);
- }
- public boolean remove(Object o)
- {
- return l.remove(o);
+ throw new UnsupportedOperationException();
}
public Object set(int index, Object o)
{
- return l.set(index, o);
+ throw new UnsupportedOperationException();
}
public List subList(int fromIndex, int toIndex)
{
private static class UnmodifiableSortedSet extends UnmodifiableSet
implements SortedSet
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedSet ss;
private static class UnmodifiableMap implements Map, Serializable
{
-
Map m;
public UnmodifiableMap(Map m)
{
return new UnmodifiableCollection(m.values());
}
+ public String toString()
+ {
+ return m.toString();
+ }
}
private static class UnmodifiableSortedMap extends UnmodifiableMap
implements SortedMap
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedMap sm;
private static class SynchronizedListIterator extends SynchronizedIterator
implements ListIterator
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private ListIterator li;
return c.toArray(a);
}
}
+ public String toString()
+ {
+ synchronized(sync)
+ {
+ return c.toString();
+ }
+ }
}
private static class SynchronizedList extends SynchronizedCollection
implements List
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
List l;
public SynchronizedList(List l)
{
super(l);
+ this.l = l;
}
public void add(int index, Object o)
private static class SynchronizedSet extends SynchronizedCollection
implements Set
{
-
public SynchronizedSet(Object sync, Set s)
{
super(sync, s);
private static class SynchronizedSortedSet extends SynchronizedSet
implements SortedSet
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedSet ss;
public SynchronizedSortedSet(SortedSet ss)
{
super(ss);
+ this.ss = ss;
}
public Comparator comparator()
private static class SynchronizedMap implements Map, Serializable
{
-
Object sync;
Map m;
}
// This is one of the ickiest cases of nesting I've ever seen. It just
- // means "return an SynchronizedSet, except that the iterator() method
+ // means "return a SynchronizedSet, except that the iterator() method
// returns an SynchronizedIterator whos next() method returns a
// synchronized wrapper around its normal return value".
public Set entrySet()
return new SynchronizedCollection(sync, m.values());
}
}
+ public String toString()
+ {
+ synchronized(sync)
+ {
+ return m.toString();
+ }
+ }
}
private static class SynchronizedSortedMap extends SynchronizedMap
implements SortedMap
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedMap sm;
public SynchronizedSortedMap(SortedMap sm)
{
super(sm);
+ this.sm = sm;
}
public Comparator comparator()