directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r949045 - in /directory/shared/trunk: ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/ ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/ ldap/src/test/java/org/apache/directory/shared/ldap/sche...
Date Thu, 27 May 2010 23:57:43 GMT
Author: elecharny
Date: Thu May 27 23:57:43 2010
New Revision: 949045

URL: http://svn.apache.org/viewvc?rev=949045&view=rev
Log:
Added the BitString comparator

Added:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparator.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparatorTest.java
Modified:
    directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.16.ldif
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/IntegerComparator.java

Modified: directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.16.ldif
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-schema/src/main/resources/schema/ou%3Dschema/cn%3Dsystem/ou%3Dcomparators/m-oid%3D2.5.13.16.ldif?rev=949045&r1=949044&r2=949045&view=diff
==============================================================================
--- directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.16.ldif
(original)
+++ directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.16.ldif
Thu May 27 23:57:43 2010
@@ -3,8 +3,7 @@ dn: m-oid=2.5.13.16,ou=comparators,cn=sy
 createtimestamp: 20090818022728Z
 m-oid: 2.5.13.16
 entrycsn: 20090818052728.039000Z#000000#000#000000
-m-fqcn: org.apache.directory.shared.ldap.schema.comparators.ComparableComparator
- 
+m-fqcn: org.apache.directory.shared.ldap.schema.comparators.BitStringComparator
 objectclass: metaComparator
 objectclass: metaTop
 objectclass: top

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparator.java?rev=949045&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparator.java
(added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparator.java
Thu May 27 23:57:43 2010
@@ -0,0 +1,131 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.comparators;
+
+
+import org.apache.directory.shared.ldap.schema.LdapComparator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A class for the bitStringMatch matchingRule (RFC 4517, par. 4.2.1)
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 437007 $
+ */
+public class BitStringComparator extends LdapComparator<String>
+{
+    /** A logger for this class */
+    private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class
);
+
+    /** The serialVersionUID */
+    private static final long serialVersionUID = 1L;
+
+
+    /**
+     * The BitStringComparator constructor. Its OID is the IntegerOrderingMatch matching
+     * rule OID.
+     */
+    public BitStringComparator( String oid )
+    {
+        super( oid );
+    }
+
+
+    /**
+     * Implementation of the Compare method
+     */
+    public int compare( String bs1, String bs2 )
+    {
+        LOG.debug( "comparing BitString objects '{}' with '{}'", bs1, bs2 );
+
+        // First, shortcut the process by comparing
+        // references. If they are equals, then bs1 and bs2
+        // reference the same object
+        if ( bs1 == bs2 )
+        {
+            return 0;
+        }
+
+        // Then, deal with one of bs1 or bs2 being null
+        // Both can't be null, because then they would 
+        // have been caught by the previous test
+        if ( ( bs1 == null ) || ( bs2 == null ) )
+        {
+            return ( bs1 == null ? -1 : 1 );
+        }
+
+        // We have to get rid of 0 from left of each BitString
+        char[] array1 = bs1.toCharArray();
+        char[] array2 = bs2.toCharArray();
+
+        int pos1 = bs1.indexOf( '1' );
+        int pos2 = bs2.indexOf( '1' );
+
+        if ( pos1 == -1 )
+        {
+            if ( pos2 == -1 )
+            {
+                return 0;
+            }
+            else
+            {
+                return -1;
+            }
+        }
+        else if ( pos2 == -1 )
+        {
+            return 1;
+        }
+
+        int length1 = array1.length - pos1;
+        int length2 = array2.length - pos2;
+
+        if ( length1 == length2 )
+        {
+            for ( int i = 0; i < length1; i++ )
+            {
+                int i1 = i + pos1;
+                int i2 = i + pos2;
+
+                if ( array1[i1] < array2[i2] )
+                {
+                    return -1;
+                }
+                else if ( array1[i1] > array2[i2] )
+                {
+                    return 1;
+                }
+            }
+
+            return 0;
+        }
+
+        if ( length1 < length2 )
+        {
+            return -1;
+        }
+        else
+        {
+            return 1;
+        }
+    }
+}

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/IntegerComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/IntegerComparator.java?rev=949045&r1=949044&r2=949045&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/IntegerComparator.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/IntegerComparator.java
Thu May 27 23:57:43 2010
@@ -76,7 +76,7 @@ public class IntegerComparator extends L
      */
     private int compare( Long backendValue, Long assertValue )
     {
-        LOG.debug( "comparing IntegerOrdering objects '{}' with '{}'", backendValue, assertValue
);
+        LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue
);
 
         // First, shortcut the process by comparing
         // references. If they are equals, then o1 and o2
@@ -94,32 +94,6 @@ public class IntegerComparator extends L
             return ( backendValue == null ? -1 : 1 );
         }
 
-        // Both objects must be stored as String for numeric.
-        // But we need to normalize the values first.
-        /*
-        try
-        {
-            backendValue = PrepareString.normalize( ( String ) backendValue, PrepareString.StringType.NUMERIC_STRING
);
-        }
-        catch ( IOException e )
-        {
-            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue )
);
-        }
-        try
-        {
-            assertValue = PrepareString.normalize( ( String ) assertValue, PrepareString.StringType.NUMERIC_STRING
);
-        }
-        catch ( IOException e )
-        {
-            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
-        }
-
-        BigInteger b1 = new BigInteger( ( String ) backendValue );
-        BigInteger b2 = new BigInteger( ( String ) assertValue );
-        */
-
-        //return b1.compareTo( b2 );
-
         return backendValue.compareTo( assertValue );
     }
 
@@ -129,7 +103,7 @@ public class IntegerComparator extends L
      */
     private int compare( String backendValue, String assertValue )
     {
-        LOG.debug( "comparing IntegerOrdering objects '{}' with '{}'", backendValue, assertValue
);
+        LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue
);
 
         // First, shortcut the process by comparing
         // references. If they are equals, then o1 and o2

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparatorTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparatorTest.java?rev=949045&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparatorTest.java
(added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/BitStringComparatorTest.java
Thu May 27 23:57:43 2010
@@ -0,0 +1,76 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.comparators;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * Test the BitString comparator
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BitStringComparatorTest
+{
+    private BitStringComparator comparator;
+
+
+    @Before
+    public void init()
+    {
+        comparator = new BitStringComparator( null );
+    }
+
+
+    @Test
+    public void testNullBitString()
+    {
+        assertEquals( 0, comparator.compare( null, null ) );
+        assertEquals( -1, comparator.compare( null, "0101B" ) );
+        assertEquals( -1, comparator.compare( null, "000B" ) );
+        assertEquals( 1, comparator.compare( "0101B", null ) );
+        assertEquals( 1, comparator.compare( "111B", null ) );
+    }
+
+
+    @Test
+    public void testBitStringsEquals()
+    {
+        assertEquals( 0, comparator.compare( "0B", "0B" ) );
+        assertEquals( 0, comparator.compare( "1B", "1B" ) );
+        assertEquals( 0, comparator.compare( "101010B", "101010B" ) );
+        assertEquals( 0, comparator.compare( "00000101010B", "00101010B" ) );
+    }
+
+
+    @Test
+    public void testBitStringsNotEquals()
+    {
+        assertEquals( -1, comparator.compare( "0B", "1B" ) );
+        assertEquals( 1, comparator.compare( "1B", "0B" ) );
+        assertEquals( 1, comparator.compare( "101110B", "101010B" ) );
+        assertEquals( -1, comparator.compare( "00000101010B", "00111010B" ) );
+    }
+}



Mime
View raw message