Author: elecharny
Date: Sun Mar 31 23:18:07 2013
New Revision: 1463067

URL: http://svn.apache.org/r1463067
Log:
o Added the Byte and ByteArray comparators
o Added the associated tests

Added:
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteArrayComparatorTest.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteComparatorTest.java
Modified:
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteArrayComparator.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteComparator.java

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteArrayComparator.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteArrayComparator.java?rev=1463067&r1=1463066&r2=1463067&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteArrayComparator.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteArrayComparator.java
 Sun Mar 31 23:18:07 2013
@@ -24,7 +24,7 @@ import java.util.Comparator;
 
 
 /**
- * Compares byte arrays
+ * Compares byte arrays.
  * 
  * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
  */
@@ -46,49 +46,79 @@ public class ByteArrayComparator impleme
 
         if ( byteArray1 == null )
         {
-            throw new IllegalArgumentException( "The first object to compare 
must not be null" );
-        }
-
-        if ( byteArray2 == null )
-        {
-            throw new IllegalArgumentException( "The second object to compare 
must not be null" );
-        }
-
-        if ( byteArray1.length < byteArray2.length )
-        {
-            return -1;
-        }
-
-        if ( byteArray1.length > byteArray2.length )
-        {
-            return 1;
-        }
-
-        for ( int pos = 0; pos < byteArray1.length; pos++ )
-        {
-            int comp = compare( byteArray1[pos], byteArray2[pos] );
-
-            if ( comp != 0 )
+            if ( byteArray2 == null )
             {
-                return comp;
+                return 0;
+            }
+            else
+            {
+                return -1;
             }
         }
-
-        return 0;
-    }
-
-
-    private int compare( byte byte1, byte byte2 )
-    {
-        if ( byte1 < byte2 )
-        {
-            return -1;
-        }
-        if ( byte1 > byte2 )
+        else
         {
-            return 1;
+            if ( byteArray2 == null )
+            {
+                return 1;
+            }
+            else
+            {
+                if ( byteArray1.length < byteArray2.length )
+                {
+                    int pos = 0;
+
+                    for ( byte b1 : byteArray1 )
+                    {
+                        byte b2 = byteArray2[pos];
+
+                        if ( b1 == b2 )
+                        {
+                            pos++;
+                        }
+                        else if ( b1 < b2 )
+                        {
+                            return -1;
+                        }
+                        else
+                        {
+                            return 1;
+                        }
+                    }
+
+                    return -1;
+                }
+                else
+                {
+                    int pos = 0;
+
+                    for ( byte b2 : byteArray2 )
+                    {
+                        byte b1 = byteArray1[pos];
+
+                        if ( b1 == b2 )
+                        {
+                            pos++;
+                        }
+                        else if ( b1 < b2 )
+                        {
+                            return -1;
+                        }
+                        else
+                        {
+                            return 1;
+                        }
+                    }
+
+                    if ( pos < byteArray1.length )
+                    {
+                        return 1;
+                    }
+                    else
+                    {
+                        return 0;
+                    }
+                }
+            }
         }
-
-        return 0;
     }
 }

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteComparator.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteComparator.java?rev=1463067&r1=1463066&r2=1463067&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteComparator.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/comparator/ByteComparator.java
 Sun Mar 31 23:18:07 2013
@@ -46,12 +46,12 @@ public class ByteComparator implements C
 
         if ( byte1 == null )
         {
-            throw new IllegalArgumentException( "The first object to compare 
must not be null" );
+            return -1;
         }
 
         if ( byte2 == null )
         {
-            throw new IllegalArgumentException( "The second object to compare 
must not be null" );
+            return 1;
         }
 
         return byte1.compareTo( byte2 );

Added: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteArrayComparatorTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteArrayComparatorTest.java?rev=1463067&view=auto
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteArrayComparatorTest.java
 (added)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteArrayComparatorTest.java
 Sun Mar 31 23:18:07 2013
@@ -0,0 +1,81 @@
+/*
+ *  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.mavibot.btree.comparator;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the ByteArrayComparator class
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class ByteArrayComparatorTest
+{
+    @Test
+    public void testBteComparator()
+    {
+        ByteArrayComparator comparator = new ByteArrayComparator();
+
+        // Check equality
+        assertEquals( 0, comparator.compare( null, null ) );
+        assertEquals( 0, comparator.compare( new byte[]
+            {}, new byte[]
+            {} ) );
+        assertEquals( 0, comparator.compare( new byte[]
+            { 0x01, 0x02 }, new byte[]
+            { 0x01, 0x02 } ) );
+
+        // The first byte[] is > the second
+        assertEquals( 1, comparator.compare( new byte[]
+            {}, null ) );
+        assertEquals( 1, comparator.compare( new byte[]
+            { 0x01 }, null ) );
+        assertEquals( 1, comparator.compare( new byte[]
+            { 0x01, 0x02 }, new byte[]
+            { 0x01, 0x01 } ) );
+        assertEquals( 1, comparator.compare( new byte[]
+            { 0x01, 0x02, 0x01 }, new byte[]
+            { 0x01, 0x02 } ) );
+        assertEquals( 1, comparator.compare( new byte[]
+            { 0x01, 0x02 }, new byte[]
+            { 0x01, 0x01, 0x02 } ) );
+
+        // The first byte[] is < the second
+        assertEquals( -1, comparator.compare( null, new byte[]
+            {} ) );
+        assertEquals( -1, comparator.compare( null, new byte[]
+            { 0x01, 0x02 } ) );
+        assertEquals( -1, comparator.compare( null, new byte[]
+            { ( byte ) 0xFF, 0x02 } ) );
+        assertEquals( -1, comparator.compare( new byte[]
+            {}, new byte[]
+            { 0x01, 0x02 } ) );
+        assertEquals( -1, comparator.compare( new byte[]
+            {}, new byte[]
+            { ( byte ) 0xFF, 0x02 } ) );
+        assertEquals( -1, comparator.compare( new byte[]
+            { ( byte ) 0xFF, 0x01 }, new byte[]
+            { 0x01, 0x01, 0x02 } ) );
+    }
+}

Added: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteComparatorTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteComparatorTest.java?rev=1463067&view=auto
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteComparatorTest.java
 (added)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/comparator/ByteComparatorTest.java
 Sun Mar 31 23:18:07 2013
@@ -0,0 +1,53 @@
+/*
+ *  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.mavibot.btree.comparator;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the ByteComparator class
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class ByteComparatorTest
+{
+    @Test
+    public void testBteComparator()
+    {
+        ByteComparator comparator = new ByteComparator();
+
+        assertEquals( 0, comparator.compare( null, null ) );
+        assertEquals( 0, comparator.compare( ( byte ) 0x00, ( byte ) 0x00 ) );
+        assertEquals( 0, comparator.compare( ( byte ) 0xFE, ( byte ) 0xFE ) );
+        assertEquals( 1, comparator.compare( ( byte ) 0x01, null ) );
+        assertEquals( 1, comparator.compare( ( byte ) 0x01, ( byte ) 0x00 ) );
+        assertEquals( 1, comparator.compare( ( byte ) 0x00, ( byte ) 0xFF ) );
+        assertEquals( 126, comparator.compare( ( byte ) 0x7F, ( byte ) 0x01 ) 
);
+        assertEquals( -1, comparator.compare( null, ( byte ) 0x00 ) );
+        assertEquals( -1, comparator.compare( null, ( byte ) 0xFF ) );
+        assertEquals( -1, comparator.compare( ( byte ) 0x00, ( byte ) 0x01 ) );
+        assertEquals( -15, comparator.compare( ( byte ) 0xF0, ( byte ) 0xFF ) 
);
+        assertEquals( -2, comparator.compare( ( byte ) 0xFF, ( byte ) 0x01 ) );
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to