[jboss-cvs] JBossCache/src/org/jboss/cache/util ...
Elias Ross
genman at noderunner.net
Mon Nov 20 02:33:14 EST 2006
User: genman
Date: 06/11/20 02:33:14
Added: src/org/jboss/cache/util MapCopy.java
Log:
JBCACHE-867 -- Fix some data copying issues of Node.getData()
Revision Changes Path
1.1 date: 2006/11/20 07:33:14; author: genman; state: Exp;JBossCache/src/org/jboss/cache/util/MapCopy.java
Index: MapCopy.java
===================================================================
package org.jboss.cache.util;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/**
* Contains a fixed array of read-only map entries, from a copy of an existing map.
* This class is more lightweight for places where the copied map will just be iterated over.
*/
public class MapCopy<K, V> extends AbstractMap implements Serializable
{
private Map.Entry<K, V> data[];
private transient Set<Map.Entry<K, V>> entrySet;
/**
* Copies the supplied map to an internal array.
*/
public MapCopy(Map<K, V> m)
{
data = new Map.Entry[m.size()];
int i = 0;
for (Map.Entry<K, V> me : (Set<Map.Entry>) m.entrySet())
{
data[i++] = new SimpleEntry<K, V>(me);
}
this.entrySet = new AbstractSet<Map.Entry<K, V>>()
{
public int size()
{
return data.length;
}
public Iterator<Map.Entry<K, V>> iterator()
{
return new EntryIterator();
}
};
}
private class EntryIterator implements Iterator<Entry<K, V>>
{
private int index;
public boolean hasNext()
{
return index < data.length;
}
public Entry<K, V> next()
{
return data[index++];
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
/**
* Where is Java 1.6?
*/
private static class SimpleEntry<K, V> implements Map.Entry<K, V>, Serializable
{
private K key;
private V value;
public SimpleEntry(Entry<K, V> me)
{
key = me.getKey();
value = me.getValue();
}
public K getKey()
{
return key;
}
public V getValue()
{
return value;
}
public V setValue(V arg0)
{
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o)
{
Map.Entry e2 = (Map.Entry) o;
return (getKey() == null ? e2.getKey() == null : getKey().equals(e2.getKey()))
&& (getValue() == null ? e2.getValue() == null : getValue().equals(e2.getValue()));
}
@Override
public int hashCode()
{
return (getKey() == null ? 0 : getKey().hashCode()) ^
(getValue() == null ? 0 : getValue().hashCode());
}
@Override
public String toString()
{
return key + "=" + value;
}
}
@Override
public Set entrySet()
{
return entrySet;
}
@Override
public int size()
{
return data.length;
}
}
More information about the jboss-cvs-commits
mailing list