org.apache.derby.iapi.services.io
Class FormatableBitSet

java.lang.Object
  extended byorg.apache.derby.iapi.services.io.FormatableBitSet
All Implemented Interfaces:
java.lang.Cloneable, java.io.Externalizable, Formatable, java.io.Serializable, TypedFormat

public final class FormatableBitSet
extends java.lang.Object
implements Formatable, java.lang.Cloneable

FormatableBitSet is implemented as a packed array of bytes.

Author:
Jamie -- originally coded by Jeff
See Also:
Serialized Form

Field Summary
private  short bitsInLastByte
           
private static char[] decodeArray
           
private  int lengthAsBits
           
private  byte[] value
          Bits are stored as an array of bytes.
 
Constructor Summary
FormatableBitSet()
          Niladic Constructor
FormatableBitSet(byte[] newValue)
          Constructs a Bit from an array of bytes.
FormatableBitSet(byte[] newValue, int numBits)
          Constructs a Bit from an array of bytes.
FormatableBitSet(FormatableBitSet original)
          Copy constructor
FormatableBitSet(int numBits)
          Constructs a Bit with the initial number of bits
 
Method Summary
 void and(FormatableBitSet otherBit)
          Bitwise AND this Bit with another Bit.
 int anySetBit()
          If any bit is set, return the bit number of a bit that is set.
 int anySetBit(int beyondBit)
          Like anySetBit(), but return any set bit whose number is bigger than beyondBit.
private  int calculateLength(int realByteLength)
           
 void clear()
          Clear all the bits in this FormatableBitSet
 void clear(int position)
          Bit clear
 java.lang.Object clone()
           
 int compare(FormatableBitSet other)
          Bit comparison.
 FormatableBitSet concatenate(FormatableBitSet other)
          Bit concatenation.
 boolean equals(FormatableBitSet other)
          Bit equivalence.
 boolean get(int position)
          Bit get -- alias for isSet()
 byte[] getByteArray()
          Get the value of the byte array
 int getLength()
          Get the length in bits
 int getLengthInBytes()
          Get the length in bytes of a Bit value
 int getNumBitsSet()
          Get a count of the number of bits that are set.
 int getTypeFormatId()
          Get the formatID which corresponds to this class.
 void grow(int n)
          Grow (widen) a FormatableBitSet to N bis
 int hashCode()
          Produce a hash code by putting the value bytes into an int, exclusive OR'ing if there are more than 4 bytes.
private static byte hexCharToByte(char hexChar)
          Translate a hex character to a byte.
private  void initializeBits(int numBits)
           
 boolean isNull()
          Set the value of the byte array
 boolean isSet(int position)
          Bit isSet
static int maxBitsForSpace(int numBytes)
          Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized.
private static short numBitsInLastByte(int bits)
          Figure out how many bits are in the last byte from the total number of bits.
protected static int numBytesFromBits(int bits)
          Figure out how many bytes are needed to store the input number of bits.
 void or(FormatableBitSet otherBit)
          Bitwise OR this Bit with another Bit.
 void readExternal(java.io.ObjectInput in)
          Note: gracefully handles zero length bits -- will create a zero length array with no bits being used.
 void readExternalFromArray(ArrayInputStream in)
           
 void set(int position)
          Bit set
 FormatableBitSet shrink(int n)
          Shrink (narrow) a FormatableBitSet to N bits
 int size()
          Get the length in bits -- alias for getLength()
 java.lang.String toString()
          Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.
 void writeExternal(java.io.ObjectOutput out)
          Format: int length in bits byte[]
 void xor(FormatableBitSet set)
          Logically XORs this FormatableBitSet with the specified FormatableBitSet.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

private byte[] value
Bits are stored as an array of bytes. Bits are numbered starting at 0. Bits 0..7 go in byte[0], 8..15 in byte[1] and so on. The number of bytes is tracked as part of the byte array. The number of bits being used is derived by the number of bytes being used and the number of bits being used by the last byte. The partially unused byte is always byte[byte.length] with the lowest bits being unused. Zero length bits are stored using a zero length byte array, with all bits marked as unused.


bitsInLastByte

private short bitsInLastByte

lengthAsBits

private transient int lengthAsBits

decodeArray

private static char[] decodeArray
Constructor Detail

FormatableBitSet

public FormatableBitSet()
Niladic Constructor


FormatableBitSet

public FormatableBitSet(int numBits)
Constructs a Bit with the initial number of bits


FormatableBitSet

public FormatableBitSet(byte[] newValue)
Constructs a Bit from an array of bytes. Assume bytes are all being used.

Parameters:
newValue - The array of bytes to make up the new Bit

FormatableBitSet

public FormatableBitSet(byte[] newValue,
                        int numBits)
Constructs a Bit from an array of bytes.

Parameters:
newValue - The array of bytes to make up the new Bit
numBits - The number of bits

FormatableBitSet

public FormatableBitSet(FormatableBitSet original)
Copy constructor

Parameters:
original - the FormatableBitSet to make a copy from
Method Detail

initializeBits

private void initializeBits(int numBits)

clone

public java.lang.Object clone()

getLengthInBytes

public int getLengthInBytes()
Get the length in bytes of a Bit value

Returns:
The length in bytes of this value

getLength

public int getLength()
Get the length in bits

Returns:
The length in bits for this value NOTE: could possibly be changed to a long. As is we are restricted to 2^(31-3) -> 256meg instead of 2^31 (Integer.MAX_VALUE) like other datatypes (or 2 gig). If it is ever changed to a long be sure to change read/writeExternal which write out the length in bits.

calculateLength

private int calculateLength(int realByteLength)

size

public int size()
Get the length in bits -- alias for getLength()

Returns:
The length in bits for this value

getByteArray

public byte[] getByteArray()
Get the value of the byte array

Returns:
The value of the byte array

isNull

public boolean isNull()
Set the value of the byte array

Returns:
The value of the byte array

grow

public void grow(int n)
Grow (widen) a FormatableBitSet to N bis

Parameters:
n - The number of bits you want. The bits are always added as 0 and are appended to the least significant end of the bit array. ASSUMPTIONS: that all extra bits in the last byte are zero.

shrink

public FormatableBitSet shrink(int n)
Shrink (narrow) a FormatableBitSet to N bits

Parameters:
n - The number of bits the caller wants. The bits are always removed from the least significant end of the bit array.

equals

public boolean equals(FormatableBitSet other)
Bit equivalence. Compare this with other. If the length is different, then cannot be equal so short circuit. Otherwise, rely on compare(). Note that two zero length bits are considered equal.

Parameters:
other - the other bit to compare to
Returns:
TRUE|FALSE

compare

public int compare(FormatableBitSet other)
Bit comparison. Compare this with other. Will always do a byte by byte compare. Given 2 similar bits of unequal lengths (x and y), where x.getLength() < y.getLength() but where: x[0..x.getLength()] == y[0..x.getLength()] then x < y.

Parameters:
other - the other bit to compare to
Returns:
-1 - if other < this 0 - if other == this 1 - if other > this

concatenate

public FormatableBitSet concatenate(FormatableBitSet other)
Bit concatenation.

Parameters:
other - the other bit to append to this
Returns:
Bit -- the newly concatenated bit

hashCode

public int hashCode()
Produce a hash code by putting the value bytes into an int, exclusive OR'ing if there are more than 4 bytes.

Returns:
the hash code

isSet

public final boolean isSet(int position)
Bit isSet

Parameters:
position - the bit to check

get

public final boolean get(int position)
Bit get -- alias for isSet()

Parameters:
position - the bit to check

set

public void set(int position)
Bit set

Parameters:
position - the bit to set

clear

public void clear(int position)
Bit clear

Parameters:
position - the bit to clear

clear

public void clear()
Clear all the bits in this FormatableBitSet


numBytesFromBits

protected static int numBytesFromBits(int bits)
Figure out how many bytes are needed to store the input number of bits.

Parameters:
bits - bits
Returns:
the number of bytes

numBitsInLastByte

private static short numBitsInLastByte(int bits)
Figure out how many bits are in the last byte from the total number of bits.

Parameters:
bits - bits
Returns:
the number of bits

hexCharToByte

private static byte hexCharToByte(char hexChar)
Translate a hex character to a byte.

Parameters:
hexChar - A character with the value [0-9a-fA-F].
Returns:
A byte with the numeric value corresponding to the hex character

toString

public java.lang.String toString()
Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.

Returns:
A new String containing the formatted Bit value

maxBitsForSpace

public static int maxBitsForSpace(int numBytes)
Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized. Only valid for this implementation of Bit.


anySetBit

public int anySetBit()
If any bit is set, return the bit number of a bit that is set. If no bit is set, return -1;

Returns:
the bit number of a bit that is set, or -1 if no bit is set

anySetBit

public int anySetBit(int beyondBit)
Like anySetBit(), but return any set bit whose number is bigger than beyondBit. If no bit is set after beyondBit, -1 is returned. By using anySetBit() and anySetBit(beyondBit), one can quickly go thru the entire bit array to return all set bit.

Parameters:
beyondBit - only look at bit that is greater than this bit number
Returns:
the bit number of a bit that is set, or -1 if no bit after beyondBit is set

or

public void or(FormatableBitSet otherBit)
Bitwise OR this Bit with another Bit.

Parameters:
otherBit - the other Bit
See Also:
Bit#or

and

public void and(FormatableBitSet otherBit)
Bitwise AND this Bit with another Bit.

Parameters:
otherBit - the other Bit
See Also:
Bit#or

xor

public void xor(FormatableBitSet set)
Logically XORs this FormatableBitSet with the specified FormatableBitSet.

Parameters:
set - The FormatableBitSet to be XORed with.

getNumBitsSet

public int getNumBitsSet()
Get a count of the number of bits that are set.

Returns:
The number of bits that are set.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Format:

Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException
See Also:
Externalizable.writeExternal(java.io.ObjectOutput)

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException
Note: gracefully handles zero length bits -- will create a zero length array with no bits being used. Fortunately in.read() is ok with a zero length array so no special code.

WARNING: this method cannot be changed w/o changing SQLBit because SQLBit calls this directly w/o calling read/writeObject(), so the format id is not stored in that case.

Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
See Also:
Externalizable.readExternal(java.io.ObjectInput)

readExternalFromArray

public void readExternalFromArray(ArrayInputStream in)
                           throws java.io.IOException
Throws:
java.io.IOException

getTypeFormatId

public int getTypeFormatId()
Get the formatID which corresponds to this class.

Specified by:
getTypeFormatId in interface TypedFormat
Returns:
the formatID of this class


Apache Derby V10.0 Engine Documentation - Copyright © 1997,2004 The Apache Software Foundation or its licensors, as applicable.