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.HashSet ; 11 import java.util.Iterator ; 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 29 public class PersistentSet extends AbstractPersistentCollection implements java.util.Set { 30 31 protected java.util.Set set; 32 protected transient java.util.List tempList; 33 34 public Serializable getSnapshot(CollectionPersister persister) 35 throws HibernateException { 36 EntityMode entityMode = getSession().getEntityMode(); 37 38 HashMap clonedSet = new HashMap ( set.size() ); 40 Iterator iter = set.iterator(); 41 while ( iter.hasNext() ) { 42 Object copied = persister.getElementType() 43 .deepCopy( iter.next(), entityMode, persister.getFactory() ); 44 clonedSet.put(copied, copied); 45 } 46 return clonedSet; 47 } 48 49 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 50 java.util.Map sn = (java.util.Map ) 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 sn = (java.util.Map ) getSnapshot(); 57 if ( sn.size()!=set.size() ) { 58 return false; 59 } 60 else { 61 Iterator iter = set.iterator(); 62 while ( iter.hasNext() ) { 63 Object test = iter.next(); 64 Object 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 snapshot) { 72 return ( (java.util.Map ) snapshot ).isEmpty(); 73 } 74 75 public PersistentSet(SessionImplementor session) { 76 super(session); 77 } 78 79 public PersistentSet() {} 81 public void beforeInitialize(CollectionPersister persister) { 82 this.set = persister.hasOrdering() ? 83 LinkedHashCollectionHelper.createLinkedHashSet() : 84 new HashSet (); 85 } 86 87 public PersistentSet(SessionImplementor session, java.util.Set set) { 88 super(session); 89 this.set = set; 94 setInitialized(); 95 setDirectlyAccessible(true); 96 } 97 98 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 99 throws HibernateException { 100 beforeInitialize(persister); 101 Serializable [] array = (Serializable []) disassembled; 102 for (int i=0; i<array.length; i++ ) { 103 Object 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 115 public int size() { 116 read(); 117 return set.size(); 118 } 119 120 123 public boolean isEmpty() { 124 read(); 125 return set.isEmpty(); 126 } 127 128 131 public boolean contains(Object o) { 132 read(); 133 return set.contains(o); 134 } 135 136 139 public Iterator iterator() { 140 read(); 141 return new IteratorProxy( set.iterator() ); 142 } 143 144 147 public Object [] toArray() { 148 read(); 149 return set.toArray(); 150 } 151 152 155 public Object [] toArray(Object [] array) { 156 read(); 157 return set.toArray(array); 158 } 159 160 163 public boolean add(Object value) { 164 write(); 165 return set.add(value); 166 } 167 168 171 public boolean remove(Object value) { 172 write(); 173 return set.remove(value); 174 } 175 176 179 public boolean containsAll(Collection coll) { 180 read(); 181 return set.containsAll(coll); 182 } 183 184 187 public boolean addAll(Collection coll) { 188 if ( coll.size()> 0 ) { 189 write(); 190 return set.addAll(coll); 191 } 192 else { 193 return false; 194 } 195 } 196 197 200 public boolean retainAll(Collection coll) { 201 write(); 202 return set.retainAll(coll); 203 } 204 205 208 public boolean removeAll(Collection coll) { 209 if ( coll.size()>0 ) { 210 write(); 211 return set.removeAll(coll); 212 } 213 else { 214 return false; 215 } 216 } 217 218 221 public void clear() { 222 write(); 223 set.clear(); 224 } 225 226 public String toString() { 227 read(); 229 return set.toString(); 230 } 231 232 public Object readFrom( 233 ResultSet rs, 234 CollectionPersister persister, 235 CollectionAliases descriptor, 236 Object owner) throws HibernateException, SQLException { 237 Object 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 (); 245 } 246 247 public boolean endRead() { 248 set.addAll(tempList); 249 tempList = null; 250 setInitialized(); 251 return true; 252 } 253 254 public Iterator entries(CollectionPersister persister) { 255 return set.iterator(); 256 } 257 258 public Serializable disassemble(CollectionPersister persister) 259 throws HibernateException { 260 261 Serializable [] result = new Serializable [ set.size() ]; 262 Iterator 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 getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { 272 Type elementType = persister.getElementType(); 273 final java.util.Map sn = (java.util.Map ) getSnapshot(); 274 ArrayList deletes = new ArrayList ( sn.size() ); 275 Iterator iter = sn.keySet().iterator(); 276 while ( iter.hasNext() ) { 277 Object test = iter.next(); 278 if ( !set.contains(test) ) { 279 deletes.add(test); 281 } 282 } 283 iter = set.iterator(); 284 while ( iter.hasNext() ) { 285 Object test = iter.next(); 286 Object oldValue = sn.get(test); 287 if ( oldValue!=null && elementType.isDirty( test, oldValue, getSession() ) ) { 288 deletes.add(oldValue); 290 } 291 } 292 return deletes.iterator(); 293 } 294 295 public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { 296 final java.util.Map sn = (java.util.Map ) getSnapshot(); 297 Object oldValue = sn.get(entry); 298 return oldValue==null || elemType.isDirty( oldValue, entry, getSession() ); 302 } 303 304 public boolean needsUpdating(Object entry, int i, Type elemType) { 305 return false; 306 } 307 308 public boolean isRowUpdatePossible() { 309 return false; 310 } 311 312 public Object getIndex(Object entry, int i, CollectionPersister persister) { 313 throw new UnsupportedOperationException ("Sets don't have indexes"); 314 } 315 316 public Object getElement(Object entry) { 317 return entry; 318 } 319 320 public Object getSnapshotElement(Object entry, int i) { 321 throw new UnsupportedOperationException ("Sets don't support updating by element"); 322 } 323 324 public boolean equals(Object 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 key, int i) { 335 return true; 336 } 337 338 public boolean isWrapper(Object collection) { 339 return set==collection; 340 } 341 342 } 343 | Popular Tags |