[FELIX-4821] Reuse the fast case insensitive comparator for the CapabilitySet indices
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1668536 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java b/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
index a64b4ab..2e29b53 100644
--- a/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
+++ b/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
@@ -81,7 +81,7 @@
m_indices = (caseSensitive)
? new TreeMap<String, Map<Object, Set<BundleCapability>>>()
: new TreeMap<String, Map<Object, Set<BundleCapability>>>(
- new StringComparator(false));
+ StringComparator.COMPARATOR);
for (int i = 0; (indexProps != null) && (i < indexProps.size()); i++)
{
m_indices.put(
diff --git a/framework/src/main/java/org/apache/felix/framework/util/StringComparator.java b/framework/src/main/java/org/apache/felix/framework/util/StringComparator.java
index 03af305..fb2f6e1 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/StringComparator.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/StringComparator.java
@@ -20,29 +20,54 @@
import java.util.Comparator;
-public class StringComparator implements Comparator
+public class StringComparator implements Comparator<String>
{
- private final boolean m_isCaseSensitive;
- public StringComparator(boolean b)
+ public static final StringComparator COMPARATOR = new StringComparator();
+
+ public int compare(String s1, String s2)
{
- m_isCaseSensitive = b;
+ int n1 = s1.length();
+ int n2 = s2.length();
+ int min = n1 < n2 ? n1 : n2;
+ for ( int i = 0; i < min; i++ )
+ {
+ char c1 = s1.charAt( i );
+ char c2 = s2.charAt( i );
+ if ( c1 != c2 )
+ {
+ // Fast check for simple ascii codes
+ if ( c1 <= 128 && c2 <= 128 )
+ {
+ c1 = toLowerCaseFast(c1);
+ c2 = toLowerCaseFast(c2);
+ if ( c1 != c2 )
+ {
+ return c1 - c2;
+ }
+ }
+ else
+ {
+ c1 = Character.toUpperCase( c1 );
+ c2 = Character.toUpperCase( c2 );
+ if ( c1 != c2 )
+ {
+ c1 = Character.toLowerCase( c1 );
+ c2 = Character.toLowerCase( c2 );
+ if ( c1 != c2 )
+ {
+ // No overflow because of numeric promotion
+ return c1 - c2;
+ }
+ }
+ }
+ }
+ }
+ return n1 - n2;
}
- public int compare(Object o1, Object o2)
+ private static char toLowerCaseFast( char ch )
{
- if (m_isCaseSensitive)
- {
- return o1.toString().compareTo(o2.toString());
- }
- else
- {
- return o1.toString().compareToIgnoreCase(o2.toString());
- }
- }
-
- public boolean isCaseSensitive()
- {
- return m_isCaseSensitive;
+ return ( ch >= 'A' && ch <= 'Z' ) ? ( char ) ( ch + 'a' - 'A' ) : ch;
}
}
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/util/StringMap.java b/framework/src/main/java/org/apache/felix/framework/util/StringMap.java
index 6cb0469..4543514 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/StringMap.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/StringMap.java
@@ -18,7 +18,6 @@
*/
package org.apache.felix.framework.util;
-import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
@@ -32,70 +31,19 @@
**/
public class StringMap extends TreeMap<String, Object>
{
- private static final CaseInsensitiveComparator COMPARATOR = new CaseInsensitiveComparator();
public StringMap()
{
- super(COMPARATOR);
+ super(StringComparator.COMPARATOR);
}
public StringMap(Map<?, ?> map)
{
- super(COMPARATOR);
+ this();
for (Map.Entry<?, ?> e : map.entrySet())
{
put(e.getKey().toString(), e.getValue());
}
}
- private static class CaseInsensitiveComparator implements Comparator<String>
- {
-
- public int compare(String s1, String s2)
- {
- int n1 = s1.length();
- int n2 = s2.length();
- int min = n1 < n2 ? n1 : n2;
- for ( int i = 0; i < min; i++ )
- {
- char c1 = s1.charAt( i );
- char c2 = s2.charAt( i );
- if ( c1 != c2 )
- {
- // Fast check for simple ascii codes
- if ( c1 <= 128 && c2 <= 128 )
- {
- c1 = toLowerCaseFast(c1);
- c2 = toLowerCaseFast(c2);
- if ( c1 != c2 )
- {
- return c1 - c2;
- }
- }
- else
- {
- c1 = Character.toUpperCase( c1 );
- c2 = Character.toUpperCase( c2 );
- if ( c1 != c2 )
- {
- c1 = Character.toLowerCase( c1 );
- c2 = Character.toLowerCase( c2 );
- if ( c1 != c2 )
- {
- // No overflow because of numeric promotion
- return c1 - c2;
- }
- }
- }
- }
- }
- return n1 - n2;
- }
- }
-
- private static char toLowerCaseFast( char ch )
- {
- return ( ch >= 'A' && ch <= 'Z' ) ? ( char ) ( ch + 'a' - 'A' ) : ch;
- }
-
}