ozeigermann    2004/06/02 05:34:33

  Modified:    transaction/src/java/org/apache/commons/transaction/memory
                        TransactionalMapWrapper.java
  Added:       transaction/src/java/org/apache/commons/transaction/memory
                        SetFactory.java MapFactory.java
  Log:
  Added a factories for temporary maps and sets
  
  Revision  Changes    Path
  1.7       +64 -9     
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java
  
  Index: TransactionalMapWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TransactionalMapWrapper.java      25 May 2004 14:28:14 -0000      1.6
  +++ TransactionalMapWrapper.java      2 Jun 2004 12:34:33 -0000       1.7
  @@ -52,10 +52,33 @@
   
       protected ThreadLocal activeTx = new ThreadLocal();
   
  +     protected MapFactory mapFactory = null;
  +     protected SetFactory setFactory = null;
  +
  +     /**
  +      * Creates a new transactional map wrapper. Temporary maps and sets to store 
transactional
  +      * data will be instances of [EMAIL PROTECTED] HashMap} and [EMAIL PROTECTED] 
HashSet}. 
  +      * 
  +      * @param wrapped map to be wrapped
  +      */
       public TransactionalMapWrapper(Map wrapped) {
           this.wrapped = wrapped;
       }
   
  +     /**
  +      * Creates a new transactional map wrapper. Temporary maps and sets to store 
transactional
  +      * data will be created and disposed using [EMAIL PROTECTED] MapFactory} and 
[EMAIL PROTECTED] SetFactory}.
  +      * 
  +      * @param wrapped map to be wrapped
  +      * @param mapFactory factory for temporary maps
  +      * @param setFactory factory for temporary sets
  +      */
  +     public TransactionalMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory 
setFactory) {
  +             this.wrapped = wrapped;
  +             this.mapFactory = mapFactory;
  +             this.setFactory = setFactory;
  +     }
  +
       public synchronized boolean isReadOnly() {
           TxContext txContext = getActiveTx();
   
  @@ -141,6 +164,8 @@
           }
   
           // simply forget about tx
  +             disposeMap(txContext.changes);
  +             disposeSet(txContext.deletes);
           setActiveTx(null);
       }
   
  @@ -171,6 +196,8 @@
           }
   
           // now forget about tx
  +        disposeMap(txContext.changes);
  +        disposeSet(txContext.deletes);
           setActiveTx(null);
       }
   
  @@ -283,7 +310,7 @@
           if (txContext == null) {
               return wrapped.entrySet();
           } else {
  -            Set entrySet = new HashSet();
  +            Set entrySet = createSet();
               Set keySet = keySet();
               // XXX expensive :(
               for (Iterator it = keySet.iterator(); it.hasNext();) {
  @@ -302,7 +329,7 @@
           if (txContext == null) {
               return wrapped.keySet();
           } else {
  -            Set keySet = (txContext.cleared ? new HashSet() : wrapped.keySet());
  +            Set keySet = (txContext.cleared ? createSet() : wrapped.keySet());
               keySet.removeAll(txContext.deletes);
               keySet.addAll(txContext.changes.keySet());
               return keySet;
  @@ -389,6 +416,34 @@
           activeTx.set(txContext);
       }
   
  +     protected Map createMap() {
  +             if (mapFactory != null) {
  +                     return mapFactory.createMap();
  +             } else {
  +                     return new HashMap();
  +             }
  +     }
  +
  +     protected void disposeMap(Map map) {
  +             if (mapFactory != null) {
  +                     mapFactory.disposeMap(map);
  +             }               
  +     }
  +     
  +     protected Set createSet() {
  +             if (setFactory != null) {
  +                     return setFactory.createSet();
  +             } else {
  +                     return new HashSet();
  +             }
  +     }
  +
  +     protected void disposeSet(Set set) {
  +             if (setFactory != null) {
  +                     setFactory.disposeSet(set);
  +             }               
  +     }
  +     
       // mostly copied from org.apache.commons.collections.map.AbstractHashedMap
       protected static class HashEntry implements Map.Entry {
           /** The key */
  @@ -436,7 +491,7 @@
           }
       }
   
  -    public static class TxContext {
  +    public class TxContext {
           protected final Set deletes;
           protected final Map changes;
           protected boolean rollbackOnly;
  @@ -445,8 +500,8 @@
           protected boolean readOnly;
   
           protected TxContext() {
  -            deletes = new HashSet();
  -            changes = new HashMap();
  +            deletes = createSet();
  +            changes = createMap();
               rollbackOnly = false;
               status = Status.STATUS_ACTIVE;
               cleared = false;
  
  
  
  1.1                  
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/SetFactory.java
  
  Index: SetFactory.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/SetFactory.java,v
 1.1 2004/06/02 12:34:33 ozeigermann Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/02 12:34:33 $
   *
   * ====================================================================
   *
   * Copyright 1999-2002 The Apache Software Foundation 
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   */
  
  package org.apache.commons.transaction.memory;
  
  import java.util.Set;
  
  /**
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
   * @version $Revision: 1.1 $
   */
  public interface SetFactory {
        public Set createSet();
        public void disposeSet(Set set);
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/MapFactory.java
  
  Index: MapFactory.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/MapFactory.java,v
 1.1 2004/06/02 12:34:33 ozeigermann Exp $
   * $Revision: 1.1 $
   * $Date: 2004/06/02 12:34:33 $
   *
   * ====================================================================
   *
   * Copyright 1999-2002 The Apache Software Foundation 
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   */
  
  package org.apache.commons.transaction.memory;
  
  import java.util.Map;
  
  /**
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
   * @version $Revision: 1.1 $
   */
  public interface MapFactory {
        public Map createMap();
        public void disposeMap(Map map);
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to