bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r995265 - in /incubator/bval/sandbox/lang3-work: ./ bval-core/ bval-core/src/main/java/org/apache/bval/ bval-core/src/main/java/org/apache/bval/model/ bval-xstream/src/main/java/org/apache/bval/xml/
Date Wed, 08 Sep 2010 22:15:53 GMT
Author: mbenson
Date: Wed Sep  8 22:15:53 2010
New Revision: 995265

URL: http://svn.apache.org/viewvc?rev=995265&view=rev
Log:
FastHashMap has been removed in [collections]' trunk; plus class is duplicated in [beanutils].
 Switch to beanutils-core (even though FastHashMap is there as well), and [collections] dependency
is now eliminated in favor of Concurrent[Hash]Map

Modified:
    incubator/bval/sandbox/lang3-work/bval-core/pom.xml
    incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/MetaBeanCache.java
    incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
    incubator/bval/sandbox/lang3-work/bval-xstream/src/main/java/org/apache/bval/xml/XMLMetaBeanInfos.java
    incubator/bval/sandbox/lang3-work/pom.xml

Modified: incubator/bval/sandbox/lang3-work/bval-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/pom.xml?rev=995265&r1=995264&r2=995265&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/pom.xml (original)
+++ incubator/bval/sandbox/lang3-work/bval-core/pom.xml Wed Sep  8 22:15:53 2010
@@ -47,11 +47,7 @@
         </dependency>
         <dependency>
             <groupId>commons-beanutils</groupId>
-            <artifactId>commons-beanutils</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>commons-collections</groupId>
-            <artifactId>commons-collections</artifactId>
+            <artifactId>commons-beanutils-core</artifactId>
         </dependency>
         <dependency>
             <groupId>junit</groupId>

Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/MetaBeanCache.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/MetaBeanCache.java?rev=995265&r1=995264&r2=995265&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/MetaBeanCache.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/MetaBeanCache.java
Wed Sep  8 22:15:53 2010
@@ -16,11 +16,12 @@
  */
 package org.apache.bval;
 
-import org.apache.bval.model.MetaBean;
-import org.apache.commons.collections.FastHashMap;
-
 import java.io.Serializable;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.bval.model.MetaBean;
 
 /**
  * Description: a cache to hold metabeans by id and by class.<br/>
@@ -31,20 +32,17 @@ public class MetaBeanCache implements Me
     /**
      * Cache keyed by id.
      */
-    protected final FastHashMap cacheById;
+    protected final ConcurrentMap<String, MetaBean> cacheById = new ConcurrentHashMap<String,
MetaBean>();
     /**
      * Cache keyed by class.
      */
-    protected final FastHashMap cacheByClass;
+    protected final ConcurrentMap<Class<?>, MetaBean> cacheByClass = new ConcurrentHashMap<Class<?>,
MetaBean>();
 
     /**
      * Create a new MetaBeanCache instance.
      */
     public MetaBeanCache() {
-        this.cacheById = new FastHashMap();
-        cacheByClass = new FastHashMap();
-        cacheByClass.setFast(true);
-        cacheById.setFast(true);
+        super();
     }
 
     /**
@@ -70,21 +68,20 @@ public class MetaBeanCache implements Me
      * {@inheritDoc}
      */
     public MetaBean findForId(String beanInfoId) {
-        return (MetaBean) cacheById.get(beanInfoId);
+        return cacheById.get(beanInfoId);
     }
 
     /**
      * {@inheritDoc}
      */
     public MetaBean findForClass(Class<?> clazz) {
-        return (MetaBean) cacheByClass.get(clazz);
+        return cacheByClass.get(clazz);
     }
 
     /**
      * Return all cached MetaBeans by id.
      * @return live map
      */
-    @SuppressWarnings("unchecked")
     public Map<String, MetaBean> findAll() {
         return cacheById;
     }
@@ -97,7 +94,7 @@ public class MetaBeanCache implements Me
         cacheById.put(beanInfo.getId(), beanInfo);
         if (beanInfo.getBeanClass() != null &&
                 beanInfo.getId().equals(beanInfo.getBeanClass().getName())) {
-            cacheByClass.put(beanInfo.getBeanClass(), beanInfo);
+            cacheByClass.putIfAbsent(beanInfo.getBeanClass(), beanInfo);
         }
     }
 

Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java?rev=995265&r1=995264&r2=995265&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
Wed Sep  8 22:15:53 2010
@@ -16,60 +16,75 @@
  */
 package org.apache.bval.model;
 
-import org.apache.commons.collections.FastHashMap;
-import org.apache.commons.lang3.ArrayUtils;
-
 import java.io.Serializable;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.commons.lang3.ArrayUtils;
 
 /**
- * Description: abstract superclass of meta objects that support a map of features.<br/>
+ * Description: abstract superclass of meta objects that support a map of
+ * features.<br/>
  */
 public abstract class FeaturesCapable implements Serializable {
-    private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = -4045110242904814218L;
+
+    private ConcurrentMap<String, Object> features = new ConcurrentHashMap<String,
Object>();
 
-    private FastHashMap features = new FastHashMap();
     /** key = validation id, value = the validation */
     private Validation[] validations = new Validation[0];
 
+    private volatile boolean locking;
+    private ReentrantLock lock = new ReentrantLock(true);
+
     /**
      * Create a new FeaturesCapable instance.
      */
     public FeaturesCapable() {
-        features.setFast(true);
+        super();
     }
 
     /**
      * Get the (live) map of features.
+     * 
      * @return Map<String, Object>
      */
-    @SuppressWarnings("unchecked")
     public Map<String, Object> getFeatures() {
         return features;
     }
 
     /**
-     * Set whether to optimize read operations by accessing the
-     * features map in an unsynchronized manner.
+     * Set whether to optimize read operations by accessing the features map in
+     * an unsynchronized manner.
+     * 
      * @param fast
      */
     public void optimizeRead(boolean fast) {
-        features.setFast(fast);
+        lock.lock();
+        try {
+            this.locking = true;
+        } finally {
+            lock.unlock();
+        }
     }
 
     /**
      * Get the specified feature.
+     * 
      * @param <T>
      * @param key
      * @return T
      */
-    @SuppressWarnings("unchecked")
     public <T> T getFeature(String key) {
-        return (T) features.get(key);
+        return getFeature(key, (T) null);
     }
 
     /**
-     * Get the specified feature, returning <code>defaultValue</code> if undeclared.
+     * Get the specified feature, returning <code>defaultValue</code> if
+     * undeclared.
+     * 
      * @param <T>
      * @param key
      * @param defaultValue
@@ -77,33 +92,52 @@ public abstract class FeaturesCapable im
      */
     @SuppressWarnings("unchecked")
     public <T> T getFeature(String key, T defaultValue) {
-        final T v = (T) features.get(key);
-        if (v == null) {
-            return (features.containsKey(key)) ? null : defaultValue;
-        } else {
-            return v;
+        boolean release = acquireLockIfNeeded();
+        try {
+            if (!features.containsKey(key)) {
+                return defaultValue;
+            }
+            final T v = (T) features.get(key);
+            if (v == null) {
+                return (features.containsKey(key)) ? null : defaultValue;
+            } else {
+                return v;
+            }
+        } finally {
+            if (release) {
+                lock.unlock();
+            }
         }
     }
 
     /**
      * Convenience method to set a particular feature value.
+     * 
      * @param <T>
      * @param key
      * @param value
      */
     public <T> void putFeature(String key, T value) {
-        features.put(key, value);
+        boolean release = acquireLockIfNeeded();
+        try {
+            features.put(key, value);
+        } finally {
+            if (release) {
+                lock.unlock();
+            }
+        }
     }
 
     /**
      * Create a deep copy (copy receiver and copy properties).
+     * 
      * @param <T>
      * @return new T instance
      */
-    @SuppressWarnings("unchecked")
     public <T extends FeaturesCapable> T copy() {
         try {
-            T self = (T) clone();
+            @SuppressWarnings("unchecked")
+            final T self = (T) clone();
             copyInto(self);
             return self;
         } catch (CloneNotSupportedException e) {
@@ -112,12 +146,14 @@ public abstract class FeaturesCapable im
     }
 
     /**
-     * Copy this {@link FeaturesCapable} into another {@link FeaturesCapable} instance.
+     * Copy this {@link FeaturesCapable} into another {@link FeaturesCapable}
+     * instance.
+     * 
      * @param <T>
      * @param target
      */
     protected <T extends FeaturesCapable> void copyInto(T target) {
-        target.features = (FastHashMap) features.clone();
+        target.features = new ConcurrentHashMap<String, Object>(features);
         if (validations != null) {
             target.validations = validations.clone();
         }
@@ -125,6 +161,7 @@ public abstract class FeaturesCapable im
 
     /**
      * Get any validations set for this {@link FeaturesCapable}.
+     * 
      * @return Validation array
      */
     public Validation[] getValidations() {
@@ -133,22 +170,40 @@ public abstract class FeaturesCapable im
 
     /**
      * Add a validation to this {@link FeaturesCapable}.
-     * @param validation to add
+     * 
+     * @param validation
+     *            to add
      */
     public void addValidation(Validation validation) {
-        validations = (Validation[]) ArrayUtils.add(validations, validation);
+        validations = ArrayUtils.add(validations, validation);
     }
 
     /**
      * Search for an equivalent validation among those configured.
+     * 
      * @param aValidation
      * @return true if found
      */
     public boolean hasValidation(Validation aValidation) {
-        if (validations == null) return false;
+        if (validations == null)
+            return false;
         for (Validation validation : validations) {
-            if (validation.equals(aValidation)) return true;
+            if (validation.equals(aValidation))
+                return true;
         }
         return false;
     }
+
+    private boolean acquireLockIfNeeded() {
+        if (locking) {
+            lock.lock();
+            // double read
+            if (locking) {
+                return true;
+            }
+            lock.unlock();
+        }
+        return false;
+    }
+
 }

Modified: incubator/bval/sandbox/lang3-work/bval-xstream/src/main/java/org/apache/bval/xml/XMLMetaBeanInfos.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-xstream/src/main/java/org/apache/bval/xml/XMLMetaBeanInfos.java?rev=995265&r1=995264&r2=995265&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-xstream/src/main/java/org/apache/bval/xml/XMLMetaBeanInfos.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-xstream/src/main/java/org/apache/bval/xml/XMLMetaBeanInfos.java
Wed Sep  8 22:15:53 2010
@@ -16,17 +16,19 @@
  */
 package org.apache.bval.xml;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.bval.model.Validation;
+import org.apache.commons.lang3.ClassUtils;
+
 import com.thoughtworks.xstream.annotations.XStreamAlias;
 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
 import com.thoughtworks.xstream.annotations.XStreamImplicit;
 import com.thoughtworks.xstream.annotations.XStreamOmitField;
-import org.apache.bval.model.Validation;
-import org.apache.commons.collections.FastHashMap;
-import org.apache.commons.lang3.ClassUtils;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 
 /**
  * Description: root element of a xml-beanInfos document<br/>
@@ -94,28 +96,28 @@ public class XMLMetaBeanInfos {
     }
 
     private void initBeanLookup() {
-        beanLookup = new FastHashMap();
+        final HashMap<String, XMLMetaBean> map = new HashMap<String, XMLMetaBean>(beans.size());
         for (XMLMetaBean bean : beans) {
-            beanLookup.put(bean.getId(), bean);
+            map.put(bean.getId(), bean);
         }
-        ((FastHashMap) beanLookup).setFast(true);
+        beanLookup = new ConcurrentHashMap<String, XMLMetaBean>(map);
     }
 
     private void initValidationLookup() throws Exception {
-        validationLookup = new FastHashMap();
+        final HashMap<String, XMLMetaValidator> map = new HashMap<String, XMLMetaValidator>(validators.size());
         for (XMLMetaValidator xv : validators) {
             if (xv.getJava() != null) {
                 Validation validation =
                         (Validation) ClassUtils.getClass(xv.getJava()).newInstance();
                 xv.setValidation(validation);
-                validationLookup.put(xv.getId(), xv);
+                map.put(xv.getId(), xv);
             }
         }
-        ((FastHashMap) validationLookup).setFast(true);
+        validationLookup = new ConcurrentHashMap<String, XMLMetaValidator>(map);
     }
 
     public void addBean(XMLMetaBean bean) {
-        if (beans == null) beans = new ArrayList();
+        if (beans == null) beans = new ArrayList<XMLMetaBean>();
         beans.add(bean);
     }
 
@@ -126,7 +128,7 @@ public class XMLMetaBeanInfos {
     }
 
     public void addValidator(XMLMetaValidator validator) {
-        if (validators == null) validators = new ArrayList();
+        if (validators == null) validators = new ArrayList<XMLMetaValidator>();
         validators.add(validator);
     }
 }

Modified: incubator/bval/sandbox/lang3-work/pom.xml
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/pom.xml?rev=995265&r1=995264&r2=995265&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/pom.xml (original)
+++ incubator/bval/sandbox/lang3-work/pom.xml Wed Sep  8 22:15:53 2010
@@ -317,7 +317,7 @@
             </dependency>
             <dependency>
                 <groupId>commons-beanutils</groupId>
-                <artifactId>commons-beanutils</artifactId>
+                <artifactId>commons-beanutils-core</artifactId>
                 <version>1.8.3</version>
             </dependency>
             <dependency>
@@ -325,11 +325,6 @@
                 <artifactId>xstream</artifactId>
                 <version>1.3.1</version>
             </dependency>
-            <dependency>
-                <groupId>commons-collections</groupId>
-                <artifactId>commons-collections</artifactId>
-                <version>3.2.1</version>
-            </dependency>
             <!-- Optional - only used by bval-json -->
             <dependency>
                 <groupId>org.freemarker</groupId>



Mime
View raw message