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.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.ListIterator ; 13 14 import org.hibernate.EntityMode; 15 import org.hibernate.HibernateException; 16 import org.hibernate.loader.CollectionAliases; 17 import org.hibernate.engine.SessionImplementor; 18 import org.hibernate.persister.collection.CollectionPersister; 19 import org.hibernate.type.Type; 20 21 33 public class PersistentIdentifierBag extends AbstractPersistentCollection implements java.util.List { 34 35 protected java.util.List values; protected java.util.Map identifiers; 38 public PersistentIdentifierBag(SessionImplementor session) { 39 super(session); 40 } 41 42 public PersistentIdentifierBag() {} 44 public PersistentIdentifierBag(SessionImplementor session, java.util.Collection coll) { 45 super(session); 46 if (coll instanceof java.util.List ) { 47 values = (java.util.List ) coll; 48 } 49 else { 50 values = new ArrayList (); 51 Iterator iter = coll.iterator(); 52 while ( iter.hasNext() ) { 53 values.add( iter.next() ); 54 } 55 } 56 setInitialized(); 57 setDirectlyAccessible(true); 58 identifiers = new HashMap (); 59 } 60 61 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 62 throws HibernateException { 63 beforeInitialize(persister); 64 Serializable [] array = (Serializable []) disassembled; 65 for ( int i=0; i<array.length; i+=2 ) { 66 identifiers.put( 67 new Integer (i/2), 68 persister.getIdentifierType().assemble( array[i], getSession(), owner ) 69 ); 70 values.add( persister.getElementType().assemble( array[i+1], getSession(), owner ) ); 71 } 72 } 73 74 public Object getIdentifier(Object entry, int i) { 75 return identifiers.get( new Integer (i) ); 76 } 77 78 public boolean isWrapper(Object collection) { 79 return values==collection; 80 } 81 82 public boolean add(Object o) { 83 write(); 84 values.add(o); 85 return true; 86 } 87 88 public void clear() { 89 write(); 90 values.clear(); 91 identifiers.clear(); 92 } 93 94 public boolean contains(Object o) { 95 read(); 96 return values.contains(o); 97 } 98 99 public boolean containsAll(Collection c) { 100 read(); 101 return values.containsAll(c); 102 } 103 104 public boolean isEmpty() { 105 read(); 106 return values.isEmpty(); 107 } 108 109 public Iterator iterator() { 110 read(); 111 return new IteratorProxy( values.iterator() ); 112 } 113 114 public boolean remove(Object o) { 115 write(); 116 int index = values.indexOf(o); 117 if (index>=0) { 118 beforeRemove(index); 119 values.remove(index); 120 return true; 121 } 122 else { 123 return false; 124 } 125 } 126 127 public boolean removeAll(Collection c) { 128 if ( c.size()>0 ) { 129 boolean result = false; 132 Iterator iter = c.iterator(); 133 while ( iter.hasNext() ) { 134 if ( remove( iter.next() ) ) result=true; 135 } 136 return result; 137 } 138 else { 139 return false; 140 } 141 } 142 143 public boolean retainAll(Collection c) { 144 write(); 145 return values.retainAll(c); 146 } 147 148 public int size() { 149 read(); 150 return values.size(); 151 } 152 153 public Object [] toArray() { 154 read(); 155 return values.toArray(); 156 } 157 158 public Object [] toArray(Object [] a) { 159 read(); 160 return values.toArray(a); 161 } 162 163 public void beforeInitialize(CollectionPersister persister) { 164 identifiers = new HashMap (); 165 values = new ArrayList (); 166 } 167 168 public Serializable disassemble(CollectionPersister persister) 169 throws HibernateException { 170 171 Serializable [] result = new Serializable [ values.size() * 2 ]; 172 int i=0; 173 for (int j=0; j< values.size(); j++) { 174 Object value = values.get(j); 175 result[i++] = persister.getIdentifierType().disassemble( identifiers.get( new Integer (j) ), getSession(), null ); 176 result[i++] = persister.getElementType().disassemble( value, getSession(), null ); 177 } 178 return result; 179 } 180 181 public boolean empty() { 182 return values.isEmpty(); 183 } 184 185 public Iterator entries(CollectionPersister persister) { 186 return values.iterator(); 187 } 188 189 public boolean entryExists(Object entry, int i) { 190 return entry!=null; 191 } 192 193 public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { 194 Type elementType = persister.getElementType(); 195 java.util.Map snap = (java.util.Map ) getSnapshot(); 196 if ( snap.size()!= values.size() ) return false; 197 for ( int i=0; i<values.size(); i++ ) { 198 Object value = values.get(i); 199 Object id = identifiers.get( new Integer (i) ); 200 if (id==null) return false; 201 Object old = snap.get(id); 202 if ( elementType.isDirty( old, value, getSession() ) ) return false; 203 } 204 return true; 205 } 206 207 public boolean isSnapshotEmpty(Serializable snapshot) { 208 return ( (java.util.Map ) snapshot ).isEmpty(); 209 } 210 211 public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { 212 java.util.Map snap = (java.util.Map ) getSnapshot(); 213 java.util.List deletes = new ArrayList ( snap.keySet() ); 214 for ( int i=0; i<values.size(); i++ ) { 215 if ( values.get(i)!=null ) deletes.remove( identifiers.get( new Integer (i) ) ); 216 } 217 return deletes.iterator(); 218 } 219 220 public Object getIndex(Object entry, int i, CollectionPersister persister) { 221 throw new UnsupportedOperationException ("Bags don't have indexes"); 222 } 223 224 public Object getElement(Object entry) { 225 return entry; 226 } 227 228 public Object getSnapshotElement(Object entry, int i) { 229 java.util.Map snap = (java.util.Map ) getSnapshot(); 230 Object id = identifiers.get( new Integer (i) ); 231 return snap.get(id); 232 } 233 234 public boolean needsInserting(Object entry, int i, Type elemType) 235 throws HibernateException { 236 237 java.util.Map snap = (java.util.Map ) getSnapshot(); 238 Object id = identifiers.get( new Integer (i) ); 239 return entry!=null && ( id==null || snap.get(id)==null ); 240 } 241 242 public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException { 243 244 if (entry==null) return false; 245 java.util.Map snap = (java.util.Map ) getSnapshot(); 246 Object id = identifiers.get( new Integer (i) ); 247 if (id==null) return false; 248 Object old = snap.get(id); 249 return old!=null && elemType.isDirty( old, entry, getSession() ); 250 } 251 252 253 public Object readFrom( 254 ResultSet rs, 255 CollectionPersister persister, 256 CollectionAliases descriptor, 257 Object owner) 258 throws HibernateException, SQLException { 259 260 Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ); 261 Object old = identifiers.put( 262 new Integer ( values.size() ), 263 persister.readIdentifier( rs, descriptor.getSuffixedIdentifierAlias(), getSession() ) 264 ); 265 if ( old==null ) values.add(element); return element; 267 } 268 269 public Serializable getSnapshot(CollectionPersister persister) 270 throws HibernateException { 271 272 EntityMode entityMode = getSession().getEntityMode(); 273 274 HashMap map = new HashMap ( values.size() ); 275 Iterator iter = values.iterator(); 276 int i=0; 277 while ( iter.hasNext() ) { 278 Object value = iter.next(); 279 map.put( 280 identifiers.get( new Integer (i++) ), 281 persister.getElementType().deepCopy(value, entityMode, persister.getFactory()) 282 ); 283 } 284 return map; 285 } 286 287 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 288 java.util.Map sn = (java.util.Map ) snapshot; 289 return getOrphans( sn.values(), values, entityName, getSession() ); 290 } 291 292 public void preInsert(CollectionPersister persister) throws HibernateException { 293 Iterator iter = values.iterator(); 294 int i=0; 295 while ( iter.hasNext() ) { 296 Object entry = iter.next(); 297 Integer loc = new Integer (i++); 298 if ( !identifiers.containsKey(loc) ) { Serializable id = persister.getIdentifierGenerator().generate( getSession(), entry ); 300 identifiers.put(loc, id); 301 } 302 } 303 } 304 305 public void add(int index, Object element) { 306 write(); 307 beforeAdd(index); 308 values.add(index, element); 309 } 310 311 public boolean addAll(int index, Collection c) { 312 if ( c.size()>0 ) { 313 Iterator iter = c.iterator(); 316 while ( iter.hasNext() ) add( index++, iter.next() ); 317 return true; 318 } 319 else { 320 return false; 321 } 322 } 323 324 public Object get(int index) { 325 read(); 326 return values.get(index); 327 } 328 329 public int indexOf(Object o) { 330 read(); 331 return values.indexOf(o); 332 } 333 334 public int lastIndexOf(Object o) { 335 read(); 336 return values.lastIndexOf(o); 337 } 338 339 public ListIterator listIterator() { 340 read(); 341 return new ListIteratorProxy( values.listIterator() ); 342 } 343 344 public ListIterator listIterator(int index) { 345 read(); 346 return new ListIteratorProxy( values.listIterator(index) ); 347 } 348 349 private void beforeRemove(int index) { 350 Object removedId = identifiers.get( new Integer (index) ); 351 int last = values.size()-1; 352 for ( int i=index; i<last; i++ ) { 353 Object id = identifiers.get( new Integer (i+1) ); 354 if ( id==null ) { 355 identifiers.remove( new Integer (i) ); 356 } 357 else { 358 identifiers.put( new Integer (i), id ); 359 } 360 } 361 identifiers.put( new Integer (last), removedId ); 362 } 363 364 private void beforeAdd(int index) { 365 for ( int i=index; i<values.size(); i++ ) { 366 identifiers.put( new Integer (i+1), identifiers.get( new Integer (i) ) ); 367 } 368 identifiers.remove( new Integer (index) ); 369 } 370 371 public Object remove(int index) { 372 write(); 373 beforeRemove(index); 374 return values.remove(index); 375 } 376 377 public Object set(int index, Object element) { 378 write(); 379 return values.set(index, element); 380 } 381 382 public List subList(int fromIndex, int toIndex) { 383 read(); 384 return new ListProxy( values.subList(fromIndex, toIndex) ); 385 } 386 387 public boolean addAll(Collection c) { 388 if ( c.size()> 0 ) { 389 write(); 390 return values.addAll(c); 391 } 392 else { 393 return false; 394 } 395 } 396 397 public void afterRowInsert( 398 CollectionPersister persister, 399 Object entry, 400 int i) 401 throws HibernateException { 402 } 404 405 } 406 | Popular Tags |