You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/06/29 03:42:20 UTC
svn commit: r417915 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util:
Vector.java WeakHashMap.java
Author: ndbeyer
Date: Wed Jun 28 18:42:20 2006
New Revision: 417915
URL: http://svn.apache.org/viewvc?rev=417915&view=rev
Log:
Add override annotations and misc code cleanup.
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/WeakHashMap.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java?rev=417915&r1=417914&r2=417915&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java Wed Jun 28 18:42:20 2006
@@ -54,7 +54,7 @@
/**
* How many elements should be added to the vector when it is detected that
- * it needs to grow to accomodate extra entries.
+ * it needs to grow to accommodate extra entries.
*/
protected int capacityIncrement;
@@ -89,7 +89,7 @@
public Vector(int capacity, int capacityIncrement) {
elementCount = 0;
try {
- elementData = (E[]) new Object[capacity];
+ elementData = newElementArray(capacity);
} catch (NegativeArraySizeException e) {
throw new IllegalArgumentException();
}
@@ -112,6 +112,11 @@
elementData[elementCount++] = it.next();
}
}
+
+ @SuppressWarnings("unchecked")
+ private E[] newElementArray(int size) {
+ return (E[])new Object[size];
+ }
/**
* Adds the specified object into this Vector at the specified location. The
@@ -130,7 +135,8 @@
* @see #addElement
* @see #size
*/
- public void add(int location, E object) {
+ @Override
+ public void add(int location, E object) {
insertElementAt(object, location);
}
@@ -141,7 +147,8 @@
* the object to add to the Vector
* @return true
*/
- public boolean add(E object) {
+ @Override
+ public boolean add(E object) {
addElement(object);
return true;
}
@@ -161,7 +168,8 @@
* when <code>location < 0</code> or
* <code>location > size()</code>
*/
- public synchronized boolean addAll(int location, Collection<? extends E> collection) {
+ @Override
+ public synchronized boolean addAll(int location, Collection<? extends E> collection) {
if (0 <= location && location <= elementCount) {
int size = collection.size();
if (size == 0) {
@@ -195,7 +203,8 @@
* the Collection of objects
* @return true if this Vector is modified, false otherwise
*/
- public synchronized boolean addAll(Collection<? extends E> collection) {
+ @Override
+ public synchronized boolean addAll(Collection<? extends E> collection) {
return addAll(elementCount, collection);
}
@@ -231,7 +240,8 @@
* @see #isEmpty
* @see #size
*/
- public void clear() {
+ @Override
+ public void clear() {
removeAllElements();
}
@@ -243,7 +253,9 @@
*
* @see java.lang.Cloneable
*/
- public synchronized Object clone() {
+ @Override
+ @SuppressWarnings("unchecked")
+ public synchronized Object clone() {
try {
Vector<E> vector = (Vector<E>) super.clone();
vector.elementData = elementData.clone();
@@ -264,7 +276,8 @@
* @see #indexOf(Object, int)
* @see java.lang.Object#equals
*/
- public boolean contains(Object object) {
+ @Override
+ public boolean contains(Object object) {
return indexOf(object, 0) != -1;
}
@@ -276,7 +289,8 @@
* @return true if all objects in the specified Collection are elements of
* this Vector, false otherwise
*/
- public synchronized boolean containsAll(Collection<?> collection) {
+ @Override
+ public synchronized boolean containsAll(Collection<?> collection) {
return super.containsAll(collection);
}
@@ -372,7 +386,8 @@
*
* @see #hashCode
*/
- public synchronized boolean equals(Object object) {
+ @Override
+ public synchronized boolean equals(Object object) {
if (this == object) {
return true;
}
@@ -426,19 +441,21 @@
*
* @see #size
*/
- public E get(int location) {
+ @Override
+ public E get(int location) {
return elementAt(location);
}
private void grow(int newCapacity) {
- E[] newData = (E[]) new Object[newCapacity];
+ E[] newData = newElementArray(newCapacity);
// Assumes elementCount is <= newCapacity
+ assert elementCount <= newCapacity;
System.arraycopy(elementData, 0, newData, 0, elementCount);
elementData = newData;
}
/**
- * jit optimization
+ * JIT optimization
*/
private void growByOne() {
int adding = 0;
@@ -450,7 +467,7 @@
adding = capacityIncrement;
}
- E[] newData = (E[]) new Object[elementData.length + adding];
+ E[] newData = newElementArray(elementData.length + adding);
System.arraycopy(elementData, 0, newData, 0, elementCount);
elementData = newData;
}
@@ -470,7 +487,7 @@
adding += capacityIncrement;
}
}
- E[] newData = (E[]) new Object[elementData.length + adding];
+ E[] newData = newElementArray(elementData.length + adding);
System.arraycopy(elementData, 0, newData, 0, elementCount);
elementData = newData;
}
@@ -483,7 +500,8 @@
*
* @see #equals
*/
- public synchronized int hashCode() {
+ @Override
+ public synchronized int hashCode() {
int result = 1;
for (int i = 0; i < elementCount; i++) {
result = (31 * result)
@@ -506,7 +524,8 @@
* @see #lastIndexOf(Object)
* @see #lastIndexOf(Object, int)
*/
- public int indexOf(Object object) {
+ @Override
+ public int indexOf(Object object) {
return indexOf(object, 0);
}
@@ -588,7 +607,8 @@
*
* @see #size
*/
- public synchronized boolean isEmpty() {
+ @Override
+ public synchronized boolean isEmpty() {
return elementCount == 0;
}
@@ -626,7 +646,8 @@
* @see #indexOf(Object)
* @see #indexOf(Object, int)
*/
- public synchronized int lastIndexOf(Object object) {
+ @Override
+ public synchronized int lastIndexOf(Object object) {
return lastIndexOf(object, elementCount - 1);
}
@@ -675,7 +696,8 @@
*
* @see java.util.List#remove(int)
*/
- public synchronized E remove(int location) {
+ @Override
+ public synchronized E remove(int location) {
if (location < elementCount) {
E result = elementData[location];
elementCount--;
@@ -704,7 +726,8 @@
* @see #removeElementAt
* @see #size
*/
- public boolean remove(Object object) {
+ @Override
+ public boolean remove(Object object) {
return removeElement(object);
}
@@ -716,7 +739,8 @@
* the Collection of objects to remove
* @return true if this Vector is modified, false otherwise
*/
- public synchronized boolean removeAll(Collection<?> collection) {
+ @Override
+ public synchronized boolean removeAll(Collection<?> collection) {
return super.removeAll(collection);
}
@@ -796,7 +820,8 @@
* when <code>start < 0, start > end</code> or
* <code>end > size()</code>
*/
- protected void removeRange(int start, int end) {
+ @Override
+ protected void removeRange(int start, int end) {
if (start >= 0 && start <= end && end <= size()) {
if (start == end) {
return;
@@ -825,7 +850,8 @@
* the Collection of objects to retain
* @return true if this Vector is modified, false otherwise
*/
- public synchronized boolean retainAll(Collection<?> collection) {
+ @Override
+ public synchronized boolean retainAll(Collection<?> collection) {
return super.retainAll(collection);
}
@@ -844,7 +870,8 @@
*
* @see #size
*/
- public synchronized E set(int location, E object) {
+ @Override
+ public synchronized E set(int location, E object) {
if (location < elementCount) {
E result = elementData[location];
elementData[location] = object;
@@ -907,7 +934,8 @@
* @see #elementCount
* @see #lastElement
*/
- public synchronized int size() {
+ @Override
+ public synchronized int size() {
return elementCount;
}
@@ -926,7 +954,8 @@
* when <code>start < 0 or <code>end > size()</code>
* @exception IllegalArgumentException when <code>start > end</code>
*/
- public synchronized List<E> subList(int start, int end) {
+ @Override
+ public synchronized List<E> subList(int start, int end) {
return new Collections.SynchronizedRandomAccessList<E>(
super.subList(start, end), this);
}
@@ -936,7 +965,8 @@
*
* @return an array of the elements from this Vector
*/
- public synchronized Object[] toArray() {
+ @Override
+ public synchronized Object[] toArray() {
Object[] result = new Object[elementCount];
System.arraycopy(elementData, 0, result, 0, elementCount);
return result;
@@ -957,7 +987,9 @@
* when the type of an element in this Vector cannot be
* stored in the type of the specified array
*/
- public synchronized <T> T[] toArray(T[] contents) {
+ @Override
+ @SuppressWarnings("unchecked")
+ public synchronized <T> T[] toArray(T[] contents) {
if (elementCount > contents.length) {
Class<?> ct = contents.getClass().getComponentType();
contents = (T[]) Array.newInstance(ct, elementCount);
@@ -976,7 +1008,8 @@
*
* @see #elements
*/
- public synchronized String toString() {
+ @Override
+ public synchronized String toString() {
if (elementCount == 0) {
return "[]";
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/WeakHashMap.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/WeakHashMap.java?rev=417915&r1=417914&r2=417915&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/WeakHashMap.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/WeakHashMap.java Wed Jun 28 18:42:20 2006
@@ -44,8 +44,7 @@
private static final int DEFAULT_SIZE = 16;
//Simple utility method to isolate unchecked cast for array creation
- //TODO Uncomment annotation, when available
- //@SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
private static <K, V> Entry<K, V>[] newEntryArray(int size) {
return new Entry[size];
}
@@ -85,9 +84,11 @@
return result;
}
+ @Override
public boolean equals(Object other) {
- if (!(other instanceof Map.Entry))
+ if (!(other instanceof Map.Entry)) {
return false;
+ }
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other;
Object key = super.get();
return (key == null ? key == entry.getKey() : key.equals(entry
@@ -96,10 +97,12 @@
.equals(entry.getValue()));
}
+ @Override
public int hashCode() {
return hash + (value == null ? 0 : value.hashCode());
}
+ @Override
public String toString() {
return super.get() + "=" + value;
}
@@ -120,15 +123,19 @@
}
public boolean hasNext() {
- if (nextEntry != null)
+ if (nextEntry != null) {
return true;
+ }
while (true) {
if (nextEntry == null) {
- while (position < elementData.length)
- if ((nextEntry = elementData[position++]) != null)
+ while (position < elementData.length) {
+ if ((nextEntry = elementData[position++]) != null) {
break;
- if (nextEntry == null)
+ }
+ }
+ if (nextEntry == null) {
return false;
+ }
}
// ensure key of next entry is not gc'ed
nextKey = nextEntry.get();
@@ -148,10 +155,12 @@
// free the key
nextKey = null;
return result;
- } else
+ } else {
throw new NoSuchElementException();
- } else
+ }
+ } else {
throw new ConcurrentModificationException();
+ }
}
public void remove() {
@@ -161,10 +170,12 @@
currentEntry = null;
expectedModCount++;
// cannot poll() as that would change the expectedModCount
- } else
+ } else {
throw new IllegalStateException();
- } else
+ }
+ } else {
throw new ConcurrentModificationException();
+ }
}
}
@@ -191,8 +202,9 @@
loadFactor = 7500; // Default load factor of 0.75
computeMaxSize();
referenceQueue = new ReferenceQueue<K>();
- } else
+ } else {
throw new IllegalArgumentException();
+ }
}
/**
@@ -215,8 +227,9 @@
this.loadFactor = (int) (loadFactor * 10000);
computeMaxSize();
referenceQueue = new ReferenceQueue<K>();
- } else
+ } else {
throw new IllegalArgumentException();
+ }
}
/**
@@ -237,6 +250,7 @@
* @see #isEmpty
* @see #size
*/
+ @Override
public void clear() {
if (elementCount > 0) {
elementCount = 0;
@@ -260,6 +274,7 @@
* @return true if <code>key</code> is a key of this WeakHashMap, false
* otherwise
*/
+ @Override
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
@@ -272,17 +287,21 @@
*
* @return a Set of the mappings
*/
+ @Override
public Set<Map.Entry<K, V>> entrySet() {
poll();
return new AbstractSet<Map.Entry<K, V>>() {
+ @Override
public int size() {
return WeakHashMap.this.size();
}
+ @Override
public void clear() {
WeakHashMap.this.clear();
}
+ @Override
public boolean remove(Object object) {
if (contains(object)) {
WeakHashMap.this.remove(((Map.Entry) object).getKey());
@@ -291,6 +310,7 @@
return false;
}
+ @Override
public boolean contains(Object object) {
if (object instanceof Map.Entry) {
Entry<?, ?> entry = getEntry(((Map.Entry) object).getKey());
@@ -304,6 +324,7 @@
return false;
}
+ @Override
public Iterator<Map.Entry<K, V>> iterator() {
return new HashIterator<Map.Entry<K, V>>(new Entry.Type<Map.Entry<K, V>, K, V>() {
public Map.Entry<K, V> get(Map.Entry<K, V> entry) {
@@ -321,22 +342,27 @@
*
* @return a Set of the keys
*/
+ @Override
public Set<K> keySet() {
poll();
if (keySet == null) {
keySet = new AbstractSet<K>() {
+ @Override
public boolean contains(Object object) {
return containsKey(object);
}
+ @Override
public int size() {
return WeakHashMap.this.size();
}
+ @Override
public void clear() {
WeakHashMap.this.clear();
}
+ @Override
public boolean remove(Object key) {
if (containsKey(key)) {
WeakHashMap.this.remove(key);
@@ -345,6 +371,7 @@
return false;
}
+ @Override
public Iterator<K> iterator() {
return new HashIterator<K>(new Entry.Type<K, K, V>() {
public K get(Map.Entry<K, V> entry) {
@@ -364,22 +391,27 @@
*
* @return a Collection of the values
*/
+ @Override
public Collection<V> values() {
poll();
if (valuesCollection == null) {
valuesCollection = new AbstractCollection<V>() {
+ @Override
public int size() {
return WeakHashMap.this.size();
}
+ @Override
public void clear() {
WeakHashMap.this.clear();
}
+ @Override
public boolean contains(Object object) {
return containsValue(object);
}
+ @Override
public Iterator<V> iterator() {
return new HashIterator<V>(new Entry.Type<V, K, V>() {
public V get(Map.Entry<K, V> entry) {
@@ -399,22 +431,25 @@
* the key
* @return the value of the mapping with the specified key
*/
+ @Override
public V get(Object key) {
poll();
if (key != null) {
int index = (key.hashCode() & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
- if (key.equals(entry.get()))
+ if (key.equals(entry.get())) {
return entry.value;
+ }
entry = entry.next;
}
return null;
}
Entry<K, V> entry = elementData[0];
while (entry != null) {
- if (entry.isNull)
+ if (entry.isNull) {
return entry.value;
+ }
entry = entry.next;
}
return null;
@@ -426,16 +461,18 @@
int index = (key.hashCode() & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
- if (key.equals(entry.get()))
+ if (key.equals(entry.get())) {
return entry;
+ }
entry = entry.next;
}
return null;
}
Entry<K, V> entry = elementData[0];
while (entry != null) {
- if (entry.isNull)
+ if (entry.isNull) {
return entry;
+ }
entry = entry.next;
}
return null;
@@ -450,6 +487,7 @@
* @return true if <code>value</code> is a value in this WeakHashMap,
* false otherwise
*/
+ @Override
public boolean containsValue(Object value) {
poll();
if (value != null) {
@@ -458,8 +496,9 @@
while (entry != null) {
K key = entry.get();
if ((key != null || entry.isNull)
- && value.equals(entry.value))
+ && value.equals(entry.value)) {
return true;
+ }
entry = entry.next;
}
}
@@ -468,8 +507,9 @@
Entry<K, V> entry = elementData[i];
while (entry != null) {
K key = entry.get();
- if ((key != null || entry.isNull) && entry.value == null)
+ if ((key != null || entry.isNull) && entry.value == null) {
return true;
+ }
entry = entry.next;
}
}
@@ -484,11 +524,12 @@
*
* @see #size
*/
+ @Override
public boolean isEmpty() {
return size() == 0;
}
- //@SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
void poll() {
Entry<K, V> toRemove;
while ((toRemove = (Entry<K, V>)referenceQueue.poll()) != null) {
@@ -505,10 +546,11 @@
while (entry != null) {
if (toRemove == entry) {
modCount++;
- if (last == null)
+ if (last == null) {
elementData[index] = entry.next;
- else
+ } else {
last.next = entry.next;
+ }
elementCount--;
break;
}
@@ -527,6 +569,7 @@
* @return the value of any previous mapping with the specified key or null
* if there was no mapping
*/
+ @Override
public V put(K key, V value) {
poll();
int index = 0;
@@ -534,12 +577,14 @@
if (key != null) {
index = (key.hashCode() & 0x7FFFFFFF) % elementData.length;
entry = elementData[index];
- while (entry != null && !key.equals(entry.get()))
+ while (entry != null && !key.equals(entry.get())) {
entry = entry.next;
+ }
} else {
entry = elementData[0];
- while (entry != null && !entry.isNull)
+ while (entry != null && !entry.isNull) {
entry = entry.next;
+ }
}
if (entry == null) {
modCount++;
@@ -560,8 +605,9 @@
private void rehash() {
int length = elementData.length << 1;
- if (length == 0)
+ if (length == 0) {
length = 1;
+ }
Entry<K, V>[] newData = newEntryArray(length);
for (int i = 0; i < elementData.length; i++) {
Entry<K, V> entry = elementData[i];
@@ -586,6 +632,7 @@
* @return the value of the removed mapping or null if key is not a key in
* this WeakHashMap
*/
+ @Override
public V remove(Object key) {
poll();
int index = 0;
@@ -606,10 +653,11 @@
}
if (entry != null) {
modCount++;
- if (last == null)
+ if (last == null) {
elementData[index] = entry.next;
- else
+ } else {
last.next = entry.next;
+ }
elementCount--;
return entry.value;
}
@@ -621,6 +669,7 @@
*
* @return the number of mappings in this WeakHashMap
*/
+ @Override
public int size() {
poll();
return elementCount;