1 package org.jboss.cache.util; 2 3 import java.io.IOException ; 4 import java.io.Serializable ; 5 import java.util.AbstractMap ; 6 import java.util.AbstractSet ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 18 public class MapCopy<K, V> extends AbstractMap implements Serializable 19 { 20 21 private static final long serialVersionUID = -958813082188242956L; 22 23 private Map.Entry <K, V> data[]; 24 25 private transient Set <Map.Entry <K, V>> entrySet; 26 27 30 public MapCopy(Map <K, V> m) 31 { 32 data = new Map.Entry [m.size()]; 33 int i = 0; 34 for (Map.Entry <K, V> me : m.entrySet()) 35 { 36 if (me == null) 37 throw new NullPointerException (); 38 data[i++] = new SimpleEntry<K, V>(me); 39 } 40 init(); 41 } 42 43 private void init() 44 { 45 this.entrySet = new AbstractSet <Map.Entry <K, V>>() 46 { 47 public int size() 48 { 49 return data.length; 50 } 51 52 public Iterator <Map.Entry <K, V>> iterator() 53 { 54 return new EntryIterator(); 55 } 56 }; 57 } 58 59 private class EntryIterator implements Iterator <Entry<K, V>> 60 { 61 private int index; 62 63 public boolean hasNext() 64 { 65 return index < data.length; 66 } 67 68 public Entry<K, V> next() 69 { 70 return data[index++]; 71 } 72 73 public void remove() 74 { 75 throw new UnsupportedOperationException (); 76 } 77 } 78 79 82 private static class SimpleEntry<K, V> implements Map.Entry <K, V>, Serializable 83 { 84 private static final long serialVersionUID = -6092752114794052323L; 85 86 private K key; 87 88 private V value; 89 90 public SimpleEntry(Entry<K, V> me) 91 { 92 key = me.getKey(); 93 value = me.getValue(); 94 } 95 96 public K getKey() 97 { 98 return key; 99 } 100 101 public V getValue() 102 { 103 return value; 104 } 105 106 public V setValue(V arg0) 107 { 108 throw new UnsupportedOperationException (); 109 } 110 111 @Override 112 public boolean equals(Object o) 113 { 114 Map.Entry e2 = (Map.Entry ) o; 115 return (getKey() == null ? e2.getKey() == null : getKey().equals(e2.getKey())) 116 && (getValue() == null ? e2.getValue() == null : getValue().equals(e2.getValue())); 117 } 118 119 @Override 120 public int hashCode() 121 { 122 return (getKey() == null ? 0 : getKey().hashCode()) ^ 123 (getValue() == null ? 0 : getValue().hashCode()); 124 } 125 126 @Override 127 public String toString() 128 { 129 return key + "=" + value; 130 } 131 } 132 133 @Override 134 public Set entrySet() 135 { 136 return entrySet; 137 } 138 139 @Override 140 public int size() 141 { 142 return data.length; 143 } 144 145 private void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException 146 { 147 in.defaultReadObject(); 148 init(); 149 } 150 151 } 152 | Popular Tags |