Tweaked the Parser.compare() method to try to squeeze out a little better
performance by putting the common cases first.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@540291 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
index 29c8ff3..3ea801b 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
@@ -1219,6 +1219,76 @@
public static boolean compare(Object lhs, String rhs, int operator)
throws EvaluationException
{
+ // Try to optimize the common case of strings by just
+ // checking for them directly.
+ if (lhs instanceof String)
+ {
+ switch (operator)
+ {
+ case EQUAL :
+ return (((String) lhs).compareTo(rhs) == 0);
+ case GREATER_EQUAL :
+ return (((String) lhs).compareTo(rhs) >= 0);
+ case LESS_EQUAL :
+ return (((String) lhs).compareTo(rhs) <= 0);
+ case APPROX:
+ return compareToApprox(((String) lhs), rhs);
+ default:
+ throw new EvaluationException("Unknown comparison operator..."
+ + operator);
+ }
+ }
+ else if (lhs instanceof Comparable)
+ {
+ // Here we know that the LHS is a comparable object, so
+ // try to create an object for the RHS by using a constructor
+ // that will take the RHS string as a parameter.
+ Comparable rhsComparable = null;
+ try
+ {
+ // We are expecting to be able to construct a comparable
+ // instance from the RHS string by passing it into the
+ // constructor of the corresponing comparable class. The
+ // Character class is a special case, since its constructor
+ // does not take a string, so handle it separately.
+ if (lhs instanceof Character)
+ {
+ rhsComparable = new Character(rhs.charAt(0));
+ }
+ else
+ {
+ rhsComparable = (Comparable) lhs.getClass()
+ .getConstructor(new Class[] { String.class })
+ .newInstance(new Object[] { rhs });
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new EvaluationException(
+ "Could not instantiate class "
+ + lhs.getClass().getName()
+ + " with constructor String parameter "
+ + rhs + " " + ex);
+ }
+
+ Comparable lhsComparable = (Comparable) lhs;
+
+ switch (operator)
+ {
+ case EQUAL :
+ return (lhsComparable.compareTo(rhsComparable) == 0);
+ case GREATER_EQUAL :
+ return (lhsComparable.compareTo(rhsComparable) >= 0);
+ case LESS_EQUAL :
+ return (lhsComparable.compareTo(rhsComparable) <= 0);
+ case APPROX:
+ return compareToApprox(lhsComparable, rhsComparable);
+ default:
+ throw new EvaluationException("Unknown comparison operator..."
+ + operator);
+ }
+ }
+
// Determine class of LHS.
Class lhsClass = null;
@@ -1289,53 +1359,6 @@
return false;
}
- // Here we know that the LHS is a comparable object, so
- // try to create an object for the RHS by using a constructor
- // that will take the RHS string as a parameter.
- Comparable rhsComparable = null;
- try
- {
- // We are expecting to be able to construct a comparable
- // instance from the RHS string by passing it into the
- // constructor of the corresponing comparable class. The
- // Character class is a special case, since its constructor
- // does not take a string, so handle it separately.
- if (lhsClass == Character.class)
- {
- rhsComparable = new Character(rhs.charAt(0));
- }
- else
- {
- rhsComparable = (Comparable) lhsClass
- .getConstructor(new Class[] { String.class })
- .newInstance(new Object[] { rhs });
- }
- }
- catch (Exception ex)
- {
- throw new EvaluationException(
- "Could not instantiate class "
- + lhsClass.getName()
- + " with constructor String parameter "
- + rhs + " " + ex);
- }
-
- Comparable lhsComparable = (Comparable) lhs;
-
- switch (operator)
- {
- case EQUAL :
- return (lhsComparable.compareTo(rhsComparable) == 0);
- case GREATER_EQUAL :
- return (lhsComparable.compareTo(rhsComparable) >= 0);
- case LESS_EQUAL :
- return (lhsComparable.compareTo(rhsComparable) <= 0);
- case APPROX:
- return compareToApprox(lhsComparable, rhsComparable);
- default:
- throw new EvaluationException("Unknown comparison operator..."
- + operator);
- }
}
return false;
@@ -1694,4 +1717,4 @@
throw new EvaluationException(
opStr + ": unsupported type " + clazz.getName(), clazz);
}
-}
\ No newline at end of file
+}