Author: rhauch
Date: 2008-12-11 15:55:11 -0500 (Thu, 11 Dec 2008)
New Revision: 682
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/NameValueFactory.java
Log:
DNA-250 - Creating a path from a parent path and a segment is not fast
Minor tweaks to NameValueFactory.create(String,...) to more efficiently determine the
pattern (either internal form with full namespace URIs, or traditional prefixed form) that
is used to first parse the string. Before this change, the internal pattern was almost
always used first, despite the fact that most names are created with strings containing
the prefixed form.
Also changed the BasicName constructor so that strings representing URIs are interned,
since there shouldn't be too many of those, and we really want there to be one string
instance for each namespace shared by all the names.
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java 2008-12-11
19:42:19 UTC (rev 681)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicName.java 2008-12-11
20:55:11 UTC (rev 682)
@@ -38,6 +38,12 @@
@Immutable
public class BasicName implements Name {
+ private String trimNonEmptyStrings( String value ) {
+ if (value == null) return null;
+ String trimmed = value.trim();
+ return trimmed.length() == 0 ? value : trimmed;
+ }
+
/**
*/
private static final long serialVersionUID = -1737537720336990144L;
@@ -48,8 +54,8 @@
public BasicName( String namespaceUri,
String localName ) {
CheckArg.isNotEmpty(localName, "localName");
- this.namespaceUri = namespaceUri != null ? namespaceUri.trim() : "";
- this.localName = localName != null ? localName.trim() : "";
+ this.namespaceUri = namespaceUri != null ? namespaceUri.trim().intern() :
"";
+ this.localName = trimNonEmptyStrings(localName);
this.hc = HashCode.compute(this.namespaceUri, this.localName);
}
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/NameValueFactory.java
===================================================================
---
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/NameValueFactory.java 2008-12-11
19:42:19 UTC (rev 681)
+++
trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/NameValueFactory.java 2008-12-11
20:55:11 UTC (rev 682)
@@ -89,7 +89,28 @@
if (value == null) return null;
if (decoder == null) decoder = getDecoder();
try {
- // First see whether the value fits the internal pattern ...
+ if (value.length() == 0) {
+ return new BasicName("", "");
+ }
+ if (value.charAt(0) != '{') {
+ // First, see whether the value fits the prefixed name pattern ...
+ Matcher matcher = PREFIXED_NAME_PATTERN.matcher(value);
+ if (matcher.matches()) {
+ String prefix = matcher.group(2);
+ String localName = matcher.group(3);
+ // Decode the parts ...
+ prefix = prefix == null ? "" : decoder.decode(prefix);
+ localName = decoder.decode(localName);
+ // Look for a namespace match ...
+ String namespaceUri =
this.namespaceRegistry.getNamespaceForPrefix(prefix);
+ // Fail if no namespace is found ...
+ if (namespaceUri == null) {
+ throw new
NamespaceException(GraphI18n.noNamespaceRegisteredForPrefix.text(prefix));
+ }
+ return new BasicName(namespaceUri, localName);
+ }
+ }
+ // If it doesn't fit the prefixed pattern, then try the internal pattern
Matcher matcher = FULLY_QUALIFIED_NAME_PATTERN.matcher(value);
if (matcher.matches()) {
String namespaceUri = matcher.group(1);
@@ -99,31 +120,15 @@
localName = decoder.decode(localName);
return new BasicName(namespaceUri, localName);
}
- // Second, see whether the value fits the prefixed name pattern ...
- matcher = PREFIXED_NAME_PATTERN.matcher(value);
- if (matcher.matches()) {
- String prefix = matcher.group(2);
- String localName = matcher.group(3);
- // Decode the parts ...
- prefix = prefix == null ? "" : decoder.decode(prefix);
- localName = decoder.decode(localName);
- // Look for a namespace match ...
- String namespaceUri =
this.namespaceRegistry.getNamespaceForPrefix(prefix);
- // Fail if no namespace is found ...
- if (namespaceUri == null) {
- throw new
NamespaceException(GraphI18n.noNamespaceRegisteredForPrefix.text(prefix));
- }
- return new BasicName(namespaceUri, localName);
- }
} catch (NamespaceException err) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
-
Name.class.getSimpleName(),
- value),
err);
+
Name.class.getSimpleName(),
+ value),
err);
}
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
-
Name.class.getSimpleName(),
-
value));
+
Name.class.getSimpleName(),
+
value));
}
/**
@@ -151,9 +156,10 @@
* {@inheritDoc}
*/
public Name create( int value ) {
- throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Integer.class.getSimpleName(),
-
value));
+ throw new ValueFormatException(value, getPropertyType(),
+
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Integer.class.getSimpleName(),
+ value));
}
/**
@@ -161,17 +167,18 @@
*/
public Name create( long value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Long.class.getSimpleName(),
-
value));
+
Long.class.getSimpleName(),
+
value));
}
/**
* {@inheritDoc}
*/
public Name create( boolean value ) {
- throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Boolean.class.getSimpleName(),
-
value));
+ throw new ValueFormatException(value, getPropertyType(),
+
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Boolean.class.getSimpleName(),
+ value));
}
/**
@@ -179,8 +186,8 @@
*/
public Name create( float value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Float.class.getSimpleName(),
-
value));
+
Float.class.getSimpleName(),
+
value));
}
/**
@@ -188,8 +195,8 @@
*/
public Name create( double value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Double.class.getSimpleName(),
-
value));
+
Double.class.getSimpleName(),
+
value));
}
/**
@@ -198,17 +205,18 @@
public Name create( BigDecimal value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
BigDecimal.class.getSimpleName(),
- value));
+
BigDecimal.class.getSimpleName(),
+ value));
}
/**
* {@inheritDoc}
*/
public Name create( Calendar value ) {
- throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Calendar.class.getSimpleName(),
-
value));
+ throw new ValueFormatException(value, getPropertyType(),
+
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
+
Calendar.class.getSimpleName(),
+ value));
}
/**
@@ -216,8 +224,8 @@
*/
public Name create( Date value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Date.class.getSimpleName(),
-
value));
+
Date.class.getSimpleName(),
+
value));
}
/**
@@ -226,9 +234,10 @@
* @see
org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.DateTime)
*/
public Name create( DateTime value ) throws ValueFormatException {
- throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
DateTime.class.getSimpleName(),
-
value));
+ throw new ValueFormatException(value, getPropertyType(),
+
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
+
DateTime.class.getSimpleName(),
+ value));
}
/**
@@ -248,8 +257,8 @@
return value.getSegment(0).getName();
}
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.errorConvertingType.text(Path.class.getSimpleName(),
-
Name.class.getSimpleName(),
-
value));
+
Name.class.getSimpleName(),
+
value));
}
/**
@@ -258,8 +267,8 @@
public Name create( Reference value ) {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
Reference.class.getSimpleName(),
- value));
+
Reference.class.getSimpleName(),
+ value));
}
/**
@@ -276,8 +285,8 @@
return create(asciiString);
}
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.errorConvertingType.text(URI.class.getSimpleName(),
-
Path.class.getSimpleName(),
-
value));
+
Path.class.getSimpleName(),
+
value));
}
/**
@@ -287,8 +296,8 @@
*/
public Name create( UUID value ) throws IoException {
throw new ValueFormatException(value, getPropertyType(),
GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
-
UUID.class.getSimpleName(),
-
value));
+
UUID.class.getSimpleName(),
+
value));
}
/**