1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.io.Externalizable ; 4 import java.io.IOException ; 5 import java.io.ObjectInput ; 6 import java.io.ObjectOutput ; 7 import java.util.*; 8 9 import org.apache.commons.collections.ReferenceMap; 10 11 public class SoftMap extends ReferenceMap implements Externalizable { 12 static final long serialVersionUID = -7396446944555084132L; 13 14 15 private transient List readBackValues; 16 private boolean valuesAreSoft; 17 18 19 public static SoftMap createSoftValuesMap() { 20 return new SoftMap(); 21 } 22 23 public static SoftMap createSoftKeysMap() { 24 return new SoftMap(false); 25 } 26 27 30 public SoftMap() { 31 this(true); 32 } 33 34 public SoftMap(SoftMap map) { 35 this.valuesAreSoft = map.valuesAreSoft; 36 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 37 Map.Entry entry = (Map.Entry) i.next(); 38 put(entry.getKey(), entry.getValue()); 39 } 40 } 41 42 private SoftMap(boolean valuesAreSoft) { 43 super(valuesAreSoft ? HARD : SOFT, valuesAreSoft ? SOFT : HARD); 44 this.valuesAreSoft = valuesAreSoft; 45 } 46 47 public void clear() { 48 buildMap(); 49 super.clear(); 50 } 51 52 public boolean containsKey(Object key) { 53 buildMap(); 54 return super.containsKey(key); 55 } 56 57 public Set entrySet() { 58 buildMap(); 59 return super.entrySet(); 60 } 61 62 public Object get(Object key) { 63 buildMap(); 64 return super.get(key); 65 } 66 67 public boolean isEmpty() { 68 buildMap(); 69 return super.isEmpty(); 70 } 71 72 public Set keySet() { 73 buildMap(); 74 return super.keySet(); 75 } 76 77 public Object put(Object key, Object value) { 78 buildMap(); 79 return super.put(key, value); 80 } 81 82 public Object remove(Object key) { 83 buildMap(); 84 return super.remove(key); 85 } 86 87 public int size() { 88 buildMap(); 89 return super.size(); 90 } 91 92 public Collection values() { 93 buildMap(); 94 return super.values(); 95 } 96 97 protected Object clone() throws CloneNotSupportedException { 98 buildMap(); 99 return super.clone(); 100 } 101 102 public boolean containsValue(Object value) { 103 buildMap(); 104 return super.containsValue(value); 105 } 106 107 public boolean equals(Object o) { 108 buildMap(); 109 return super.equals(o); 110 } 111 112 public int hashCode() { 113 buildMap(); 114 return super.hashCode(); 115 } 116 117 public void putAll(Map t) { 118 buildMap(); 119 super.putAll(t); 120 } 121 122 public String toString() { 123 buildMap(); 124 return super.toString(); 125 } 126 127 130 private synchronized void buildMap() { 131 if (readBackValues != null) { 132 List values = readBackValues; 133 readBackValues = null; 134 for (Iterator i = values.iterator(); i.hasNext();) { 135 Object key = i.next(); 136 Object value = i.next(); 137 put(key, value); 138 } 139 } 140 } 141 142 public void writeExternal(ObjectOutput out) throws IOException { 143 Set keys = keySet(); 144 out.writeInt(keys.size()); 145 146 for (Iterator i = keys.iterator(); i.hasNext();) { 147 Object key = i.next(); 148 Object value = get(key); 149 out.writeObject(key); 150 out.writeObject(value); 151 } 152 } 153 154 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 155 int num = in.readInt(); 156 readBackValues = new ArrayList(num * 2); 157 158 for (int i = 0; i < num; i++) { 159 Object key = in.readObject(); 160 assert key != null; 161 Object value = in.readObject(); 162 assert value != null; 163 164 readBackValues.add(key); 165 readBackValues.add(value); 166 } 167 } 168 } 169 170 | Popular Tags |