1 package org.hibernate.collection; 3 4 import java.io.Serializable ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.ListIterator ; 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 26 public class PersistentList extends AbstractPersistentCollection implements java.util.List { 27 28 protected java.util.List list; 29 30 public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { 31 32 EntityMode entityMode = getSession().getEntityMode(); 33 34 ArrayList clonedList = new ArrayList ( list.size() ); 35 Iterator iter = list.iterator(); 36 while ( iter.hasNext() ) { 37 Object deepCopy = persister.getElementType() 38 .deepCopy( iter.next(), entityMode, persister.getFactory() ); 39 clonedList.add( deepCopy ); 40 } 41 return clonedList; 42 } 43 44 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 45 java.util.List sn = (java.util.List ) 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 sn = (java.util.List ) getSnapshot(); 52 if ( sn.size()!=this.list.size() ) return false; 53 Iterator iter = list.iterator(); 54 Iterator 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 snapshot) { 62 return ( (Collection ) snapshot ).isEmpty(); 63 } 64 65 public PersistentList(SessionImplementor session) { 66 super(session); 67 } 68 69 public PersistentList(SessionImplementor session, java.util.List 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 (); 77 } 78 79 public boolean isWrapper(Object collection) { 80 return list==collection; 81 } 82 public PersistentList() {} 84 87 public int size() { 88 read(); 89 return list.size(); 90 } 91 92 95 public boolean isEmpty() { 96 read(); 97 return list.isEmpty(); 98 } 99 100 103 public boolean contains(Object object) { 104 read(); 105 return list.contains(object); 106 } 107 108 111 public Iterator iterator() { 112 read(); 113 return new IteratorProxy( list.iterator() ); 114 } 115 116 119 public Object [] toArray() { 120 read(); 121 return list.toArray(); 122 } 123 124 127 public Object [] toArray(Object [] array) { 128 read(); 129 return list.toArray(array); 130 } 131 132 135 public boolean add(Object object) { 136 if ( !queueAdd(object) ) { 137 write(); 138 return list.add(object); 139 } 140 else { 141 return true; 142 } 143 } 144 145 148 public boolean remove(Object value) { 149 write(); 150 return list.remove(value); 151 } 152 153 156 public boolean containsAll(Collection coll) { 157 read(); 158 return list.containsAll(coll); 159 } 160 161 164 public boolean addAll(Collection 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 c) { 176 list.addAll(c); 177 } 178 179 182 public boolean addAll(int index, Collection coll) { 183 if ( coll.size()>0 ) { 184 write(); 185 return list.addAll(index, coll); 186 } 187 else { 188 return false; 189 } 190 } 191 192 195 public boolean removeAll(Collection coll) { 196 if ( coll.size()>0 ) { 197 write(); 198 return list.removeAll(coll); 199 } 200 else { 201 return false; 202 } 203 } 204 205 208 public boolean retainAll(Collection coll) { 209 write(); 210 return list.retainAll(coll); 211 } 212 213 216 public void clear() { 217 write(); 218 list.clear(); 219 } 220 221 224 public Object get(int index) { 225 read(); 226 return list.get(index); 227 } 228 229 232 public Object set(int index, Object value) { 233 write(); 234 return list.set(index, value); 235 } 236 237 240 public void add(int index, Object value) { 241 write(); 242 list.add(index, value); 243 } 244 245 248 public Object remove(int index) { 249 write(); 250 return list.remove(index); 251 } 252 253 256 public int indexOf(Object value) { 257 read(); 258 return list.indexOf(value); 259 } 260 261 264 public int lastIndexOf(Object value) { 265 read(); 266 return list.lastIndexOf(value); 267 } 268 269 272 public ListIterator listIterator() { 273 read(); 274 return new ListIteratorProxy( list.listIterator() ); 275 } 276 277 280 public ListIterator listIterator(int index) { 281 read(); 282 return new ListIteratorProxy( list.listIterator(index) ); 283 } 284 285 288 public java.util.List 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 toString() { 298 read(); 299 return list.toString(); 300 } 301 302 public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) throws HibernateException, SQLException { 303 Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ; 304 int index = ( (Integer ) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue(); 305 306 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 entries(CollectionPersister persister) { 316 return list.iterator(); 317 } 318 319 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 320 throws HibernateException { 321 beforeInitialize(persister); 322 Serializable [] array = (Serializable []) 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 disassemble(CollectionPersister persister) 329 throws HibernateException { 330 331 int length = list.size(); 332 Serializable [] result = new Serializable [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 getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { 341 java.util.List deletes = new ArrayList (); 342 java.util.List sn = (java.util.List ) 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 (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 (i) ); 356 } 357 } 358 return deletes.iterator(); 359 } 360 361 public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { 362 final java.util.List sn = (java.util.List ) getSnapshot(); 363 return list.get(i)!=null && ( i >= sn.size() || sn.get(i)==null ); 364 } 365 366 public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException { 367 final java.util.List sn = (java.util.List ) 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 getIndex(Object entry, int i, CollectionPersister persister) { 373 return new Integer (i); 374 } 375 376 public Object getElement(Object entry) { 377 return entry; 378 } 379 380 public Object getSnapshotElement(Object entry, int i) { 381 final java.util.List sn = (java.util.List ) getSnapshot(); 382 return sn.get(i); 383 } 384 385 public boolean equals(Object 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 entry, int i) { 396 return entry!=null; 397 } 398 399 } 400 | Popular Tags |