[hibernate-commits] Hibernate SVN: r10470 - trunk/Hibernate3/src/org/hibernate/collection

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Sep 8 08:53:07 EDT 2006


Author: steve.ebersole at jboss.com
Date: 2006-09-08 08:53:06 -0400 (Fri, 08 Sep 2006)
New Revision: 10470

Modified:
   trunk/Hibernate3/src/org/hibernate/collection/PersistentMap.java
   trunk/Hibernate3/src/org/hibernate/collection/PersistentSet.java
Log:
formatting

Modified: trunk/Hibernate3/src/org/hibernate/collection/PersistentMap.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/collection/PersistentMap.java	2006-09-08 12:23:18 UTC (rev 10469)
+++ trunk/Hibernate3/src/org/hibernate/collection/PersistentMap.java	2006-09-08 12:53:06 UTC (rev 10470)
@@ -31,6 +31,39 @@
 
 	protected Map map;
 
+	/**
+	 * Empty constructor.
+	 * <p/>
+	 * Note: this form is not ever ever ever used by Hibernate; it is, however,
+	 * needed for SOAP libraries and other such marshalling code.
+	 */
+	public PersistentMap() {
+		// intentionally empty
+	}
+
+	/**
+	 * Instantiates a lazy map (the underlying map is un-initialized).
+	 *
+	 * @param session The session to which this map will belong.
+	 */
+	public PersistentMap(SessionImplementor session) {
+		super(session);
+	}
+
+	/**
+	 * Instantiates a non-lazy map (the underlying map is constructed
+	 * from the incoming map reference).
+	 *
+	 * @param session The session to which this map will belong.
+	 * @param map The underlying map data.
+	 */
+	public PersistentMap(SessionImplementor session, Map map) {
+		super(session);
+		this.map = map;
+		setInitialized();
+		setDirectlyAccessible(true);
+	}
+
 	public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
 		EntityMode entityMode = getSession().getEntityMode();
 		HashMap clonedMap = new HashMap( map.size() );
@@ -68,23 +101,11 @@
 	public boolean isWrapper(Object collection) {
 		return map==collection;
 	}
-	
-	public PersistentMap(SessionImplementor session) {
-		super(session);
-	}
 
-	public PersistentMap() {} //needed for SOAP libraries, etc
-
 	public void beforeInitialize(CollectionPersister persister, int anticipatedSize) {
 		this.map = ( Map ) persister.getCollectionType().instantiate( anticipatedSize );
 	}
 
-	public PersistentMap(SessionImplementor session, Map map) {
-		super(session);
-		this.map = map;
-		setInitialized();
-		setDirectlyAccessible(true);
-	}
 
 	/**
 	 * @see java.util.Map#size()
@@ -130,14 +151,21 @@
 	 * @see java.util.Map#put(Object, Object)
 	 */
 	public Object put(Object key, Object value) {
-		Object old = isPutQueueEnabled() ?
-				readElementByIndex(key) : UNKNOWN;
-		if ( old==UNKNOWN ) {
-			write();
-			return map.put(key, value);
+		if ( isPutQueueEnabled() ) {
+			Object old = readElementByIndex( key );
+			queueOperation( new Put( key, value, old ) );
+			return old;
 		}
 		else {
-			queueOperation( new Put(key, value, old) );
+			initialize( true );
+			Object old = map.put( key, value );
+			// would be better to use the element-type to determine
+			// whether the old and the new are equal here; the problem being
+			// we do not necessarily have access to the element type in all
+			// cases
+			if ( value != old ) {
+				dirty();
+			}
 			return old;
 		}
 	}
@@ -146,15 +174,18 @@
 	 * @see java.util.Map#remove(Object)
 	 */
 	public Object remove(Object key) {
-		Object old = isPutQueueEnabled() ?
-				readElementByIndex(key) : UNKNOWN;
-		if ( old==UNKNOWN ) {
-			write();
-			return map.remove(key);
+		if ( isPutQueueEnabled() ) {
+			Object old = readElementByIndex( key );
+			queueOperation( new Remove( key, old ) );
+			return old;
 		}
 		else {
-			queueOperation( new Remove(key, old) );
-			return old;
+			// TODO : safe to interpret "map.remove(key) == null" as non-dirty?
+			initialize( true );
+			if ( map.containsKey( key ) ) {
+				dirty();
+			}
+			return map.remove( key );
 		}
 	}
 
@@ -163,8 +194,12 @@
 	 */
 	public void putAll(Map puts) {
 		if ( puts.size()>0 ) {
-			write();
-			map.putAll(puts);
+			initialize( true );
+			Iterator itr = puts.entrySet().iterator();
+			while ( itr.hasNext() ) {
+				Map.Entry entry = ( Entry ) itr.next();
+				put( entry.getKey(), entry.getValue() );
+			}
 		}
 	}
 
@@ -176,8 +211,11 @@
 			queueOperation( new Clear() );
 		}
 		else {
-			write();
-			map.clear();
+			initialize( true );
+			if ( ! map.isEmpty() ) {
+				dirty();
+				map.clear();
+			}
 		}
 	}
 

Modified: trunk/Hibernate3/src/org/hibernate/collection/PersistentSet.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/collection/PersistentSet.java	2006-09-08 12:23:18 UTC (rev 10469)
+++ trunk/Hibernate3/src/org/hibernate/collection/PersistentSet.java	2006-09-08 12:53:06 UTC (rev 10470)
@@ -31,6 +31,45 @@
 	protected Set set;
 	protected transient List tempList;
 
+	/**
+	 * Empty constructor.
+	 * <p/>
+	 * Note: this form is not ever ever ever used by Hibernate; it is, however,
+	 * needed for SOAP libraries and other such marshalling code.
+	 */
+	public PersistentSet() {
+		// intentionally empty
+	}
+
+	/**
+	 * Constructor matching super.  Instantiates a lazy set (the underlying
+	 * set is un-initialized).
+	 *
+	 * @param session The session to which this set will belong.
+	 */
+	public PersistentSet(SessionImplementor session) {
+		super( session );
+	}
+
+	/**
+	 * Instantiates a non-lazy set (the underlying set is constructed
+	 * from the incoming set reference).
+	 *
+	 * @param session The session to which this set will belong.
+	 * @param set The underlying set data.
+	 */
+	public PersistentSet(SessionImplementor session, java.util.Set set) {
+		super(session);
+		// Sets can be just a view of a part of another collection.
+		// do we need to copy it to be sure it won't be changing
+		// underneath us?
+		// ie. this.set.addAll(set);
+		this.set = set;
+		setInitialized();
+		setDirectlyAccessible(true);
+	}
+
+
 	public Serializable getSnapshot(CollectionPersister persister) 
 	throws HibernateException {
 		EntityMode entityMode = getSession().getEntityMode();
@@ -71,27 +110,10 @@
 	public boolean isSnapshotEmpty(Serializable snapshot) {
 		return ( (java.util.Map) snapshot ).isEmpty();
 	}
-	
-	public PersistentSet(SessionImplementor session) {
-		super(session);
-	}
 
-	public PersistentSet() {} //needed for SOAP libraries, etc
-
 	public void beforeInitialize(CollectionPersister persister, int anticipatedSize) {
 		this.set = ( Set ) persister.getCollectionType().instantiate( anticipatedSize );
 	}
-	
-	public PersistentSet(SessionImplementor session, java.util.Set set) {
-		super(session);
-		// Sets can be just a view of a part of another collection.
-		// do we need to copy it to be sure it won't be changing
-		// underneath us?
-		// ie. this.set.addAll(set);
-		this.set = set;
-		setInitialized();
-		setDirectlyAccessible(true);
-	}
 
 	public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner)
 	throws HibernateException {
@@ -162,11 +184,16 @@
 	 * @see java.util.Set#add(Object)
 	 */
 	public boolean add(Object value) {
-		Boolean exists = isOperationQueueEnabled() ?
-				readElementExistence(value) : null;
-		if (exists==null) {
-			write();
-			return set.add(value);
+		Boolean exists = isOperationQueueEnabled() ? readElementExistence( value ) : null;
+		if ( exists == null ) {
+			initialize( true );
+			if ( set.add( value ) ) {
+				dirty();
+				return true;
+			}
+			else {
+				return false;
+			}
 		}
 		else if ( exists.booleanValue() ) {
 			return false;
@@ -181,11 +208,16 @@
 	 * @see java.util.Set#remove(Object)
 	 */
 	public boolean remove(Object value) {
-		Boolean exists = isPutQueueEnabled() ?
-				readElementExistence(value) : null;
+		Boolean exists = isPutQueueEnabled() ? readElementExistence( value ) : null;
 		if ( exists==null ) {
-			write();
-			return set.remove(value);
+			initialize( true );
+			if ( set.remove( value ) ) {
+				dirty();
+				return true;
+			}
+			else {
+				return false;
+			}
 		}
 		else if ( exists.booleanValue() ) {
 			queueOperation( new SimpleRemove(value) );
@@ -208,9 +240,15 @@
 	 * @see java.util.Set#addAll(Collection)
 	 */
 	public boolean addAll(Collection coll) {
-		if ( coll.size()> 0 ) {
-			write();
-			return set.addAll(coll);
+		if ( coll.size() > 0 ) {
+			initialize( true );
+			if ( set.addAll( coll ) ) {
+				dirty();
+				return true;
+			}
+			else {
+				return false;
+			}
 		}
 		else {
 			return false;
@@ -221,17 +259,29 @@
 	 * @see java.util.Set#retainAll(Collection)
 	 */
 	public boolean retainAll(Collection coll) {
-		write();
-		return set.retainAll(coll);
+		initialize( true );
+		if ( set.addAll( coll ) ) {
+			dirty();
+			return true;
+		}
+		else {
+			return false;
+		}
 	}
 
 	/**
 	 * @see java.util.Set#removeAll(Collection)
 	 */
 	public boolean removeAll(Collection coll) {
-		if ( coll.size()>0 ) {
-			write();
-			return set.removeAll(coll);
+		if ( coll.size() > 0 ) {
+			initialize( true );
+			if ( set.removeAll( coll ) ) {
+				dirty();
+				return true;
+			}
+			else {
+				return false;
+			}
 		}
 		else {
 			return false;
@@ -246,8 +296,11 @@
 			queueOperation( new Clear() );
 		}
 		else {
-			write();
-			set.clear();
+			initialize( true );
+			if ( !set.isEmpty() ) {
+				set.clear();
+				dirty();
+			}
 		}
 	}
 




More information about the hibernate-commits mailing list