KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PersistentList.java,v 1.19 2005/07/21 17:09:20 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.Iterator JavaDoc;
10 import java.util.ListIterator JavaDoc;
11
12 import org.hibernate.EntityMode;
13 import org.hibernate.HibernateException;
14 import org.hibernate.loader.CollectionAliases;
15 import org.hibernate.engine.SessionImplementor;
16 import org.hibernate.persister.collection.CollectionPersister;
17 import org.hibernate.type.Type;
18
19 /**
20  * A persistent wrapper for a <tt>java.util.List</tt>. Underlying
21  * collection is an <tt>ArrayList</tt>.
22  *
23  * @see java.util.ArrayList
24  * @author Gavin King
25  */

26 public class PersistentList extends AbstractPersistentCollection implements java.util.List JavaDoc {
27
28     protected java.util.List JavaDoc list;
29
30     public Serializable JavaDoc getSnapshot(CollectionPersister persister) throws HibernateException {
31         
32         EntityMode entityMode = getSession().getEntityMode();
33         
34         ArrayList JavaDoc clonedList = new ArrayList JavaDoc( list.size() );
35         Iterator JavaDoc iter = list.iterator();
36         while ( iter.hasNext() ) {
37             Object JavaDoc deepCopy = persister.getElementType()
38                     .deepCopy( iter.next(), entityMode, persister.getFactory() );
39             clonedList.add( deepCopy );
40         }
41         return clonedList;
42     }
43
44     public Collection JavaDoc getOrphans(Serializable JavaDoc snapshot, String JavaDoc entityName) throws HibernateException {
45         java.util.List JavaDoc sn = (java.util.List JavaDoc) snapshot;
46         return getOrphans( sn, list, entityName, getSession() );
47     }
48
49     public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
50         Type elementType = persister.getElementType();
51         java.util.List JavaDoc sn = (java.util.List JavaDoc) getSnapshot();
52         if ( sn.size()!=this.list.size() ) return false;
53         Iterator JavaDoc iter = list.iterator();
54         Iterator JavaDoc sniter = sn.iterator();
55         while ( iter.hasNext() ) {
56             if ( elementType.isDirty( iter.next(), sniter.next(), getSession() ) ) return false;
57         }
58         return true;
59     }
60
61     public boolean isSnapshotEmpty(Serializable JavaDoc snapshot) {
62         return ( (Collection JavaDoc) snapshot ).isEmpty();
63     }
64     
65     public PersistentList(SessionImplementor session) {
66         super(session);
67     }
68
69     public PersistentList(SessionImplementor session, java.util.List JavaDoc list) {
70         super(session);
71         this.list = list;
72         setInitialized();
73         setDirectlyAccessible(true);
74     }
75     public void beforeInitialize(CollectionPersister persister) {
76         this.list = new ArrayList JavaDoc();
77     }
78
79     public boolean isWrapper(Object JavaDoc collection) {
80         return list==collection;
81     }
82     public PersistentList() {} //needed for SOAP libraries, etc
83

84     /**
85      * @see java.util.List#size()
86      */

87     public int size() {
88         read();
89         return list.size();
90     }
91
92     /**
93      * @see java.util.List#isEmpty()
94      */

95     public boolean isEmpty() {
96         read();
97         return list.isEmpty();
98     }
99
100     /**
101      * @see java.util.List#contains(Object)
102      */

103     public boolean contains(Object JavaDoc object) {
104         read();
105         return list.contains(object);
106     }
107
108     /**
109      * @see java.util.List#iterator()
110      */

111     public Iterator JavaDoc iterator() {
112         read();
113         return new IteratorProxy( list.iterator() );
114     }
115
116     /**
117      * @see java.util.List#toArray()
118      */

119     public Object JavaDoc[] toArray() {
120         read();
121         return list.toArray();
122     }
123
124     /**
125      * @see java.util.List#toArray(Object[])
126      */

127     public Object JavaDoc[] toArray(Object JavaDoc[] array) {
128         read();
129         return list.toArray(array);
130     }
131
132     /**
133      * @see java.util.List#add(Object)
134      */

135     public boolean add(Object JavaDoc object) {
136         if ( !queueAdd(object) ) {
137             write();
138             return list.add(object);
139         }
140         else {
141             return true;
142         }
143     }
144
145     /**
146      * @see java.util.List#remove(Object)
147      */

148     public boolean remove(Object JavaDoc value) {
149         write();
150         return list.remove(value);
151     }
152
153     /**
154      * @see java.util.List#containsAll(Collection)
155      */

156     public boolean containsAll(Collection JavaDoc coll) {
157         read();
158         return list.containsAll(coll);
159     }
160
161     /**
162      * @see java.util.List#addAll(Collection)
163      */

164     public boolean addAll(Collection JavaDoc c) {
165         if ( c.size()==0 ) return false;
166         if ( !queueAddAll(c) ) {
167             write();
168             return list.addAll(c);
169         }
170         else {
171             return c.size()>0;
172         }
173     }
174
175     public void delayedAddAll(Collection JavaDoc c) {
176         list.addAll(c);
177     }
178
179     /**
180      * @see java.util.List#addAll(int, Collection)
181      */

182     public boolean addAll(int index, Collection JavaDoc coll) {
183         if ( coll.size()>0 ) {
184             write();
185             return list.addAll(index, coll);
186         }
187         else {
188             return false;
189         }
190     }
191
192     /**
193      * @see java.util.List#removeAll(Collection)
194      */

195     public boolean removeAll(Collection JavaDoc coll) {
196         if ( coll.size()>0 ) {
197             write();
198             return list.removeAll(coll);
199         }
200         else {
201             return false;
202         }
203     }
204
205     /**
206      * @see java.util.List#retainAll(Collection)
207      */

208     public boolean retainAll(Collection JavaDoc coll) {
209         write();
210         return list.retainAll(coll);
211     }
212
213     /**
214      * @see java.util.List#clear()
215      */

216     public void clear() {
217         write();
218         list.clear();
219     }
220
221     /**
222      * @see java.util.List#get(int)
223      */

224     public Object JavaDoc get(int index) {
225         read();
226         return list.get(index);
227     }
228
229     /**
230      * @see java.util.List#set(int, Object)
231      */

232     public Object JavaDoc set(int index, Object JavaDoc value) {
233         write();
234         return list.set(index, value);
235     }
236
237     /**
238      * @see java.util.List#add(int, Object)
239      */

240     public void add(int index, Object JavaDoc value) {
241         write();
242         list.add(index, value);
243     }
244
245     /**
246      * @see java.util.List#remove(int)
247      */

248     public Object JavaDoc remove(int index) {
249         write();
250         return list.remove(index);
251     }
252
253     /**
254      * @see java.util.List#indexOf(Object)
255      */

256     public int indexOf(Object JavaDoc value) {
257         read();
258         return list.indexOf(value);
259     }
260
261     /**
262      * @see java.util.List#lastIndexOf(Object)
263      */

264     public int lastIndexOf(Object JavaDoc value) {
265         read();
266         return list.lastIndexOf(value);
267     }
268
269     /**
270      * @see java.util.List#listIterator()
271      */

272     public ListIterator JavaDoc listIterator() {
273         read();
274         return new ListIteratorProxy( list.listIterator() );
275     }
276
277     /**
278      * @see java.util.List#listIterator(int)
279      */

280     public ListIterator JavaDoc listIterator(int index) {
281         read();
282         return new ListIteratorProxy( list.listIterator(index) );
283     }
284
285     /**
286      * @see java.util.List#subList(int, int)
287      */

288     public java.util.List JavaDoc subList(int from, int to) {
289         read();
290         return new ListProxy( list.subList(from, to) );
291     }
292
293     public boolean empty() {
294         return list.isEmpty();
295     }
296
297     public String JavaDoc toString() {
298         read();
299         return list.toString();
300     }
301
302     public Object JavaDoc readFrom(ResultSet JavaDoc rs, CollectionPersister persister, CollectionAliases descriptor, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
303         Object JavaDoc element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ;
304         int index = ( (Integer JavaDoc) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue();
305         
306         //pad with nulls from the current last element up to the new index
307
for ( int i = list.size(); i<=index; i++) {
308             list.add(i, null);
309         }
310         
311         list.set(index, element);
312         return element;
313     }
314
315     public Iterator JavaDoc entries(CollectionPersister persister) {
316         return list.iterator();
317     }
318
319     public void initializeFromCache(CollectionPersister persister, Serializable JavaDoc disassembled, Object JavaDoc owner)
320     throws HibernateException {
321         beforeInitialize(persister);
322         Serializable JavaDoc[] array = (Serializable JavaDoc[]) disassembled;
323         for ( int i=0; i<array.length; i++ ) {
324             list.add( persister.getElementType().assemble( array[i], getSession(), owner ) );
325         }
326     }
327
328     public Serializable JavaDoc disassemble(CollectionPersister persister)
329     throws HibernateException {
330
331         int length = list.size();
332         Serializable JavaDoc[] result = new Serializable JavaDoc[length];
333         for ( int i=0; i<length; i++ ) {
334             result[i] = persister.getElementType().disassemble( list.get(i), getSession(), null );
335         }
336         return result;
337     }
338
339
340     public Iterator JavaDoc getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
341         java.util.List JavaDoc deletes = new ArrayList JavaDoc();
342         java.util.List JavaDoc sn = (java.util.List JavaDoc) getSnapshot();
343         int end;
344         if ( sn.size() > list.size() ) {
345             for ( int i=list.size(); i<sn.size(); i++ ) {
346                 deletes.add( indexIsFormula ? sn.get(i) : new Integer JavaDoc(i) );
347             }
348             end = list.size();
349         }
350         else {
351             end = sn.size();
352         }
353         for ( int i=0; i<end; i++ ) {
354             if ( list.get(i)==null && sn.get(i)!=null ) {
355                 deletes.add( indexIsFormula ? sn.get(i) : new Integer JavaDoc(i) );
356             }
357         }
358         return deletes.iterator();
359     }
360
361     public boolean needsInserting(Object JavaDoc entry, int i, Type elemType) throws HibernateException {
362         final java.util.List JavaDoc sn = (java.util.List JavaDoc) getSnapshot();
363         return list.get(i)!=null && ( i >= sn.size() || sn.get(i)==null );
364     }
365
366     public boolean needsUpdating(Object JavaDoc entry, int i, Type elemType) throws HibernateException {
367         final java.util.List JavaDoc sn = (java.util.List JavaDoc) getSnapshot();
368         return i<sn.size() && sn.get(i)!=null && list.get(i)!=null &&
369             elemType.isDirty( list.get(i), sn.get(i), getSession() );
370     }
371
372     public Object JavaDoc getIndex(Object JavaDoc entry, int i, CollectionPersister persister) {
373         return new Integer JavaDoc(i);
374     }
375
376     public Object JavaDoc getElement(Object JavaDoc entry) {
377         return entry;
378     }
379
380     public Object JavaDoc getSnapshotElement(Object JavaDoc entry, int i) {
381         final java.util.List JavaDoc sn = (java.util.List JavaDoc) getSnapshot();
382         return sn.get(i);
383     }
384
385     public boolean equals(Object JavaDoc other) {
386         read();
387         return list.equals(other);
388     }
389
390     public int hashCode() {
391         read();
392         return list.hashCode();
393     }
394
395     public boolean entryExists(Object JavaDoc entry, int i) {
396         return entry!=null;
397     }
398
399 }
400
Popular Tags