Commit f24dbacf by Bryce McKinlay Committed by Bryce McKinlay

Vector.java (ensureCapacity): Don't increment modCount.

2000-11-27  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/util/Vector.java (ensureCapacity): Don't increment modCount.
	(addElement): Don't increment elementCount twice. Doh.
	* java/util/ArrayList.java (add): Only call ensureCapacity if the
	array needs to be expanded.
	(addAll): Ditto.
	* java/util/Collections.java (UnmodifiableCollection): Implement
	toString().
	(UnmodifiableList): Throw UnsupportedOperationException from
	modification methods. Set `l' from the one-parameter constructor.
	(UnmodifiableMap): Implement toString().
	(SynchronizedCollection): Ditto.
	(SynchronizedList): Set `l' from the one-parameter constructor.
	(SynchronizedSortedSet): Set `ss' from the one-parameter constructor.
	(SynchronizedMap): Implement toString().

From-SVN: r37785
parent 27e2564a
2000-11-27 Bryce McKinlay <bryce@albatross.co.nz>
* java/util/Vector.java (ensureCapacity): Don't increment modCount.
(addElement): Don't increment elementCount twice. Doh.
* java/util/ArrayList.java (add): Only call ensureCapacity if the
array needs to be expanded.
(addAll): Ditto.
* java/util/Collections.java (UnmodifiableCollection): Implement
toString().
(UnmodifiableList): Throw UnsupportedOperationException from
modification methods. Set `l' from the one-parameter constructor.
(UnmodifiableMap): Implement toString().
(SynchronizedCollection): Ditto.
(SynchronizedList): Set `l' from the one-parameter constructor.
(SynchronizedSortedSet): Set `ss' from the one-parameter constructor.
(SynchronizedMap): Implement toString().
Sun Nov 26 22:35:53 2000 Anthony Green <green@redhat.com> Sun Nov 26 22:35:53 2000 Anthony Green <green@redhat.com>
* javax/naming/InitialContext.java (rebind): Implement. * javax/naming/InitialContext.java (rebind): Implement.
......
...@@ -43,7 +43,7 @@ import java.io.ObjectStreamField; ...@@ -43,7 +43,7 @@ import java.io.ObjectStreamField;
* to or removing from the end of a list, checking the size, &c. * to or removing from the end of a list, checking the size, &c.
* *
* @author Jon A. Zeppieri * @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.AbstractList
* @see java.util.List * @see java.util.List
*/ */
...@@ -127,6 +127,7 @@ public class ArrayList extends AbstractList ...@@ -127,6 +127,7 @@ public class ArrayList extends AbstractList
public boolean add(Object e) public boolean add(Object e)
{ {
modCount++; modCount++;
if (size == data.length)
ensureCapacity(size + 1); ensureCapacity(size + 1);
data[size++] = e; data[size++] = e;
return true; return true;
...@@ -204,6 +205,7 @@ public class ArrayList extends AbstractList ...@@ -204,6 +205,7 @@ public class ArrayList extends AbstractList
if (index < 0 || index > size) if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" +
size); size);
if (size == data.length)
ensureCapacity(size + 1); ensureCapacity(size + 1);
if (index != size) if (index != size)
System.arraycopy(data, index, data, index + 1, size - index); System.arraycopy(data, index, data, index + 1, size - index);
...@@ -239,6 +241,7 @@ public class ArrayList extends AbstractList ...@@ -239,6 +241,7 @@ public class ArrayList extends AbstractList
Iterator itr = c.iterator(); Iterator itr = c.iterator();
int csize = c.size(); int csize = c.size();
if (csize + size > data.length)
ensureCapacity(size + csize); ensureCapacity(size + csize);
int end = index + csize; int end = index + csize;
if (size > 0 && index != size) if (size > 0 && index != size)
......
...@@ -43,7 +43,6 @@ import java.io.Serializable; ...@@ -43,7 +43,6 @@ import java.io.Serializable;
*/ */
public class Collections public class Collections
{ {
/** /**
* This class is non-instantiable. * This class is non-instantiable.
*/ */
...@@ -58,7 +57,6 @@ public class Collections ...@@ -58,7 +57,6 @@ public class Collections
*/ */
public static final Set EMPTY_SET = new AbstractSet() public static final Set EMPTY_SET = new AbstractSet()
{ {
public int size() public int size()
{ {
return 0; return 0;
...@@ -97,7 +95,6 @@ public class Collections ...@@ -97,7 +95,6 @@ public class Collections
*/ */
public static final List EMPTY_LIST = new AbstractList() public static final List EMPTY_LIST = new AbstractList()
{ {
public int size() public int size()
{ {
return 0; return 0;
...@@ -116,7 +113,6 @@ public class Collections ...@@ -116,7 +113,6 @@ public class Collections
*/ */
public static final Map EMPTY_MAP = new AbstractMap() public static final Map EMPTY_MAP = new AbstractMap()
{ {
public Set entrySet() public Set entrySet()
{ {
return EMPTY_SET; return EMPTY_SET;
...@@ -147,7 +143,6 @@ public class Collections ...@@ -147,7 +143,6 @@ public class Collections
*/ */
private static int search(List l, Object key, final Comparator c) private static int search(List l, Object key, final Comparator c)
{ {
int pos = 0; int pos = 0;
// We use a linear search using an iterator if we can guess that the list // We use a linear search using an iterator if we can guess that the list
...@@ -437,7 +432,6 @@ public class Collections ...@@ -437,7 +432,6 @@ public class Collections
// stated - I just would be amazed if it isn't... // stated - I just would be amazed if it isn't...
public static List nCopies(final int n, final Object o) public static List nCopies(final int n, final Object o)
{ {
// Check for insane arguments // Check for insane arguments
if (n < 0) if (n < 0)
{ {
...@@ -552,7 +546,6 @@ public class Collections ...@@ -552,7 +546,6 @@ public class Collections
// Iterate backwards over l // Iterate backwards over l
while (i.hasPrevious()) while (i.hasPrevious())
{ {
// Obtain a random position to swap with. nextIndex is used so that the // Obtain a random position to swap with. nextIndex is used so that the
// range of the random number includes the current position. // range of the random number includes the current position.
int swap = r.nextInt(i.nextIndex()); int swap = r.nextInt(i.nextIndex());
...@@ -579,10 +572,8 @@ public class Collections ...@@ -579,10 +572,8 @@ public class Collections
// It's not serializable because the spec is broken. // It's not serializable because the spec is broken.
public static Set singleton(final Object o) public static Set singleton(final Object o)
{ {
return new AbstractSet() return new AbstractSet()
{ {
public int size() public int size()
{ {
return 1; return 1;
...@@ -592,7 +583,6 @@ public class Collections ...@@ -592,7 +583,6 @@ public class Collections
{ {
return new Iterator() return new Iterator()
{ {
private boolean hasNext = true; private boolean hasNext = true;
public boolean hasNext() public boolean hasNext()
...@@ -632,10 +622,8 @@ public class Collections ...@@ -632,10 +622,8 @@ public class Collections
// It's not serializable because the spec is broken. // It's not serializable because the spec is broken.
public static List singletonList(final Object o) public static List singletonList(final Object o)
{ {
return new AbstractList() return new AbstractList()
{ {
public int size() public int size()
{ {
return 1; return 1;
...@@ -666,7 +654,6 @@ public class Collections ...@@ -666,7 +654,6 @@ public class Collections
// It's not serializable because the spec is broken. // It's not serializable because the spec is broken.
public static Map singletonMap(final Object key, final Object value) public static Map singletonMap(final Object key, final Object value)
{ {
return new AbstractMap() return new AbstractMap()
{ {
public Set entrySet() public Set entrySet()
...@@ -811,7 +798,6 @@ public class Collections ...@@ -811,7 +798,6 @@ public class Collections
private static class UnmodifiableListIterator extends UnmodifiableIterator private static class UnmodifiableListIterator extends UnmodifiableIterator
implements ListIterator implements ListIterator
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private ListIterator li; private ListIterator li;
...@@ -910,12 +896,15 @@ public class Collections ...@@ -910,12 +896,15 @@ public class Collections
{ {
return c.toArray(a); return c.toArray(a);
} }
public String toString()
{
return c.toString();
}
} }
private static class UnmodifiableList extends UnmodifiableCollection private static class UnmodifiableList extends UnmodifiableCollection
implements List implements List
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
List l; List l;
...@@ -928,11 +917,11 @@ public class Collections ...@@ -928,11 +917,11 @@ public class Collections
public void add(int index, Object o) public void add(int index, Object o)
{ {
l.add(index, o); throw new UnsupportedOperationException();
} }
public boolean addAll(int index, Collection c) public boolean addAll(int index, Collection c)
{ {
return l.addAll(index, c); throw new UnsupportedOperationException();
} }
public boolean equals(Object o) public boolean equals(Object o)
{ {
...@@ -964,15 +953,11 @@ public class Collections ...@@ -964,15 +953,11 @@ public class Collections
} }
public Object remove(int index) public Object remove(int index)
{ {
return l.remove(index); throw new UnsupportedOperationException();
}
public boolean remove(Object o)
{
return l.remove(o);
} }
public Object set(int index, Object o) public Object set(int index, Object o)
{ {
return l.set(index, o); throw new UnsupportedOperationException();
} }
public List subList(int fromIndex, int toIndex) public List subList(int fromIndex, int toIndex)
{ {
...@@ -1000,7 +985,6 @@ public class Collections ...@@ -1000,7 +985,6 @@ public class Collections
private static class UnmodifiableSortedSet extends UnmodifiableSet private static class UnmodifiableSortedSet extends UnmodifiableSet
implements SortedSet implements SortedSet
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private SortedSet ss; private SortedSet ss;
...@@ -1039,7 +1023,6 @@ public class Collections ...@@ -1039,7 +1023,6 @@ public class Collections
private static class UnmodifiableMap implements Map, Serializable private static class UnmodifiableMap implements Map, Serializable
{ {
Map m; Map m;
public UnmodifiableMap(Map m) public UnmodifiableMap(Map m)
...@@ -1143,12 +1126,15 @@ public class Collections ...@@ -1143,12 +1126,15 @@ public class Collections
{ {
return new UnmodifiableCollection(m.values()); return new UnmodifiableCollection(m.values());
} }
public String toString()
{
return m.toString();
}
} }
private static class UnmodifiableSortedMap extends UnmodifiableMap private static class UnmodifiableSortedMap extends UnmodifiableMap
implements SortedMap implements SortedMap
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private SortedMap sm; private SortedMap sm;
...@@ -1226,7 +1212,6 @@ public class Collections ...@@ -1226,7 +1212,6 @@ public class Collections
private static class SynchronizedListIterator extends SynchronizedIterator private static class SynchronizedListIterator extends SynchronizedIterator
implements ListIterator implements ListIterator
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private ListIterator li; private ListIterator li;
...@@ -1389,12 +1374,18 @@ public class Collections ...@@ -1389,12 +1374,18 @@ public class Collections
return c.toArray(a); return c.toArray(a);
} }
} }
public String toString()
{
synchronized(sync)
{
return c.toString();
}
}
} }
private static class SynchronizedList extends SynchronizedCollection private static class SynchronizedList extends SynchronizedCollection
implements List implements List
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
List l; List l;
...@@ -1407,6 +1398,7 @@ public class Collections ...@@ -1407,6 +1398,7 @@ public class Collections
public SynchronizedList(List l) public SynchronizedList(List l)
{ {
super(l); super(l);
this.l = l;
} }
public void add(int index, Object o) public void add(int index, Object o)
...@@ -1505,7 +1497,6 @@ public class Collections ...@@ -1505,7 +1497,6 @@ public class Collections
private static class SynchronizedSet extends SynchronizedCollection private static class SynchronizedSet extends SynchronizedCollection
implements Set implements Set
{ {
public SynchronizedSet(Object sync, Set s) public SynchronizedSet(Object sync, Set s)
{ {
super(sync, s); super(sync, s);
...@@ -1534,7 +1525,6 @@ public class Collections ...@@ -1534,7 +1525,6 @@ public class Collections
private static class SynchronizedSortedSet extends SynchronizedSet private static class SynchronizedSortedSet extends SynchronizedSet
implements SortedSet implements SortedSet
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private SortedSet ss; private SortedSet ss;
...@@ -1547,6 +1537,7 @@ public class Collections ...@@ -1547,6 +1537,7 @@ public class Collections
public SynchronizedSortedSet(SortedSet ss) public SynchronizedSortedSet(SortedSet ss)
{ {
super(ss); super(ss);
this.ss = ss;
} }
public Comparator comparator() public Comparator comparator()
...@@ -1596,7 +1587,6 @@ public class Collections ...@@ -1596,7 +1587,6 @@ public class Collections
private static class SynchronizedMap implements Map, Serializable private static class SynchronizedMap implements Map, Serializable
{ {
Object sync; Object sync;
Map m; Map m;
...@@ -1634,7 +1624,7 @@ public class Collections ...@@ -1634,7 +1624,7 @@ public class Collections
} }
// This is one of the ickiest cases of nesting I've ever seen. It just // 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 // returns an SynchronizedIterator whos next() method returns a
// synchronized wrapper around its normal return value". // synchronized wrapper around its normal return value".
public Set entrySet() public Set entrySet()
...@@ -1772,12 +1762,18 @@ public class Collections ...@@ -1772,12 +1762,18 @@ public class Collections
return new SynchronizedCollection(sync, m.values()); return new SynchronizedCollection(sync, m.values());
} }
} }
public String toString()
{
synchronized(sync)
{
return m.toString();
}
}
} }
private static class SynchronizedSortedMap extends SynchronizedMap private static class SynchronizedSortedMap extends SynchronizedMap
implements SortedMap implements SortedMap
{ {
// This is stored both here and in the superclass, to avoid excessive // This is stored both here and in the superclass, to avoid excessive
// casting. // casting.
private SortedMap sm; private SortedMap sm;
...@@ -1790,6 +1786,7 @@ public class Collections ...@@ -1790,6 +1786,7 @@ public class Collections
public SynchronizedSortedMap(SortedMap sm) public SynchronizedSortedMap(SortedMap sm)
{ {
super(sm); super(sm);
this.sm = sm;
} }
public Comparator comparator() public Comparator comparator()
......
...@@ -178,7 +178,6 @@ public class Vector extends AbstractList ...@@ -178,7 +178,6 @@ public class Vector extends AbstractList
*/ */
public synchronized void ensureCapacity(int minCapacity) public synchronized void ensureCapacity(int minCapacity)
{ {
modCount++;
if (elementData.length >= minCapacity) if (elementData.length >= minCapacity)
return; return;
...@@ -459,7 +458,7 @@ public class Vector extends AbstractList ...@@ -459,7 +458,7 @@ public class Vector extends AbstractList
public synchronized void addElement(Object obj) public synchronized void addElement(Object obj)
{ {
if (elementCount == elementData.length) if (elementCount == elementData.length)
ensureCapacity(++elementCount); ensureCapacity(elementCount + 1);
modCount++; modCount++;
elementData[elementCount++] = obj; elementData[elementCount++] = obj;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment