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.Map ; 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 PersistentMap extends AbstractPersistentCollection implements java.util.Map { 30 31 protected java.util.Map map; 32 33 public Serializable getSnapshot(CollectionPersister persister) 34 throws HibernateException { 35 EntityMode entityMode = getSession().getEntityMode(); 36 37 HashMap clonedMap = new HashMap ( map.size() ); 38 Iterator iter = map.entrySet().iterator(); 39 while ( iter.hasNext() ) { 40 java.util.Map.Entry e = (java.util.Map.Entry) iter.next(); 41 final Object copy = persister.getElementType() 42 .deepCopy( e.getValue(), entityMode, persister.getFactory() ); 43 clonedMap.put( e.getKey(), copy ); 44 } 45 return clonedMap; 46 } 47 48 public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { 49 java.util.Map sn = (java.util.Map ) snapshot; 50 return getOrphans( sn.values(), map.values(), entityName, getSession() ); 51 } 52 53 public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { 54 Type elementType = persister.getElementType(); 55 java.util.Map xmap = (java.util.Map ) getSnapshot(); 56 if ( xmap.size()!=this.map.size() ) return false; 57 Iterator iter = map.entrySet().iterator(); 58 while ( iter.hasNext() ) { 59 java.util.Map.Entry entry = (java.util.Map.Entry) iter.next(); 60 if ( elementType.isDirty( entry.getValue(), xmap.get( entry.getKey() ), getSession() ) ) return false; 61 } 62 return true; 63 } 64 65 public boolean isSnapshotEmpty(Serializable snapshot) { 66 return ( (java.util.Map ) snapshot ).isEmpty(); 67 } 68 69 public boolean isWrapper(Object collection) { 70 return map==collection; 71 } 72 public PersistentMap(SessionImplementor session) { 73 super(session); 74 } 75 76 public PersistentMap() {} 78 public void beforeInitialize(CollectionPersister persister) { 79 this.map = persister.hasOrdering() ? 80 LinkedHashCollectionHelper.createLinkedHashMap() : 81 new HashMap (); 82 } 83 84 public PersistentMap(SessionImplementor session, java.util.Map map) { 85 super(session); 86 this.map = map; 87 setInitialized(); 88 setDirectlyAccessible(true); 89 } 90 91 94 public int size() { 95 read(); 96 return map.size(); 97 } 98 99 102 public boolean isEmpty() { 103 read(); 104 return map.isEmpty(); 105 } 106 107 110 public boolean containsKey(Object key) { 111 read(); 112 return map.containsKey(key); 113 } 114 115 118 public boolean containsValue(Object value) { 119 read(); 120 return map.containsValue(value) ; 121 } 122 123 126 public Object get(Object key) { 127 read(); 128 return map.get(key); 129 } 130 131 134 public Object put(Object key, Object value) { 135 write(); 136 return map.put(key, value); 137 } 138 139 142 public Object remove(Object key) { 143 write(); 144 return map.remove(key); 145 } 146 147 150 public void putAll(java.util.Map puts) { 151 if ( puts.size()>0 ) { 152 write(); 153 map.putAll(puts); 154 } 155 } 156 157 160 public void clear() { 161 write(); 162 map.clear(); 163 } 164 165 168 public java.util.Set keySet() { 169 read(); 170 return new SetProxy( map.keySet() ); 171 } 172 173 176 public Collection values() { 177 read(); 178 return new SetProxy( map.values() ); 179 } 180 181 184 public java.util.Set entrySet() { 185 read(); 186 return new EntrySetProxy( map.entrySet() ); 187 } 188 189 public boolean empty() { 190 return map.isEmpty(); 191 } 192 193 public String toString() { 194 read(); 195 return map.toString(); 196 } 197 198 public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) 199 throws HibernateException, SQLException { 200 Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ); 201 Object index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ); 202 if ( element!=null ) map.put(index, element); 203 return element; 204 } 205 206 public Iterator entries(CollectionPersister persister) { 207 return map.entrySet().iterator(); 208 } 209 210 211 class EntrySetProxy implements java.util.Set { 212 private final java.util.Set set; 213 EntrySetProxy(java.util.Set set) { 214 this.set=set; 215 } 216 public boolean add(Object entry) { 217 return set.add(entry); 219 } 220 public boolean addAll(Collection entries) { 221 return set.addAll(entries); 223 } 224 public void clear() { 225 write(); 226 set.clear(); 227 } 228 public boolean contains(Object entry) { 229 return set.contains(entry); 230 } 231 public boolean containsAll(Collection entries) { 232 return set.containsAll(entries); 233 } 234 public boolean isEmpty() { 235 return set.isEmpty(); 236 } 237 public Iterator iterator() { 238 return new EntryIteratorProxy( set.iterator() ); 239 } 240 public boolean remove(Object entry) { 241 write(); 242 return set.remove(entry); 243 } 244 public boolean removeAll(Collection entries) { 245 write(); 246 return set.removeAll(entries); 247 } 248 public boolean retainAll(Collection entries) { 249 write(); 250 return set.retainAll(entries); 251 } 252 public int size() { 253 return set.size(); 254 } 255 public Object [] toArray() { 258 return set.toArray(); 259 } 260 public Object [] toArray(Object [] array) { 261 return set.toArray(array); 262 } 263 } 264 final class EntryIteratorProxy implements Iterator { 265 private final Iterator iter; 266 EntryIteratorProxy(Iterator iter) { 267 this.iter=iter; 268 } 269 public boolean hasNext() { 270 return iter.hasNext(); 271 } 272 public Object next() { 273 return new MapEntryProxy( (java.util.Map.Entry) iter.next() ); 274 } 275 public void remove() { 276 write(); 277 iter.remove(); 278 } 279 } 280 281 final class MapEntryProxy implements java.util.Map.Entry { 282 private final java.util.Map.Entry me; 283 MapEntryProxy( java.util.Map.Entry me ) { 284 this.me = me; 285 } 286 public Object getKey() { return me.getKey(); } 287 public Object getValue() { return me.getValue(); } 288 public boolean equals(Object o) { return me.equals(o); } 289 public int hashCode() { return me.hashCode(); } 290 public Object setValue(Object value) { 292 write(); 293 return me.setValue(value); 294 } 295 } 296 297 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 298 throws HibernateException { 299 beforeInitialize(persister); 300 Serializable [] array = (Serializable []) disassembled; 301 for (int i=0; i<array.length; i+=2 ) map.put( 302 persister.getIndexType().assemble( array[i], getSession(), owner ), 303 persister.getElementType().assemble( array[i+1], getSession(), owner ) 304 ); 305 } 306 307 public Serializable disassemble(CollectionPersister persister) throws HibernateException { 308 309 Serializable [] result = new Serializable [ map.size() * 2 ]; 310 Iterator iter = map.entrySet().iterator(); 311 int i=0; 312 while ( iter.hasNext() ) { 313 java.util.Map.Entry e = (java.util.Map.Entry) iter.next(); 314 result[i++] = persister.getIndexType().disassemble( e.getKey(), getSession(), null ); 315 result[i++] = persister.getElementType().disassemble( e.getValue(), getSession(), null ); 316 } 317 return result; 318 319 } 320 321 public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) 322 throws HibernateException { 323 java.util.List deletes = new ArrayList (); 324 Iterator iter = ( (java.util.Map ) getSnapshot() ).entrySet().iterator(); 325 while ( iter.hasNext() ) { 326 java.util.Map.Entry e = (java.util.Map.Entry) iter.next(); 327 Object key = e.getKey(); 328 if ( e.getValue()!=null && map.get(key)==null ) { 329 deletes.add( indexIsFormula ? e.getValue() : key ); 330 } 331 } 332 return deletes.iterator(); 333 } 334 335 public boolean needsInserting(Object entry, int i, Type elemType) 336 throws HibernateException { 337 final java.util.Map sn = (java.util.Map ) getSnapshot(); 338 java.util.Map.Entry e = (java.util.Map.Entry) entry; 339 return e.getValue()!=null && sn.get( e.getKey() )==null; 340 } 341 342 public boolean needsUpdating(Object entry, int i, Type elemType) 343 throws HibernateException { 344 final java.util.Map sn = (java.util.Map ) getSnapshot(); 345 java.util.Map.Entry e = (java.util.Map.Entry) entry; 346 Object snValue = sn.get( e.getKey() ); 347 return e.getValue()!=null && 348 snValue!=null && 349 elemType.isDirty( snValue, e.getValue(), getSession() ); 350 } 351 352 353 public Object getIndex(Object entry, int i, CollectionPersister persister) { 354 return ( (java.util.Map.Entry) entry ).getKey(); 355 } 356 357 public Object getElement(Object entry) { 358 return ( (java.util.Map.Entry) entry ).getValue(); 359 } 360 361 public Object getSnapshotElement(Object entry, int i) { 362 final java.util.Map sn = (java.util.Map ) getSnapshot(); 363 return sn.get( ( (java.util.Map.Entry) entry ).getKey() ); 364 } 365 366 public boolean equals(Object other) { 367 read(); 368 return map.equals(other); 369 } 370 371 public int hashCode() { 372 read(); 373 return map.hashCode(); 374 } 375 376 public boolean entryExists(Object entry, int i) { 377 return ( (Map.Entry ) entry ).getValue()!=null; 378 } 379 380 } 381 | Popular Tags |