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.List ; 11 import java.util.ListIterator ; 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 20 28 public class PersistentBag extends AbstractPersistentCollection implements java.util.List { 29 30 protected java.util.List bag; 31 32 public PersistentBag(SessionImplementor session) { 33 super(session); 34 } 35 36 public PersistentBag(SessionImplementor session, java.util.Collection coll) { 37 super(session); 38 if (coll instanceof java.util.List ) { 39 bag = (java.util.List ) coll; 40 } 41 else { 42 bag = new ArrayList (); 43 Iterator iter = coll.iterator(); 44 while ( iter.hasNext() ) { 45 bag.add( iter.next() ); 46 } 47 } 48 setInitialized(); 49 setDirectlyAccessible(true); 50 } 51 52 public PersistentBag() {} 54 public boolean isWrapper(Object collection) { 55 return bag==collection; 56 } 57 public boolean empty() { 58 return bag.isEmpty(); 59 } 60 61 public Iterator entries(CollectionPersister persister) { 62 return bag.iterator(); 63 } 64 65 public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) 66 throws HibernateException, SQLException { 67 Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ; 70 if (element!=null) bag.add(element); 71 return element; 72 } 73 74 public void beforeInitialize(CollectionPersister persister) { 75 this.bag = new ArrayList (); 76 } 77 78 public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { 79 Type elementType = persister.getElementType(); 80 EntityMode entityMode = getSession().getEntityMode(); 81 java.util.List sn = (java.util.List ) getSnapshot(); 82 if ( sn.size()!=bag.size() ) return false; 83 Iterator iter = bag.iterator(); 84 while ( iter.hasNext() ) { 85 Object elt = iter.next(); 86 final boolean unequal = countOccurrences(elt, bag, elementType, entityMode) != 87 countOccurrences(elt, sn, elementType, entityMode); 88 if ( unequal ) return false; 89 } 90 return true; 91 } 92 93 public boolean isSnapshotEmpty(Serializable snapshot) { 94 return ( (Collection ) snapshot ).isEmpty(); 95 } 96 97 private int countOccurrences(Object element, java.util.List list, Type elementType, EntityMode entityMode) 98 throws HibernateException { 99 Iterator iter = list.iterator(); 100 int result=0; 101 while ( iter.hasNext() ) { 102 if ( elementType.isSame( element, iter.next(), entityMode ) ) result++; 103 } 104 return result; 105 } 106 107 public Serializable getSnapshot(CollectionPersister persister) 108 throws HibernateException { 109 EntityMode entityMode = getSession().getEntityMode(); 110 ArrayList clonedList = new ArrayList ( bag.size() ); 111 Iterator iter = bag.iterator(); 112 while ( iter.hasNext() ) { 113 clonedList.add( persister.getElementType().deepCopy( iter.next(), entityMode, persister.getFactory() ) ); 114 } 115 return clonedList; 116 } 117 118 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 119 java.util.List sn = (java.util.List ) snapshot; 120 return getOrphans( sn, bag, entityName, getSession() ); 121 } 122 123 124 public Serializable disassemble(CollectionPersister persister) 125 throws HibernateException { 126 127 int length = bag.size(); 128 Serializable [] result = new Serializable [length]; 129 for ( int i=0; i<length; i++ ) { 130 result[i] = persister.getElementType().disassemble( bag.get(i), getSession(), null ); 131 } 132 return result; 133 } 134 135 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 136 throws HibernateException { 137 beforeInitialize(persister); 138 Serializable [] array = (Serializable []) disassembled; 139 for ( int i=0; i<array.length; i++ ) { 140 Object element = persister.getElementType().assemble( array[i], getSession(), owner ); 141 if ( element!=null ) bag.add( element ); 142 } 143 } 144 145 public boolean needsRecreate(CollectionPersister persister) { 146 return !persister.isOneToMany(); 147 } 148 149 150 155 158 public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { 159 Type elementType = persister.getElementType(); 161 EntityMode entityMode = getSession().getEntityMode(); 162 ArrayList deletes = new ArrayList (); 163 java.util.List sn = (java.util.List ) getSnapshot(); 164 Iterator olditer = sn.iterator(); 165 int i=0; 166 while ( olditer.hasNext() ) { 167 Object old = olditer.next(); 168 Iterator newiter = bag.iterator(); 169 boolean found = false; 170 if ( bag.size()>i && elementType.isSame( old, bag.get(i++), entityMode ) ) { 171 found = true; 173 } 174 else { 175 while ( newiter.hasNext() ) { 178 if ( elementType.isSame( old, newiter.next(), entityMode ) ) { 179 found = true; 180 break; 181 } 182 } 183 } 184 if (!found) deletes.add(old); 185 } 186 return deletes.iterator(); 187 } 188 189 public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { 190 java.util.List sn = (java.util.List ) getSnapshot(); 192 final EntityMode entityMode = getSession().getEntityMode(); 193 if ( sn.size()>i && elemType.isSame( sn.get(i), entry, entityMode ) ) { 194 return false; 196 } 197 else { 198 Iterator olditer = sn.iterator(); 201 while ( olditer.hasNext() ) { 202 Object old = olditer.next(); 203 if ( elemType.isSame( old, entry, entityMode ) ) return false; 204 } 205 return true; 206 } 207 } 208 209 public boolean isRowUpdatePossible() { 210 return false; 211 } 212 213 public boolean needsUpdating(Object entry, int i, Type elemType) { 214 return false; 216 } 217 218 221 public int size() { 222 read(); 223 return bag.size(); 224 } 225 226 229 public boolean isEmpty() { 230 read(); 231 return bag.isEmpty(); 232 } 233 234 237 public boolean contains(Object o) { 238 read(); 239 return bag.contains(o); 240 } 241 242 245 public Iterator iterator() { 246 read(); 247 return new IteratorProxy( bag.iterator() ); 248 } 249 250 253 public Object [] toArray() { 254 read(); 255 return bag.toArray(); 256 } 257 258 261 public Object [] toArray(Object [] a) { 262 read(); 263 return bag.toArray(a); 264 } 265 266 269 public boolean add(Object o) { 270 if ( !queueAdd(o) ) { 271 write(); 272 return bag.add(o); 273 } 274 else { 275 return true; 276 } 277 } 278 279 282 public boolean remove(Object o) { 283 write(); 284 return bag.remove(o); 285 } 286 287 290 public boolean containsAll(Collection c) { 291 read(); 292 return bag.containsAll(c); 293 } 294 295 298 public boolean addAll(Collection c) { 299 if ( c.size()==0 ) return false; 300 if ( !queueAddAll(c) ) { 301 write(); 302 return bag.addAll(c); 303 } 304 else { 305 return c.size()>0; 306 } 307 } 308 309 public void delayedAddAll(Collection c) { 310 bag.addAll(c); 311 } 312 313 316 public boolean removeAll(Collection c) { 317 if ( c.size()>0 ) { 318 write(); 319 return bag.removeAll(c); 320 } 321 else { 322 return false; 323 } 324 } 325 326 329 public boolean retainAll(Collection c) { 330 write(); 331 return bag.retainAll(c); 332 } 333 334 337 public void clear() { 338 write(); 339 bag.clear(); 340 } 341 342 public Object getIndex(Object entry, int i, CollectionPersister persister) { 343 throw new UnsupportedOperationException ("Bags don't have indexes"); 344 } 345 346 public Object getElement(Object entry) { 347 return entry; 348 } 349 350 public Object getSnapshotElement(Object entry, int i) { 351 java.util.List sn = (java.util.List ) getSnapshot(); 352 return sn.get(i); 353 } 354 355 public int occurrences(Object o) { 356 read(); 357 Iterator iter = bag.iterator(); 358 int result=0; 359 while ( iter.hasNext() ) { 360 if ( o.equals( iter.next() ) ) result++; 361 } 362 return result; 363 } 364 365 367 370 public void add(int i, Object o) { 371 write(); 372 bag.add(i, o); 373 } 374 375 378 public boolean addAll(int i, Collection c) { 379 if ( c.size()>0 ) { 380 write(); 381 return bag.addAll(i, c); 382 } 383 else { 384 return false; 385 } 386 } 387 388 391 public Object get(int i) { 392 read(); 393 return bag.get(i); 394 } 395 396 399 public int indexOf(Object o) { 400 read(); 401 return bag.indexOf(o); 402 } 403 404 407 public int lastIndexOf(Object o) { 408 read(); 409 return bag.lastIndexOf(o); 410 } 411 412 415 public ListIterator listIterator() { 416 read(); 417 return new ListIteratorProxy( bag.listIterator() ); 418 } 419 420 423 public ListIterator listIterator(int i) { 424 read(); 425 return new ListIteratorProxy( bag.listIterator(i) ); 426 } 427 428 431 public Object remove(int i) { 432 write(); 433 return bag.remove(i); 434 } 435 436 439 public Object set(int i, Object o) { 440 write(); 441 return bag.set(i, o); 442 } 443 444 447 public List subList(int start, int end) { 448 read(); 449 return new ListProxy( bag.subList(start, end) ); 450 } 451 452 public String toString() { 453 read(); 454 return bag.toString(); 455 } 456 457 466 467 public boolean entryExists(Object entry, int i) { 468 return entry!=null; 469 } 470 471 478 public boolean equals(Object obj) { 479 return super.equals(obj); 480 } 481 482 485 public int hashCode() { 486 return super.hashCode(); 487 } 488 489 } 490 | Popular Tags |