Author: jason.greene(a)jboss.com
Date: 2009-01-05 11:05:58 -0500 (Mon, 05 Jan 2009)
New Revision: 7373
Modified:
core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java
Log:
Fix serialization bug
Fix SimpleEntry.hashCode()
Modified: core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java 2009-01-05 15:52:48
UTC (rev 7372)
+++ core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java 2009-01-05 16:05:58
UTC (rev 7373)
@@ -68,12 +68,12 @@
/**
* The open-addressed table
*/
- private Entry<K, V>[] table;
+ private transient Entry<K, V>[] table;
/**
* The current number of key-value pairs
*/
- private int size;
+ private transient int size;
/**
* The next resize
@@ -98,19 +98,13 @@
public FastCopyHashMap(int initialCapacity, float loadFactor)
{
if (initialCapacity < 0)
- {
throw new IllegalArgumentException("Can not have a negative size
table!");
- }
if (initialCapacity > MAXIMUM_CAPACITY)
- {
initialCapacity = MAXIMUM_CAPACITY;
- }
if (!(loadFactor > 0F && loadFactor <= 1F))
- {
throw new IllegalArgumentException("Load factor must be greater than 0 and
less than or equal to 1");
- }
this.loadFactor = loadFactor;
init(initialCapacity, loadFactor);
@@ -213,14 +207,10 @@
{
Entry<K, V> e = table[index];
if (e == null)
- {
return null;
- }
if (e.hash == hash && eq(key, e.key))
- {
return e.value;
- }
index = nextIndex(index, length);
}
@@ -238,14 +228,10 @@
{
Entry<K, V> e = table[index];
if (e == null)
- {
return false;
- }
if (e.hash == hash && eq(key, e.key))
- {
return true;
- }
index = nextIndex(index, length);
}
@@ -254,12 +240,8 @@
public boolean containsValue(Object value)
{
for (Entry<K, V> e : table)
- {
if (e != null && eq(value, e.value))
- {
return true;
- }
- }
return false;
}
@@ -279,9 +261,7 @@
{
Entry<K, V> e = table[index];
if (e == null)
- {
break;
- }
if (e.hash == hash && eq(key, e.key))
{
@@ -291,17 +271,13 @@
index = nextIndex(index, length);
if (index == start)
- {
throw new IllegalStateException("Table is full!");
- }
}
modCount++;
table[index] = new Entry<K, V>(key, hash, value);
if (++size >= threshold)
- {
resize(length);
- }
return null;
}
@@ -314,9 +290,7 @@
// Can't get any bigger
if (newLength > MAXIMUM_CAPACITY || newLength <= from)
- {
return;
- }
Entry<K, V>[] newTable = new Entry[newLength];
Entry<K, V>[] old = table;
@@ -324,15 +298,11 @@
for (Entry<K, V> e : old)
{
if (e == null)
- {
continue;
- }
int index = index(e.hash, newLength);
while (newTable[index] != null)
- {
index = nextIndex(index, newLength);
- }
newTable[index] = e;
}
@@ -345,16 +315,12 @@
{
int size = map.size();
if (size == 0)
- {
return;
- }
if (size > threshold)
{
if (size > MAXIMUM_CAPACITY)
- {
size = MAXIMUM_CAPACITY;
- }
int length = table.length;
for (; length < size; length <<= 1) ;
@@ -363,9 +329,7 @@
}
for (Map.Entry<? extends K, ? extends V> e : map.entrySet())
- {
put(e.getKey(), e.getValue());
- }
}
public V remove(Object key)
@@ -381,9 +345,7 @@
{
Entry<K, V> e = table[index];
if (e == null)
- {
return null;
- }
if (e.hash == hash && eq(key, e.key))
{
@@ -396,9 +358,7 @@
index = nextIndex(index, length);
if (index == start)
- {
return null;
- }
}
@@ -414,9 +374,7 @@
{
Entry<K, V> e = table[current];
if (e == null)
- {
return;
- }
// A Doug Lea variant of Knuth's Section 6.4 Algorithm R.
// This provides a non-recursive method of relocating
@@ -439,9 +397,7 @@
modCount++;
Entry<K, V>[] table = this.table;
for (int i = 0; i < table.length; i++)
- {
table[i] = null;
- }
size = 0;
}
@@ -480,9 +436,7 @@
total++;
int target = index(e.hash, table.length);
if (i == target)
- {
optimal++;
- }
else
{
int skew = Math.abs(i - target);
@@ -503,9 +457,7 @@
public Set<Map.Entry<K, V>> entrySet()
{
if (entrySet == null)
- {
entrySet = new EntrySet();
- }
return entrySet;
}
@@ -513,9 +465,7 @@
public Set<K> keySet()
{
if (keySet == null)
- {
keySet = new KeySet();
- }
return keySet;
}
@@ -523,9 +473,7 @@
public Collection<V> values()
{
if (values == null)
- {
values = new Values();
- }
return values;
}
@@ -545,6 +493,8 @@
V value = (V) s.readObject();
putForCreate(key, value);
}
+
+ this.size = size;
}
@SuppressWarnings("unchecked")
@@ -607,9 +557,7 @@
public boolean hasNext()
{
if (hasNext == true)
- {
return true;
- }
Entry<K, V> table[] = this.table;
for (int i = next; i < table.length; i++)
@@ -628,14 +576,10 @@
protected Entry<K, V> nextEntry()
{
if (modCount != expectedCount)
- {
throw new ConcurrentModificationException();
- }
if (!hasNext && !hasNext())
- {
throw new NoSuchElementException();
- }
current = next++;
hasNext = false;
@@ -647,17 +591,13 @@
public void remove()
{
if (modCount != expectedCount)
- {
throw new ConcurrentModificationException();
- }
int current = this.current;
int delete = current;
if (current == -1)
- {
throw new IllegalStateException();
- }
// Invalidate current (prevents multiple remove)
this.current = -1;
@@ -686,9 +626,7 @@
i = nextIndex(i, length);
Entry<K, V> e = table[i];
if (e == null)
- {
break;
- }
int prefer = index(e.hash, length);
if ((i < prefer && (prefer <= delete || delete <= i))
@@ -746,9 +684,7 @@
public V setValue(V value)
{
if (table != FastCopyHashMap.this.table)
- {
FastCopyHashMap.this.put(getKey(), value);
- }
return super.setValue(value);
}
@@ -820,9 +756,7 @@
public boolean contains(Object o)
{
if (!(o instanceof Map.Entry))
- {
return false;
- }
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
Object value = get(entry.getKey());
@@ -882,22 +816,18 @@
public boolean equals(Object o)
{
if (this == o)
- {
return true;
- }
if (!(o instanceof Map.Entry))
- {
return false;
- }
Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
return eq(key, e.getKey()) && eq(value, e.getValue());
}
public int hashCode()
{
- return hash(maskNull(key)) ^
- (value == null ? 0 : hash(value));
+ return (key == null ? 0 : hash(key)) ^
+ (value == null ? 0 : hash(value));
}
public String toString()