harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r385537 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util: HashMap.java HashSet.java LinkedHashMap.java
Date Mon, 13 Mar 2006 13:00:12 GMT
Author: tellison
Date: Mon Mar 13 05:00:08 2006
New Revision: 385537

URL: http://svn.apache.org/viewcvs?rev=385537&view=rev
Log:
Fixes to HashMap:
 - rename HashMapEntry to Entry 
 - represent load factor as a float
 - declare invariant fields as final 
 - update serialization to reflect modifications

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java?rev=385537&r1=385536&r2=385537&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
Mon Mar 13 05:00:08 2006
@@ -15,17 +15,14 @@
 
 package java.util;
 
-
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.ObjectStreamField;
 import java.io.Serializable;
 
 /**
  * HashMap is an implementation of Map. All optional operations are supported,
  * adding and removing. Keys and values can be any objects.
- * 
  */
 public class HashMap extends AbstractMap implements Map, Cloneable,
 		Serializable {
@@ -33,9 +30,9 @@
 
 	transient int elementCount;
 
-	transient HashMapEntry[] elementData;
+	transient Entry[] elementData;
 
-	int loadFactor;
+	final float loadFactor;
 
 	int threshold;
 
@@ -43,17 +40,17 @@
 
 	private static final int DEFAULT_SIZE = 16;
 
-	static class HashMapEntry extends MapEntry {
-		HashMapEntry next;
+	static class Entry extends MapEntry {
+		Entry next;
 
-		HashMapEntry(Object theKey, Object theValue) {
+		Entry(Object theKey, Object theValue) {
 			super(theKey, theValue);
 		}
 
 		public Object clone() {
-			HashMapEntry entry = (HashMapEntry) super.clone();
+			Entry entry = (Entry) super.clone();
 			if (next != null)
-				entry.next = (HashMapEntry) next.clone();
+				entry.next = (Entry) next.clone();
 			return entry;
 		}
 
@@ -67,15 +64,15 @@
 
 		int expectedModCount;
 
-		MapEntry.Type type;
+		final MapEntry.Type type;
 
 		boolean canRemove = false;
 
-		HashMapEntry entry;
+		Entry entry;
 
-		HashMapEntry lastEntry;
+		Entry lastEntry;
 
-		HashMap associatedMap;
+		final HashMap associatedMap;
 
 		HashMapIterator(MapEntry.Type value, HashMap hm) {
 			associatedMap = hm;
@@ -105,7 +102,7 @@
 			if (!hasNext())
 				throw new NoSuchElementException();
 
-			HashMapEntry result;
+			Entry result;
 			if (entry == null) {
 				result = lastEntry = associatedMap.elementData[position++];
 				entry = lastEntry.next;
@@ -139,7 +136,7 @@
 	}
 
 	static class HashMapEntrySet extends AbstractSet {
-		private HashMap associatedMap;
+		private final HashMap associatedMap;
 
 		public HashMapEntrySet(HashMap hm) {
 			associatedMap = hm;
@@ -167,8 +164,8 @@
 
 		public boolean contains(Object object) {
 			if (object instanceof Map.Entry) {
-				HashMapEntry entry = associatedMap
-						.getEntry(((Map.Entry) object).getKey());
+				Entry entry = associatedMap.getEntry(((Map.Entry) object)
+						.getKey());
 				return object.equals(entry);
 			}
 			return false;
@@ -189,8 +186,8 @@
 	 * @param s
 	 * @return Reference to the element array
 	 */
-	HashMapEntry[] newElementArray(int s) {
-		return new HashMapEntry[s];
+	Entry[] newElementArray(int s) {
+		return new Entry[s];
 	}
 
 	/**
@@ -214,7 +211,7 @@
 		if (capacity >= 0) {
 			elementCount = 0;
 			elementData = newElementArray(capacity == 0 ? 1 : capacity);
-			loadFactor = 7500; // Default load factor of 0.75
+			loadFactor = 0.75f; // Default load factor of 0.75
 			computeMaxSize();
 		} else
 			throw new IllegalArgumentException();
@@ -238,7 +235,7 @@
 		if (capacity >= 0 && loadFactor > 0) {
 			elementCount = 0;
 			elementData = newElementArray(capacity == 0 ? 1 : capacity);
-			this.loadFactor = (int) (loadFactor * 10000);
+			this.loadFactor = loadFactor;
 			computeMaxSize();
 		} else
 			throw new IllegalArgumentException();
@@ -281,10 +278,10 @@
 		try {
 			HashMap map = (HashMap) super.clone();
 			map.elementData = newElementArray(elementData.length);
-			HashMapEntry entry;
+			Entry entry;
 			for (int i = 0; i < elementData.length; i++) {
 				if ((entry = elementData[i]) != null)
-					map.elementData[i] = (HashMapEntry) entry.clone();
+					map.elementData[i] = (Entry) entry.clone();
 			}
 			return map;
 		} catch (CloneNotSupportedException e) {
@@ -293,7 +290,7 @@
 	}
 
 	private void computeMaxSize() {
-		threshold = (int) ((long) elementData.length * loadFactor / 10000);
+		threshold = (int) (elementData.length * loadFactor);
 	}
 
 	/**
@@ -333,7 +330,7 @@
 	public boolean containsValue(Object value) {
 		if (value != null) {
 			for (int i = elementData.length; --i >= 0;) {
-				HashMapEntry entry = elementData[i];
+				Entry entry = elementData[i];
 				while (entry != null) {
 					if (value.equals(entry.value))
 						return true;
@@ -342,7 +339,7 @@
 			}
 		} else {
 			for (int i = elementData.length; --i >= 0;) {
-				HashMapEntry entry = elementData[i];
+				Entry entry = elementData[i];
 				while (entry != null) {
 					if (entry.value == null)
 						return true;
@@ -372,14 +369,14 @@
 	 * @return the value of the mapping with the specified key
 	 */
 	public Object get(Object key) {
-		HashMapEntry m = getEntry(key);
+		Entry m = getEntry(key);
 		if (m != null) {
 			return m.value;
 		}
 		return null;
 	}
 
-	HashMapEntry getEntry(Object key) {
+	Entry getEntry(Object key) {
 		int index = getModuloHash(key);
 		return findEntry(key, index);
 	}
@@ -390,8 +387,8 @@
 		return (key.hashCode() & 0x7FFFFFFF) % elementData.length;
 	}
 
-	HashMapEntry findEntry(Object key, int index) {
-		HashMapEntry m;
+	Entry findEntry(Object key, int index) {
+		Entry m;
 		m = elementData[index];
 		if (key != null) {
 			while (m != null && !keysEqual(key, m.key))
@@ -468,7 +465,7 @@
 	 */
 	public Object put(Object key, Object value) {
 		int index = getModuloHash(key);
-		HashMapEntry entry = findEntry(key, index);
+		Entry entry = findEntry(key, index);
 
 		if (entry == null) {
 			modCount++;
@@ -484,8 +481,8 @@
 		return result;
 	}
 
-	HashMapEntry createEntry(Object key, int index, Object value) {
-		HashMapEntry entry = new HashMapEntry(key, value);
+	Entry createEntry(Object key, int index, Object value) {
+		Entry entry = new Entry(key, value);
 		entry.next = elementData[index];
 		elementData[index] = entry;
 		return entry;
@@ -505,14 +502,14 @@
 		int length = elementData.length << 1;
 		if (length == 0)
 			length = 1;
-		HashMapEntry[] newData = newElementArray(length);
+		Entry[] newData = newElementArray(length);
 		for (int i = 0; i < elementData.length; i++) {
-			HashMapEntry entry = elementData[i];
+			Entry entry = elementData[i];
 			while (entry != null) {
 				Object key = entry.key;
 				int index = key == null ? 0 : (key.hashCode() & 0x7FFFFFFF)
 						% length;
-				HashMapEntry next = entry.next;
+				Entry next = entry.next;
 				entry.next = newData[index];
 				newData[index] = entry;
 				entry = next;
@@ -531,17 +528,17 @@
 	 *         this HashMap
 	 */
 	public Object remove(Object key) {
-		HashMapEntry entry = removeEntry(key);
+		Entry entry = removeEntry(key);
 		if (entry != null) {
 			return entry.value;
 		}
 		return null;
 	}
 
-	HashMapEntry removeEntry(Object key) {
+	Entry removeEntry(Object key) {
 		int index = 0;
-		HashMapEntry entry;
-		HashMapEntry last = null;
+		Entry entry;
+		Entry last = null;
 		if (key != null) {
 			index = (key.hashCode() & 0x7FFFFFFF) % elementData.length;
 			entry = elementData[index];
@@ -610,20 +607,13 @@
 		return valuesCollection;
 	}
 
-	private static final ObjectStreamField[] serialPersistentFields = {
-			new ObjectStreamField("loadFactor", Float.TYPE),
-			new ObjectStreamField("threshold", Integer.TYPE) };
-
 	private void writeObject(ObjectOutputStream stream) throws IOException {
-		ObjectOutputStream.PutField fields = stream.putFields();
-		fields.put("loadFactor", (float) loadFactor / 10000);
-		fields.put("threshold", threshold);
-		stream.writeFields();
+		stream.defaultWriteObject();
 		stream.writeInt(elementData.length);
 		stream.writeInt(elementCount);
 		Iterator iterator = entrySet().iterator();
 		while (iterator.hasNext()) {
-			HashMapEntry entry = (HashMapEntry) iterator.next();
+			Entry entry = (Entry) iterator.next();
 			stream.writeObject(entry.key);
 			stream.writeObject(entry.value);
 			entry = entry.next;
@@ -632,11 +622,9 @@
 
 	private void readObject(ObjectInputStream stream) throws IOException,
 			ClassNotFoundException {
-		ObjectInputStream.GetField fields = stream.readFields();
-		loadFactor = (int) (fields.get("loadFactor", 0.75f) * 10000);
-		threshold = fields.get("threshold", 0);
+		stream.defaultReadObject();
 		int length = stream.readInt();
-		elementData = new HashMapEntry[length];
+		elementData = new Entry[length];
 		elementCount = stream.readInt();
 		for (int i = elementCount; --i >= 0;) {
 			Object key = stream.readObject();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java?rev=385537&r1=385536&r2=385537&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
Mon Mar 13 05:00:08 2006
@@ -176,10 +176,10 @@
 	private void writeObject(ObjectOutputStream stream) throws IOException {
 		stream.defaultWriteObject();
 		stream.writeInt(backingMap.elementData.length);
-		stream.writeFloat((float) backingMap.loadFactor / 10000);
+		stream.writeFloat(backingMap.loadFactor);
 		stream.writeInt(backingMap.elementCount);
 		for (int i = backingMap.elementData.length; --i >= 0;) {
-			HashMap.HashMapEntry entry = backingMap.elementData[i];
+			HashMap.Entry entry = backingMap.elementData[i];
 			while (entry != null) {
 				stream.writeObject(entry.key);
 				entry = entry.next;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java?rev=385537&r1=385536&r2=385537&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
Mon Mar 13 05:00:08 2006
@@ -17,14 +17,22 @@
 
 
 /**
- * LinkedHashMap
- * 
+ * LinkedHashMap is a variant on HashMap. Its entries are kept in a doubly-linked list.
+ * The iteration order is, by default, the order in which keys were inserted.
+ * <p> 
+ * If the three arg constructor is used, and <code>order</code> is specified
as <code>true</code>, 
+ * the iteration would be in the order that entries were accessed. The access order gets

+ * affected by put(), get(), putAll() operations, but not by operations on the collection
views.
+ * <p>
+ * Null elements are allowed, and all the optional Map operations are supported.
+ * <p>
+ *
  */
 public class LinkedHashMap extends HashMap {
 	
 	private static final long serialVersionUID = 3801124242820219131L;
 
-	private boolean accessOrder;
+	private final boolean accessOrder;
 
 	transient private LinkedHashMapEntry head, tail;
 
@@ -173,7 +181,7 @@
 		}
 	}
 
-	static final class LinkedHashMapEntry extends HashMapEntry {
+	static final class LinkedHashMapEntry extends Entry {
 		LinkedHashMapEntry chainForward, chainBackward;
 
 		LinkedHashMapEntry(Object theKey, Object theValue) {
@@ -199,7 +207,7 @@
 	 * @param s
 	 * @return Reference to the element array
 	 */
-	HashMapEntry[] newElementArray(int s) {
+	Entry[] newElementArray(int s) {
 		return new LinkedHashMapEntry[s];
 	}
 
@@ -233,9 +241,9 @@
 	/*
 	 * @param key
 	 * @param index
-	 * @return
+	 * @return Entry
 	 */
-	HashMapEntry createEntry(Object key, int index, Object value) {
+	Entry createEntry(Object key, int index, Object value) {
 		LinkedHashMapEntry m = new LinkedHashMapEntry(key, value);
 		m.next = elementData[index];
 		elementData[index] = m;



Mime
View raw message