Author: mbenson
Date: Fri Oct 5 05:49:44 2012
New Revision: 1394367
URL: http://svn.apache.org/viewvc?rev=1394367&view=rev
Log:
[BVAL-113] Minor performance improvements
Modified:
bval/trunk/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaBean.java
bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
bval/trunk/pom.xml
Modified:
bval/trunk/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
URL:
http://svn.apache.org/viewvc/bval/trunk/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java?rev=1394367&r1=1394366&r2=1394367&view=diff
==============================================================================
---
bval/trunk/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
(original)
+++
bval/trunk/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java
Fri Oct 5 05:49:44 2012
@@ -78,7 +78,7 @@ public abstract class FeaturesCapable im
* @return T
*/
public <T> T getFeature(String key) {
- return getFeature(key, (T) null);
+ return getFeature(key, null);
}
/**
@@ -146,9 +146,7 @@ public abstract class FeaturesCapable im
protected void copyInto(FeaturesCapable target) {
target.features = target.createFeaturesMap();
target.features.putAll(features);
- if (validations != null) {
- target.validations = validations.clone();
- }
+ target.validations = ArrayUtils.clone(validations);
}
/**
@@ -176,7 +174,14 @@ public abstract class FeaturesCapable im
* to add
*/
public void addValidation(Validation validation) {
- validations = ArrayUtils.add(validations, validation);
+ if (this.validations == null) {
+ this.validations = new Validation[] { validation };
+ } else {
+ Validation[] newValidations = new
Validation[this.validations.length + 1];
+ System.arraycopy(this.validations, 0, newValidations, 0,
this.validations.length);
+ newValidations[validations.length] = validation;
+ this.validations = newValidations;
+ }
}
/**
Modified: bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaBean.java
URL:
http://svn.apache.org/viewvc/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaBean.java?rev=1394367&r1=1394366&r2=1394367&view=diff
==============================================================================
--- bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaBean.java
(original)
+++ bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaBean.java Fri
Oct 5 05:49:44 2012
@@ -16,10 +16,8 @@
*/
package org.apache.bval.model;
-import java.util.Arrays;
-import java.util.Comparator;
-
-import org.apache.commons.lang3.ArrayUtils;
+import java.util.Map;
+import java.util.TreeMap;
/**
* Description: the meta description of a bean or class. the class/bean itself
can have a map of features and an array
@@ -28,34 +26,12 @@ import org.apache.commons.lang3.ArrayUti
* @see MetaProperty
*/
public class MetaBean extends FeaturesCapable implements Cloneable,
Features.Bean {
- private static final long serialVersionUID = 1L;
-
- /**
- * Comparator for managing the sorted properties array.
- */
- private static class PropertyNameComparator implements Comparator<Object> {
- /** Static instance */
- static final PropertyNameComparator INSTANCE = new
PropertyNameComparator();
-
- /**
- * {@inheritDoc}
- */
- public int compare(Object o1, Object o2) {
- return getName(o1).compareTo(getName(o2));
- }
-
- private String getName(Object o) {
- if (o == null) {
- throw new NullPointerException();
- }
- return o instanceof MetaProperty ? ((MetaProperty) o).getName() :
String.valueOf(o);
- }
- }
+ private static final long serialVersionUID = 2L;
private String id;
private String name;
private Class<?> beanClass;
- private MetaProperty[] properties = new MetaProperty[0];
+ private Map<String, MetaProperty> properties = new TreeMap<String,
MetaProperty>();
/**
* Get the id.
@@ -120,7 +96,7 @@ public class MetaBean extends FeaturesCa
* @return MetaProperty[]
*/
public MetaProperty[] getProperties() {
- return ArrayUtils.clone(properties);
+ return properties.values().toArray(new
MetaProperty[this.properties.size()]);
}
/**
@@ -130,8 +106,10 @@ public class MetaBean extends FeaturesCa
* the MetaProperty[] to set
*/
public void setProperties(MetaProperty[] properties) {
- this.properties = ArrayUtils.clone(properties);
- Arrays.sort(this.properties, PropertyNameComparator.INSTANCE);
+ this.properties.clear();
+ for (MetaProperty property : properties) {
+ this.properties.put(property.getName(), property);
+ }
}
/**
@@ -141,9 +119,7 @@ public class MetaBean extends FeaturesCa
* @return MetaProperty found or <code>null</code>
*/
public MetaProperty getProperty(String name) {
- final MetaProperty[] props = properties;
- int pos = Arrays.binarySearch(props, name,
PropertyNameComparator.INSTANCE);
- return pos < 0 ? null : props[pos];
+ return this.properties.get(name);
}
/**
@@ -153,8 +129,8 @@ public class MetaBean extends FeaturesCa
* @return true when at least one of the properties is a relationship
*/
public boolean hasRelationships() {
- for (MetaProperty p : properties) {
- if (p.isRelationship()) {
+ for (MetaProperty property : this.properties.values()) {
+ if (property.isRelationship()) {
return true;
}
}
@@ -167,7 +143,7 @@ public class MetaBean extends FeaturesCa
* @return boolean
*/
public boolean hasProperties() {
- return properties.length > 0;
+ return this.properties.size() > 0;
}
/**
@@ -178,27 +154,12 @@ public class MetaBean extends FeaturesCa
* if <code>null</code>, remove
*/
public void putProperty(String name, MetaProperty property) {
- if (property != null) {
+ if (property == null) {
+ this.properties.remove(name);
+ } else {
property.setParentMetaBean(this);
+ this.properties.put(name, property);
}
- Object key = property == null ? name : property;
- // make a local copy for consistency
- MetaProperty[] props = properties;
- int pos = Arrays.binarySearch(props, key,
PropertyNameComparator.INSTANCE);
- if (pos < 0) {
- if (property == null) {
- // store null property for unknown name == NOOP
- return;
- }
- props = ArrayUtils.add(props, 0 - pos - 1, property);
- } else {
- if (property == null) {
- props = ArrayUtils.remove(props, pos);
- } else {
- props[pos] = property;
- }
- }
- this.properties = props;
}
/**
@@ -216,9 +177,9 @@ public class MetaBean extends FeaturesCa
super.copyInto(target);
final MetaBean copy = (MetaBean) target;
if (properties != null) {
- copy.properties = properties.clone();
- for (int i = copy.properties.length - 1; i >= 0; i--) {
- copy.properties[i] = copy.properties[i].copy();
+ copy.properties = new TreeMap<String, MetaProperty>();
+ for (Map.Entry<String, MetaProperty> entry :
properties.entrySet()) {
+ copy.properties.put(entry.getKey(), (MetaProperty)
entry.getValue().copy());
}
}
}
Modified:
bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
URL:
http://svn.apache.org/viewvc/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java?rev=1394367&r1=1394366&r2=1394367&view=diff
==============================================================================
---
bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
(original)
+++
bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
Fri Oct 5 05:49:44 2012
@@ -183,7 +183,10 @@ public final class AnnotationProcessor {
strategies = new AccessStrategy[] { access };
prop.putFeature(Features.Property.REF_CASCADE, strategies);
} else if (!ArrayUtils.contains(strategies, access)) {
- prop.putFeature(Features.Property.REF_CASCADE,
ArrayUtils.add(strategies, access));
+ AccessStrategy[] newStrategies = new
AccessStrategy[strategies.length + 1];
+ System.arraycopy(strategies, 0, newStrategies, 0,
strategies.length);
+ newStrategies[strategies.length] = access;
+ prop.putFeature(Features.Property.REF_CASCADE, newStrategies);
}
return true;
}
Modified: bval/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/bval/trunk/pom.xml?rev=1394367&r1=1394366&r2=1394367&view=diff
==============================================================================
--- bval/trunk/pom.xml (original)
+++ bval/trunk/pom.xml Fri Oct 5 05:49:44 2012
@@ -205,6 +205,12 @@
</developer>
</developers>
+ <contributors>
+ <contributor>
+ <name>Jarek Gawor</name>
+ </contributor>
+ </contributors>
+
<distributionManagement>
<site>
<id>people.apache.org</id>