Author: epbernard
Date: 2007-01-08 08:55:36 -0500 (Mon, 08 Jan 2007)
New Revision: 11023
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/search/util/WeakIdentityHashMap.java
Log:
generic weakidentityhashmap
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/search/util/WeakIdentityHashMap.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/search/util/WeakIdentityHashMap.java 2007-01-08
12:23:31 UTC (rev 11022)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/search/util/WeakIdentityHashMap.java 2007-01-08
13:55:36 UTC (rev 11023)
@@ -58,7 +58,7 @@
* @see java.util.IdentityHashMap
* @see java.util.WeakHashMap
*/
-public class WeakIdentityHashMap /*extends AbstractMap*/ implements Map {
+public class WeakIdentityHashMap<K,V> /*extends AbstractMap*/ implements
Map<K,V> {
/**
* The default initial capacity -- MUST be a power of two.
@@ -80,7 +80,7 @@
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
- private Entry[] table;
+ private Entry<K,V>[] table;
/**
* The number of key-value mappings contained in this weak hash map.
@@ -195,16 +195,16 @@
/**
* Use NULL_KEY for key if it is null.
*/
- private static Object maskNull(Object key) {
+ private static <T> T maskNull(T key) {
return ( key == null ?
- NULL_KEY :
+ (T) NULL_KEY : //i don't think there is a better way
key );
}
/**
* Return internal representation of null key back to caller as null
*/
- private static Object unmaskNull(Object key) {
+ private static <T> T unmaskNull(T key) {
return ( key == NULL_KEY ?
null :
key );
@@ -258,7 +258,7 @@
/**
* Return the table after first expunging stale entries
*/
- private Entry[] getTable() {
+ private Entry<K,V>[] getTable() {
expungeStaleEntries();
return table;
}
@@ -300,12 +300,12 @@
* <tt>null</tt> if the map contains no mapping for this key.
* @see #put(Object,Object)
*/
- public Object get(Object key) {
+ public V get(Object key) {
Object k = maskNull( key );
int h = hash( k );
- Entry[] tab = getTable();
+ Entry<K,V>[] tab = getTable();
int index = indexFor( h, tab.length );
- Entry e = tab[index];
+ Entry<K,V> e = tab[index];
while ( e != null ) {
if ( e.hash == h && k == e.get() )
return e.value;
@@ -330,12 +330,12 @@
* Returns the entry associated with the specified key in the HashMap.
* Returns null if the HashMap contains no mapping for this key.
*/
- Entry getEntry(Object key) {
+ Entry<K,V> getEntry(Object key) {
Object k = maskNull( key );
int h = hash( k );
- Entry[] tab = getTable();
+ Entry<K,V>[] tab = getTable();
int index = indexFor( h, tab.length );
- Entry e = tab[index];
+ Entry<K,V> e = tab[index];
while ( e != null && !( e.hash == h && k == e.get() ) )
e = e.next;
return e;
@@ -353,15 +353,15 @@
* also indicate that the HashMap previously associated
* <tt>null</tt> with the specified key.
*/
- public Object put(Object key, Object value) {
- Object k = maskNull( key );
+ public V put(K key, V value) {
+ K k = maskNull( key );
int h = hash( k );
- Entry[] tab = getTable();
+ Entry<K,V>[] tab = getTable();
int i = indexFor( h, tab.length );
- for ( Entry e = tab[i]; e != null; e = e.next ) {
+ for ( Entry<K,V> e = tab[i]; e != null; e = e.next ) {
if ( h == e.hash && k == e.get() ) {
- Object oldValue = e.value;
+ V oldValue = e.value;
if ( value != oldValue )
e.value = value;
return oldValue;
@@ -369,7 +369,7 @@
}
modCount++;
- tab[i] = new Entry( k, value, queue, h, tab[i] );
+ tab[i] = new Entry<K,V>( k, value, queue, h, tab[i] );
if ( ++size >= threshold )
resize( tab.length * 2 );
return null;
@@ -388,14 +388,14 @@
void resize(int newCapacity) {
// assert (newCapacity & -newCapacity) == newCapacity; // power of 2
- Entry[] oldTable = getTable();
+ Entry<K,V>[] oldTable = getTable();
int oldCapacity = oldTable.length;
// check if needed
if ( size < threshold || oldCapacity > newCapacity )
return;
- Entry[] newTable = new Entry[newCapacity];
+ Entry<K,V>[] newTable = new Entry[newCapacity];
transfer( oldTable, newTable );
table = newTable;
@@ -418,13 +418,13 @@
/**
* Transfer all entries from src to dest tables
*/
- private void transfer(Entry[] src, Entry[] dest) {
+ private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
for ( int j = 0; j < src.length; ++j ) {
- Entry e = src[j];
+ Entry<K,V> e = src[j];
src[j] = null;
while ( e != null ) {
- Entry next = e.next;
- Object key = e.get();
+ Entry<K,V> next = e.next;
+ K key = e.get();
if ( key == null ) {
e.next = null; // Help GC
e.value = null; // " "
@@ -448,7 +448,7 @@
* @param t mappings to be stored in this map.
* @throws NullPointerException if the specified map is null.
*/
- public void putAll(Map t) {
+ public void putAll(Map<? extends K, ? extends V> t) {
// Expand enough to hold t's elements without resizing.
int n = t.size();
if ( n == 0 )
@@ -464,7 +464,7 @@
}
for ( Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
- Map.Entry e = (Map.Entry) i.next();
+ Map.Entry<K,V> e = (Map.Entry<K,V>) i.next(); //FIXME should not have to
cast
put( e.getKey(), e.getValue() );
}
}
@@ -478,16 +478,16 @@
* also indicate that the map previously associated <tt>null</tt>
* with the specified key.
*/
- public Object remove(Object key) {
+ public V remove(Object key) {
Object k = maskNull( key );
int h = hash( k );
- Entry[] tab = getTable();
+ Entry<K,V>[] tab = getTable();
int i = indexFor( h, tab.length );
- Entry prev = tab[i];
- Entry e = prev;
+ Entry<K,V> prev = tab[i];
+ Entry<K,V> e = prev;
while ( e != null ) {
- Entry next = e.next;
+ Entry<K,V> next = e.next;
if ( h == e.hash && k == e.get() ) {
modCount++;
size--;
@@ -632,32 +632,32 @@
* The entries in this hash table extend WeakReference, using its main ref
* field as the key.
*/
- private static class Entry extends WeakReference implements Map.Entry {
- private Object value;
+ private static class Entry<K,V> extends WeakReference<K> implements
Map.Entry<K,V> {
+ private V value;
private final int hash;
- private Entry next;
+ private Entry<K,V> next;
/**
* Create new entry.
*/
- Entry(Object key, Object value, ReferenceQueue queue,
- int hash, Entry next) {
+ Entry(K key, V value, ReferenceQueue queue,
+ int hash, Entry<K,V> next) {
super( key, queue );
this.value = value;
this.hash = hash;
this.next = next;
}
- public Object getKey() {
- return unmaskNull( this.get() );
+ public K getKey() {
+ return WeakIdentityHashMap.unmaskNull( this.get() );
}
- public Object getValue() {
+ public V getValue() {
return value;
}
- public Object setValue(Object newValue) {
- Object oldValue = value;
+ public V setValue(V newValue) {
+ V oldValue = value;
value = newValue;
return oldValue;
}
@@ -693,10 +693,10 @@
}
}
- private abstract class HashIterator implements Iterator {
+ private abstract class HashIterator<E> implements Iterator<E> {
int index;
- Entry entry = null;
- Entry lastReturned = null;
+ Entry<K,V> entry = null;
+ Entry<K,V> lastReturned = null;
int expectedModCount = modCount;
/**
@@ -741,7 +741,7 @@
/**
* The common parts of next() across different types of iterators
*/
- protected Entry nextEntry() {
+ protected Entry<K,V> nextEntry() {
if ( modCount != expectedModCount )
throw new ConcurrentModificationException();
if ( nextKey == null && !hasNext() )
@@ -780,15 +780,15 @@
}
}
- private class EntryIterator extends HashIterator {
- public Object next() {
+ private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
+ public Map.Entry<K,V> next() {
return nextEntry();
}
}
// Views
- private transient Set entrySet = null;
+ private transient Set<Map.Entry<K,V>> entrySet = null;
/**
* Returns a set view of the keys contained in this map. The set is
@@ -912,15 +912,15 @@
* @return a collection view of the mappings contained in this map.
* @see java.util.Map.Entry
*/
- public Set entrySet() {
- Set es = entrySet;
+ public Set<Map.Entry<K,V>> entrySet() {
+ Set<Map.Entry<K,V>> es = entrySet;
return ( es != null ?
es :
( entrySet = new EntrySet() ) );
}
- private class EntrySet extends AbstractSet {
- public Iterator iterator() {
+ private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
+ public Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}