KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > collection > PersistentMap


1 //$Id: PersistentMap.java,v 1.19 2005/07/21 16:40:23 oneovthafew Exp $
2
package org.hibernate.collection;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.hibernate.EntityMode;
14 import org.hibernate.HibernateException;
15 import org.hibernate.loader.CollectionAliases;
16 import org.hibernate.engine.SessionImplementor;
17 import org.hibernate.persister.collection.CollectionPersister;
18 import org.hibernate.type.Type;
19 import org.hibernate.util.LinkedHashCollectionHelper;
20
21
22 /**
23  * A persistent wrapper for a <tt>java.util.Map</tt>. Underlying collection
24  * is a <tt>HashMap</tt>.
25  *
26  * @see java.util.HashMap
27  * @author Gavin King
28  */

29 public class PersistentMap extends AbstractPersistentCollection implements java.util.Map JavaDoc {
30
31     protected java.util.Map JavaDoc map;
32
33     public Serializable JavaDoc getSnapshot(CollectionPersister persister)
34     throws HibernateException {
35         EntityMode entityMode = getSession().getEntityMode();
36         
37         HashMap JavaDoc clonedMap = new HashMap JavaDoc( map.size() );
38         Iterator JavaDoc iter = map.entrySet().iterator();
39         while ( iter.hasNext() ) {
40             java.util.Map.Entry e = (java.util.Map.Entry) iter.next();
41             final Object JavaDoc copy = persister.getElementType()
42                 .deepCopy( e.getValue(), entityMode, persister.getFactory() );
43             clonedMap.put( e.getKey(), copy );
44         }
45         return clonedMap;
46     }
47
48     public Collection JavaDoc getOrphans(Serializable JavaDoc snapshot, String JavaDoc entityName) throws HibernateException {
49         java.util.Map JavaDoc sn = (java.util.Map JavaDoc) snapshot;
50         return getOrphans( sn.values(), map.values(), entityName, getSession() );
51     }
52
53     public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
54         Type elementType = persister.getElementType();
55         java.util.Map JavaDoc xmap = (java.util.Map JavaDoc) getSnapshot();
56         if ( xmap.size()!=this.map.size() ) return false;
57         Iterator JavaDoc iter = map.entrySet().iterator();
58         while ( iter.hasNext() ) {
59             java.util.Map.Entry entry = (java.util.Map.Entry) iter.next();
60             if ( elementType.isDirty( entry.getValue(), xmap.get( entry.getKey() ), getSession() ) ) return false;
61         }
62         return true;
63     }
64
65     public boolean isSnapshotEmpty(Serializable JavaDoc snapshot) {
66         return ( (java.util.Map JavaDoc) snapshot ).isEmpty();
67     }
68
69     public boolean isWrapper(Object JavaDoc collection) {
70         return map==collection;
71     }
72     public PersistentMap(SessionImplementor session) {
73         super(session);
74     }
75
76     public PersistentMap() {} //needed for SOAP libraries, etc
77

78     public void beforeInitialize(CollectionPersister persister) {
79         this.map = persister.hasOrdering() ?
80             LinkedHashCollectionHelper.createLinkedHashMap() :
81             new HashMap JavaDoc();
82     }
83
84     public PersistentMap(SessionImplementor session, java.util.Map JavaDoc map) {
85         super(session);
86         this.map = map;
87         setInitialized();
88         setDirectlyAccessible(true);
89     }
90
91     /**
92      * @see java.util.Map#size()
93      */

94     public int size() {
95         read();
96         return map.size();
97     }
98
99     /**
100      * @see java.util.Map#isEmpty()
101      */

102     public boolean isEmpty() {
103         read();
104         return map.isEmpty();
105     }
106
107     /**
108      * @see java.util.Map#containsKey(Object)
109      */

110     public boolean containsKey(Object JavaDoc key) {
111         read();
112         return map.containsKey(key);
113     }
114
115     /**
116      * @see java.util.Map#containsValue(Object)
117      */

118     public boolean containsValue(Object JavaDoc value) {
119         read();
120         return map.containsValue(value) ;
121     }
122
123     /**
124      * @see java.util.Map#get(Object)
125      */

126     public Object JavaDoc get(Object JavaDoc key) {
127         read();
128         return map.get(key);
129     }
130
131     /**
132      * @see java.util.Map#put(Object, Object)
133      */

134     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
135         write();
136         return map.put(key, value);
137     }
138
139     /**
140      * @see java.util.Map#remove(Object)
141      */

142     public Object JavaDoc remove(Object JavaDoc key) {
143         write();
144         return map.remove(key);
145     }
146
147     /**
148      * @see java.util.Map#putAll(java.util.Map puts)
149      */

150     public void putAll(java.util.Map JavaDoc puts) {
151         if ( puts.size()>0 ) {
152             write();
153             map.putAll(puts);
154         }
155     }
156
157     /**
158      * @see java.util.Map#clear()
159      */

160     public void clear() {
161         write();
162         map.clear();
163     }
164
165     /**
166      * @see java.util.Map#keySet()
167      */

168     public java.util.Set JavaDoc keySet() {
169         read();
170         return new SetProxy( map.keySet() );
171     }
172
173     /**
174      * @see java.util.Map#values()
175      */

176     public Collection JavaDoc values() {
177         read();
178         return new SetProxy( map.values() );
179     }
180
181     /**
182      * @see java.util.Map#entrySet()
183      */

184     public java.util.Set JavaDoc entrySet() {
185         read();
186         return new EntrySetProxy( map.entrySet() );
187     }
188
189     public boolean empty() {
190         return map.isEmpty();
191     }
192
193     public String JavaDoc toString() {
194         read();
195         return map.toString();
196     }
197
198     public Object JavaDoc readFrom(ResultSet JavaDoc rs, CollectionPersister persister, CollectionAliases descriptor, Object JavaDoc owner)
199     throws HibernateException, SQLException JavaDoc {
200         Object JavaDoc element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
201         Object JavaDoc index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() );
202         if ( element!=null ) map.put(index, element);
203         return element;
204     }
205
206     public Iterator JavaDoc entries(CollectionPersister persister) {
207         return map.entrySet().iterator();
208     }
209
210     /** a wrapper for Map.Entry sets */
211     class EntrySetProxy implements java.util.Set JavaDoc {
212         private final java.util.Set JavaDoc set;
213         EntrySetProxy(java.util.Set JavaDoc set) {
214             this.set=set;
215         }
216         public boolean add(Object JavaDoc entry) {
217             //write(); -- doesn't
218
return set.add(entry);
219         }
220         public boolean addAll(Collection JavaDoc entries) {
221             //write(); -- doesn't
222
return set.addAll(entries);
223         }
224         public void clear() {
225             write();
226             set.clear();
227         }
228         public boolean contains(Object JavaDoc entry) {
229             return set.contains(entry);
230         }
231         public boolean containsAll(Collection JavaDoc entries) {
232             return set.containsAll(entries);
233         }
234         public boolean isEmpty() {
235             return set.isEmpty();
236         }
237         public Iterator JavaDoc iterator() {
238             return new EntryIteratorProxy( set.iterator() );
239         }
240         public boolean remove(Object JavaDoc entry) {
241             write();
242             return set.remove(entry);
243         }
244         public boolean removeAll(Collection JavaDoc entries) {
245             write();
246             return set.removeAll(entries);
247         }
248         public boolean retainAll(Collection JavaDoc entries) {
249             write();
250             return set.retainAll(entries);
251         }
252         public int size() {
253             return set.size();
254         }
255         // amazingly, these two will work because AbstractCollection
256
// uses iterator() to fill the array
257
public Object JavaDoc[] toArray() {
258             return set.toArray();
259         }
260         public Object JavaDoc[] toArray(Object JavaDoc[] array) {
261             return set.toArray(array);
262         }
263     }
264     final class EntryIteratorProxy implements Iterator JavaDoc {
265         private final Iterator JavaDoc iter;
266         EntryIteratorProxy(Iterator JavaDoc iter) {
267             this.iter=iter;
268         }
269         public boolean hasNext() {
270             return iter.hasNext();
271         }
272         public Object JavaDoc next() {
273             return new MapEntryProxy( (java.util.Map.Entry) iter.next() );
274         }
275         public void remove() {
276             write();
277             iter.remove();
278         }
279     }
280
281     final class MapEntryProxy implements java.util.Map.Entry {
282         private final java.util.Map.Entry me;
283         MapEntryProxy( java.util.Map.Entry me ) {
284             this.me = me;
285         }
286         public Object JavaDoc getKey() { return me.getKey(); }
287         public Object JavaDoc getValue() { return me.getValue(); }
288         public boolean equals(Object JavaDoc o) { return me.equals(o); }
289         public int hashCode() { return me.hashCode(); }
290         // finally, what it's all about...
291
public Object JavaDoc setValue(Object JavaDoc value) {
292             write();
293             return me.setValue(value);
294         }
295     }
296
297     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
298     throws HibernateException {
299         beforeInitialize(persister);
300         Serializable JavaDoc[] array = (Serializable JavaDoc[]) disassembled;
301         for (int i=0; i<array.length; i+=2 ) map.put(
302             persister.getIndexType().assemble( array[i], getSession(), owner ),
303             persister.getElementType().assemble( array[i+1], getSession(), owner )
304         );
305     }
306
307     public Serializable JavaDoc disassemble(CollectionPersister persister) throws HibernateException {
308
309         Serializable JavaDoc[] result = new Serializable JavaDoc[ map.size() * 2 ];
310         Iterator JavaDoc iter = map.entrySet().iterator();
311         int i=0;
312         while ( iter.hasNext() ) {
313             java.util.Map.Entry e = (java.util.Map.Entry) iter.next();
314             result[i++] = persister.getIndexType().disassemble( e.getKey(), getSession(), null );
315             result[i++] = persister.getElementType().disassemble( e.getValue(), getSession(), null );
316         }
317         return result;
318
319     }
320
321     public Iterator JavaDoc getDeletes(CollectionPersister persister, boolean indexIsFormula)
322     throws HibernateException {
323         java.util.List JavaDoc deletes = new ArrayList JavaDoc();
324         Iterator JavaDoc iter = ( (java.util.Map JavaDoc) getSnapshot() ).entrySet().iterator();
325         while ( iter.hasNext() ) {
326             java.util.Map.Entry e = (java.util.Map.Entry) iter.next();
327             Object JavaDoc key = e.getKey();
328             if ( e.getValue()!=null && map.get(key)==null ) {
329                 deletes.add( indexIsFormula ? e.getValue() : key );
330             }
331         }
332         return deletes.iterator();
333     }
334
335     public boolean needsInserting(Object JavaDoc entry, int i, Type elemType)
336     throws HibernateException {
337         final java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
338         java.util.Map.Entry e = (java.util.Map.Entry) entry;
339         return e.getValue()!=null && sn.get( e.getKey() )==null;
340     }
341
342     public boolean needsUpdating(Object JavaDoc entry, int i, Type elemType)
343     throws HibernateException {
344         final java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
345         java.util.Map.Entry e = (java.util.Map.Entry) entry;
346         Object JavaDoc snValue = sn.get( e.getKey() );
347         return e.getValue()!=null &&
348             snValue!=null &&
349             elemType.isDirty( snValue, e.getValue(), getSession() );
350     }
351
352
353     public Object JavaDoc getIndex(Object JavaDoc entry, int i, CollectionPersister persister) {
354         return ( (java.util.Map.Entry) entry ).getKey();
355     }
356
357     public Object JavaDoc getElement(Object JavaDoc entry) {
358         return ( (java.util.Map.Entry) entry ).getValue();
359     }
360
361     public Object JavaDoc getSnapshotElement(Object JavaDoc entry, int i) {
362         final java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
363         return sn.get( ( (java.util.Map.Entry) entry ).getKey() );
364     }
365
366     public boolean equals(Object JavaDoc other) {
367         read();
368         return map.equals(other);
369     }
370
371     public int hashCode() {
372         read();
373         return map.hashCode();
374     }
375
376     public boolean entryExists(Object JavaDoc entry, int i) {
377         return ( (Map.Entry JavaDoc) entry ).getValue()!=null;
378     }
379
380 }
381
Popular Tags