KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > SoftMap


1 package com.tirsen.nanning.samples.prevayler;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7 import java.util.*;
8
9 import org.apache.commons.collections.ReferenceMap;
10
11 public class SoftMap extends ReferenceMap implements Externalizable JavaDoc {
12     static final long serialVersionUID = -7396446944555084132L;
13
14     /* Only for externalization */
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     /**
28      * Create a map with soft values
29      */

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 JavaDoc 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 JavaDoc get(Object JavaDoc 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 JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
78         buildMap();
79         return super.put(key, value);
80     }
81
82     public Object JavaDoc remove(Object JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
98         buildMap();
99         return super.clone();
100     }
101
102     public boolean containsValue(Object JavaDoc value) {
103         buildMap();
104         return super.containsValue(value);
105     }
106
107     public boolean equals(Object JavaDoc 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 JavaDoc toString() {
123         buildMap();
124         return super.toString();
125     }
126
127     /**
128      * This provides lazy deserialization which is needed if you have dynamic proxies as keys
129      */

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 JavaDoc key = i.next();
136                 Object JavaDoc value = i.next();
137                 put(key, value);
138             }
139         }
140     }
141
142     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
143         Set keys = keySet();
144         out.writeInt(keys.size());
145
146         for (Iterator i = keys.iterator(); i.hasNext();) {
147             Object JavaDoc key = i.next();
148             Object JavaDoc value = get(key);
149             out.writeObject(key);
150             out.writeObject(value);
151         }
152     }
153
154     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
155         int num = in.readInt();
156         readBackValues = new ArrayList(num * 2);
157
158         for (int i = 0; i < num; i++) {
159             Object JavaDoc key = in.readObject();
160             assert key != null;
161             Object JavaDoc value = in.readObject();
162             assert value != null;
163
164             readBackValues.add(key);
165             readBackValues.add(value);
166         }
167     }
168 }
169
170
Popular Tags