KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PersistentSet.java,v 1.20 2005/06/28 08:59:06 maxcsaucdk 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.HashSet JavaDoc;
11 import java.util.Iterator 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.Set</tt>. The underlying
24  * collection is a <tt>HashSet</tt>.
25  *
26  * @see java.util.HashSet
27  * @author Gavin King
28  */

29 public class PersistentSet extends AbstractPersistentCollection implements java.util.Set JavaDoc {
30
31     protected java.util.Set JavaDoc set;
32     protected transient java.util.List JavaDoc tempList;
33
34     public Serializable JavaDoc getSnapshot(CollectionPersister persister)
35     throws HibernateException {
36         EntityMode entityMode = getSession().getEntityMode();
37         
38         //if (set==null) return new Set(session);
39
HashMap JavaDoc clonedSet = new HashMap JavaDoc( set.size() );
40         Iterator JavaDoc iter = set.iterator();
41         while ( iter.hasNext() ) {
42             Object JavaDoc copied = persister.getElementType()
43                     .deepCopy( iter.next(), entityMode, persister.getFactory() );
44             clonedSet.put(copied, copied);
45         }
46         return clonedSet;
47     }
48
49     public Collection JavaDoc getOrphans(Serializable JavaDoc snapshot, String JavaDoc entityName) throws HibernateException {
50         java.util.Map JavaDoc sn = (java.util.Map JavaDoc) snapshot;
51         return getOrphans( sn.keySet(), set, entityName, getSession() );
52     }
53
54     public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
55         Type elementType = persister.getElementType();
56         java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
57         if ( sn.size()!=set.size() ) {
58             return false;
59         }
60         else {
61             Iterator JavaDoc iter = set.iterator();
62             while ( iter.hasNext() ) {
63                 Object JavaDoc test = iter.next();
64                 Object JavaDoc oldValue = sn.get(test);
65                 if ( oldValue==null || elementType.isDirty( oldValue, test, getSession() ) ) return false;
66             }
67             return true;
68         }
69     }
70
71     public boolean isSnapshotEmpty(Serializable JavaDoc snapshot) {
72         return ( (java.util.Map JavaDoc) snapshot ).isEmpty();
73     }
74     
75     public PersistentSet(SessionImplementor session) {
76         super(session);
77     }
78
79     public PersistentSet() {} //needed for SOAP libraries, etc
80

81     public void beforeInitialize(CollectionPersister persister) {
82         this.set = persister.hasOrdering() ?
83             LinkedHashCollectionHelper.createLinkedHashSet() :
84             new HashSet JavaDoc();
85     }
86     
87     public PersistentSet(SessionImplementor session, java.util.Set JavaDoc set) {
88         super(session);
89         // Sets can be just a view of a part of another collection.
90
// do we need to copy it to be sure it won't be changing
91
// underneath us?
92
// ie. this.set.addAll(set);
93
this.set = set;
94         setInitialized();
95         setDirectlyAccessible(true);
96     }
97
98     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
99     throws HibernateException {
100         beforeInitialize(persister);
101         Serializable JavaDoc[] array = (Serializable JavaDoc[]) disassembled;
102         for (int i=0; i<array.length; i++ ) {
103             Object JavaDoc element = persister.getElementType().assemble( array[i], getSession(), owner );
104             if (element!=null) set.add(element);
105         }
106     }
107
108     public boolean empty() {
109         return set.isEmpty();
110     }
111
112     /**
113      * @see java.util.Set#size()
114      */

115     public int size() {
116         read();
117         return set.size();
118     }
119
120     /**
121      * @see java.util.Set#isEmpty()
122      */

123     public boolean isEmpty() {
124         read();
125         return set.isEmpty();
126     }
127
128     /**
129      * @see java.util.Set#contains(Object)
130      */

131     public boolean contains(Object JavaDoc o) {
132         read();
133         return set.contains(o);
134     }
135
136     /**
137      * @see java.util.Set#iterator()
138      */

139     public Iterator JavaDoc iterator() {
140         read();
141         return new IteratorProxy( set.iterator() );
142     }
143
144     /**
145      * @see java.util.Set#toArray()
146      */

147     public Object JavaDoc[] toArray() {
148         read();
149         return set.toArray();
150     }
151
152     /**
153      * @see java.util.Set#toArray(Object[])
154      */

155     public Object JavaDoc[] toArray(Object JavaDoc[] array) {
156         read();
157         return set.toArray(array);
158     }
159
160     /**
161      * @see java.util.Set#add(Object)
162      */

163     public boolean add(Object JavaDoc value) {
164         write();
165         return set.add(value);
166     }
167
168     /**
169      * @see java.util.Set#remove(Object)
170      */

171     public boolean remove(Object JavaDoc value) {
172         write();
173         return set.remove(value);
174     }
175
176     /**
177      * @see java.util.Set#containsAll(Collection)
178      */

179     public boolean containsAll(Collection JavaDoc coll) {
180         read();
181         return set.containsAll(coll);
182     }
183
184     /**
185      * @see java.util.Set#addAll(Collection)
186      */

187     public boolean addAll(Collection JavaDoc coll) {
188         if ( coll.size()> 0 ) {
189             write();
190             return set.addAll(coll);
191         }
192         else {
193             return false;
194         }
195     }
196
197     /**
198      * @see java.util.Set#retainAll(Collection)
199      */

200     public boolean retainAll(Collection JavaDoc coll) {
201         write();
202         return set.retainAll(coll);
203     }
204
205     /**
206      * @see java.util.Set#removeAll(Collection)
207      */

208     public boolean removeAll(Collection JavaDoc coll) {
209         if ( coll.size()>0 ) {
210             write();
211             return set.removeAll(coll);
212         }
213         else {
214             return false;
215         }
216     }
217
218     /**
219      * @see java.util.Set#clear()
220      */

221     public void clear() {
222         write();
223         set.clear();
224     }
225
226     public String JavaDoc toString() {
227         //if (needLoading) return "asleep";
228
read();
229         return set.toString();
230     }
231
232     public Object JavaDoc readFrom(
233             ResultSet JavaDoc rs,
234             CollectionPersister persister,
235             CollectionAliases descriptor,
236             Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
237         Object JavaDoc element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
238         if (element!=null) tempList.add(element);
239         return element;
240     }
241
242     public void beginRead() {
243         super.beginRead();
244         tempList = new ArrayList JavaDoc();
245     }
246
247     public boolean endRead() {
248         set.addAll(tempList);
249         tempList = null;
250         setInitialized();
251         return true;
252     }
253
254     public Iterator JavaDoc entries(CollectionPersister persister) {
255         return set.iterator();
256     }
257
258     public Serializable JavaDoc disassemble(CollectionPersister persister)
259     throws HibernateException {
260
261         Serializable JavaDoc[] result = new Serializable JavaDoc[ set.size() ];
262         Iterator JavaDoc iter = set.iterator();
263         int i=0;
264         while ( iter.hasNext() ) {
265             result[i++] = persister.getElementType().disassemble( iter.next(), getSession(), null );
266         }
267         return result;
268
269     }
270
271     public Iterator JavaDoc getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
272         Type elementType = persister.getElementType();
273         final java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
274         ArrayList JavaDoc deletes = new ArrayList JavaDoc( sn.size() );
275         Iterator JavaDoc iter = sn.keySet().iterator();
276         while ( iter.hasNext() ) {
277             Object JavaDoc test = iter.next();
278             if ( !set.contains(test) ) {
279                 // the element has been removed from the set
280
deletes.add(test);
281             }
282         }
283         iter = set.iterator();
284         while ( iter.hasNext() ) {
285             Object JavaDoc test = iter.next();
286             Object JavaDoc oldValue = sn.get(test);
287             if ( oldValue!=null && elementType.isDirty( test, oldValue, getSession() ) ) {
288                 // the element has changed
289
deletes.add(oldValue);
290             }
291         }
292         return deletes.iterator();
293     }
294
295     public boolean needsInserting(Object JavaDoc entry, int i, Type elemType) throws HibernateException {
296         final java.util.Map JavaDoc sn = (java.util.Map JavaDoc) getSnapshot();
297         Object JavaDoc oldValue = sn.get(entry);
298         // note that it might be better to iterate the snapshot but this is safe,
299
// assuming the user implements equals() properly, as required by the Set
300
// contract!
301
return oldValue==null || elemType.isDirty( oldValue, entry, getSession() );
302     }
303
304     public boolean needsUpdating(Object JavaDoc entry, int i, Type elemType) {
305         return false;
306     }
307
308     public boolean isRowUpdatePossible() {
309         return false;
310     }
311
312     public Object JavaDoc getIndex(Object JavaDoc entry, int i, CollectionPersister persister) {
313         throw new UnsupportedOperationException JavaDoc("Sets don't have indexes");
314     }
315
316     public Object JavaDoc getElement(Object JavaDoc entry) {
317         return entry;
318     }
319
320     public Object JavaDoc getSnapshotElement(Object JavaDoc entry, int i) {
321         throw new UnsupportedOperationException JavaDoc("Sets don't support updating by element");
322     }
323
324     public boolean equals(Object JavaDoc other) {
325         read();
326         return set.equals(other);
327     }
328
329     public int hashCode() {
330         read();
331         return set.hashCode();
332     }
333
334     public boolean entryExists(Object JavaDoc key, int i) {
335         return true;
336     }
337
338     public boolean isWrapper(Object JavaDoc collection) {
339         return set==collection;
340     }
341
342 }
343
Popular Tags