You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2007/01/16 15:59:13 UTC

svn commit: r496719 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/io/FormatableBitSet.java engine/org/apache/derby/iapi/util/ReuseFactory.java testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java

Author: kahatlen
Date: Tue Jan 16 06:59:12 2007
New Revision: 496719

URL: http://svn.apache.org/viewvc?view=rev&rev=496719
Log:
DERBY-2191: Cleanup of FormatableBitSet

Ensure that value is never null and remove redundant checks for
(value == null). Patch contributed by Dyre Tjeldvoll.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/ReuseFactory.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java?view=diff&rev=496719&r1=496718&r2=496719
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/FormatableBitSet.java Tue Jan 16 06:59:12 2007
@@ -22,6 +22,7 @@
 package org.apache.derby.iapi.services.io;
 
 import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.util.ReuseFactory;
 
 import java.io.InputStream;
 import java.io.ObjectOutput;
@@ -67,6 +68,8 @@
 	** zero length byte array, with all bits
 	** marked as unused.
 	*/
+	// value is never null. An empty bitset is represented by a
+	// zero-length array.
 	private byte[]	value;
 	private	short	bitsInLastByte;
 
@@ -77,6 +80,7 @@
 	 */
 	public FormatableBitSet()
 	{
+		value = ReuseFactory.getZeroLenByteArray();
 	}
 
 	/**
@@ -167,11 +171,6 @@
 	 */
 	public int getLengthInBytes()
 	{
-		if (value == null)
-		{
-			return 0;
-		}
-
 		return FormatableBitSet.numBytesFromBits(lengthAsBits);
 	}
 
@@ -219,9 +218,6 @@
 
 	public byte[] getByteArray()
 	{
-		if (value == null)
-			return null;
-
 		// In some cases the array is bigger than the actual number
 		// of valid bytes.
 		int realByteLength = getLengthInBytes();
@@ -239,16 +235,6 @@
 	}
 
 	/**
-	 * Set the value of the byte array
-	 *
-	 * @return	The value of the byte array
-	 */
-	public boolean isNull()
-	{
-		return this.value == null;
-	}
-
-	/**
 	 * Grow (widen) a FormatableBitSet to N bis
 	 *
 	 * @param n	The number of bits you want.  The bits are
@@ -263,12 +249,6 @@
 		if (n <= this.getLength())
 			return;
 
-		if (value == null)
-		{
-			initializeBits(n);
-			return;
-		}
-
 		int delta = n - this.getLength();
 
 
@@ -322,20 +302,6 @@
 		int		numBytes;
 		int		lastByteNum;
 
-		/*
-		** Sanity check: we shouldn't shrink down to
-		** nothing.
-		*/
-		if (SanityManager.DEBUG)
-		{
-			if (value == null)
-			{
-				SanityManager.THROWASSERT("Attempt to shrink a null Bit"+
-						" -- caller should have known better probably");
-				return null;
-			}
-		}
-
 		if (n >= this.getLength())
 		{
 			return this;
@@ -413,18 +379,6 @@
 		byte[]	otherb;
 
 		otherb = other.value;
-		/*
-		** By convention, nulls sort low, and null == null
-		*/
-		if (this.value == null || otherb == null)
-		{
-			if (this.value != null)	// otherb == null
-				return 1;
-			if (otherb != null)		// this.value == null
-				return -1;
-			return 0;				// both null
-		}
-
 		otherLen = other.getLengthInBytes();
 		thisLen = getLengthInBytes();
 		for (otherCount = 0, thisCount = 0;
@@ -581,9 +535,6 @@
      */
     public int hashCode()
     {
-        if( null == value)
-            return 0;
-        
         int code = 0;
         int i;
         int shift = 0;
@@ -710,9 +661,6 @@
 	  */
 	public void clear()
 	{
-		if (value == null) 
-            return;
-
 		int byteLength = getLengthInBytes();
 		for (int ix=0; ix < byteLength; ix++)
             value[ix] = 0;
@@ -861,10 +809,6 @@
 		int		outPosition;
 		int 	inByte;
 
-		if (value == null)
-		{
-			return null;
-		}
 		{
 			// give it a reasonable size
 			StringBuffer str = new StringBuffer(getLength()*8*3);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/ReuseFactory.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/ReuseFactory.java?view=diff&rev=496719&r1=496718&r2=496719
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/ReuseFactory.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/ReuseFactory.java Tue Jan 16 06:59:12 2007
@@ -122,4 +122,10 @@
     {
         return b ? staticTrue : staticFalse;
     }
+
+	private static final byte[] staticZeroLenByteArray = new byte[0];
+	public static byte[] getZeroLenByteArray() 
+	{
+		return staticZeroLenByteArray;
+	}
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java?view=diff&rev=496719&r1=496718&r2=496719
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/FormatableBitSetTest.java Tue Jan 16 06:59:12 2007
@@ -101,7 +101,7 @@
     public void testSetup() {
         assertEquals(0,empty.getLength());
         assertEquals(0,empty.getLengthInBytes());
-        assertEquals(null,empty.getByteArray());
+        assertEquals(0,empty.getByteArray().length);
         assertEquals(0,empty.getNumBitsSet());
 
         assertEquals(18,bitset18.getLength());
@@ -240,16 +240,11 @@
 
     // Test cases for shrink(int)
     public void testShrinkEmpty() {
-        if (SanityManager.DEBUG) {
-            try { empty.shrink(0); fail(); } catch (AssertFailure af) {}
-        }
-        else {
-            empty.shrink(0);
-            assertEquals(0,empty.getLength());
-            assertEquals(0,empty.getLengthInBytes());
-            assertEquals(null,empty.getByteArray());
-            assertEquals(0,empty.getNumBitsSet());
-        }
+        empty.shrink(0);
+        assertEquals(0,empty.getLength());
+        assertEquals(0,empty.getLengthInBytes());
+        assertEquals(0,empty.getByteArray().length);
+        assertEquals(0,empty.getNumBitsSet());
     }
     public void testShrink() {
         bitset18.shrink(9);
@@ -316,14 +311,14 @@
         empty.concatenate(empty);
         assertEquals(0,empty.getLength());
         assertEquals(0,empty.getLengthInBytes());
-        assertEquals(null,empty.getByteArray());
+        assertEquals(0,empty.getByteArray().length);
         assertEquals(0,empty.getNumBitsSet());
     }
     public void testCatAnotherEmpty() {
         empty.concatenate(new FormatableBitSet());
         assertEquals(0,empty.getLength());
         assertEquals(0,empty.getLengthInBytes());
-        assertEquals(null,empty.getByteArray());
+        assertEquals(0,empty.getByteArray().length);
         assertEquals(0,empty.getNumBitsSet());
     }
     public void testCatSame() {
@@ -351,12 +346,12 @@
 
     // Test cases for isSet(int)
     public void testIsSetEmpty() {
-        try { empty.isSet(-1); fail(); } catch (NullPointerException npe) {}
+        empty.isSet(-1);
         if (SanityManager.DEBUG) {
             try { empty.isSet(0); fail(); } catch (AssertFailure af) {}
         }
         else {
-            try { empty.isSet(0); fail(); } catch (NullPointerException e) {}
+            assertFalse(empty.isSet(0));
         }
     }
     public void testIsSet() {
@@ -393,13 +388,14 @@
 
     // Test cases for set(int)
     public void testSetEmpty() {
-        try { empty.set(-1); fail(); } catch (NullPointerException npe) {}
+        try { empty.set(-1); fail(); } 
+        catch (ArrayIndexOutOfBoundsException e) {}
         if (SanityManager.DEBUG) {
             try { empty.set(0); fail(); } catch (AssertFailure af) {}
         }
         else {
             try { empty.set(0); fail(); }
-            catch (NullPointerException npe) {}
+            catch (ArrayIndexOutOfBoundsException e) {}
         }
     }
     public void testSet() {
@@ -421,12 +417,14 @@
 
     // Test cases for clear(int)
     public void testClearEmpty() {
-        try { empty.clear(-1); fail(); } catch (NullPointerException npe) {}
+        try { empty.clear(-1); fail(); } 
+        catch (ArrayIndexOutOfBoundsException e) {}
         if (SanityManager.DEBUG) {
             try { empty.clear(0); fail(); } catch (AssertFailure af) {}
         }
         else {
-            try { empty.clear(0); fail(); } catch (NullPointerException npe) {}
+            try { empty.clear(0); fail(); } 
+            catch (ArrayIndexOutOfBoundsException e) {}
         }
     }
     public void testClear() {
@@ -451,7 +449,8 @@
     // Test cases for anySetBit()
     public void testAnySetBitEmpty() {
         // More reasonable to return -1 here ?
-        try { empty.anySetBit(); fail(); } catch (NullPointerException npe) {}
+        try { empty.anySetBit(); fail(); } 
+        catch (ArrayIndexOutOfBoundsException e) {}
         //assertEquals(empty.anySetBit(),-1);
     }
     public void testAnySetBit() {
@@ -552,7 +551,7 @@
             bitset18.xor(empty);
             assertEquals(0,empty.getLength());
             assertEquals(0,empty.getLengthInBytes());
-            assertEquals(null,empty.getByteArray());
+            assertEquals(0,empty.getByteArray().length);
             assertEquals(0,empty.getNumBitsSet());
         }
         //assertEquals(9,bitset18.getNumBitsSet());