Top Description Fields Constructors Methods
org.apache.derby.iapi.services.io

public final Class FormatableBitSet

extends Object
implements Formatable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, org.apache.derby.iapi.services.io.Formatable, org.apache.derby.iapi.services.io.TypedFormat, java.io.Externalizable, java.io.Serializable
Imports
org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.shared.common.util.ArrayUtil, java.io.ObjectOutput, .ObjectInput, .IOException

FormatableBitSet is implemented as a packed array of bytes.

Field Summary

Modifier and TypeField and Description
private byte
private transient int
private byte[]
value

Bits are stored as an array of bytes.

Constructor Summary

AccessConstructor and Description
public
FormatableBitSet()

Niladic Constructor

public
FormatableBitSet(int
The number of bits in the set
numBits
)

Constructs a Bit with the initial number of bits

public
FormatableBitSet(byte[]
The array of bytes to make up the new Bit
newValue
)

Constructs a Bit from an array of bytes.

public
FormatableBitSet(FormatableBitSet
the FormatableBitSet to make a copy from
original
)

Copy constructor

Method Summary

Modifier and TypeMethod and Description
public void
and(FormatableBitSet
bitset operand
otherBit
)

Bitwise AND this FormatableBitSet with another FormatableBitSet.

public int

Returns:

the zero-based index of the first bit that is set, or -1 if no bit is set
anySetBit
()

If any bit is set, return the zero-based bit index of the first bit that is set.

public int

Returns:

the bit number of a bit that is set, or -1 if no bit after beyondBit is set
anySetBit
(int
Only look at bit that is greater than this bit number. Supplying a value of -1 makes the call equivalent to anySetBit().
beyondBit
)

Like anySetBit(), but return any set bit whose number is bigger than beyondBit.

private int
calculateLength(int realByteLength)

private final void
public void
clear(int
the bit to clear
position
)

Bit clear

public void
clear()

Clear all the bits in this FormatableBitSet

public Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public int

Returns:

-1 - if other < this 0 - if other == this 1 - if other > this
compare
(FormatableBitSet
the other bit to compare to
other
)

Bit comparison.

public void
copyFrom(FormatableBitSet
Bit set to copy
that
)

Copy the bytes from another FormatableBitSet.

public boolean

Returns:

TRUE|FALSE
equals
(Object
the other bit to compare to
other
)

Overrides java.lang.Object.equals.

Bit equivalence.

private static byte

Returns:

the zero-based index of the first set bit in the argument byte
firstSet
(byte
a non-zero byte to check for set bits
v
)

A utility method which treats the byte argument as an 8-bit bitset and finds the first set bit in that byte.

public final boolean

Returns:

true if the bit is set
get
(int
the bit to check
position
)

Bit get -- alias for isSet()

public byte[]

Returns:

The value of the byte array
getByteArray
()

Get the value of the byte array

public int

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.
getLength
()

Get the length in bits

public int

Returns:

The length in bytes of this value
getLengthInBytes
()

Get the length in bytes of a Bit value

public int

Returns:

The number of bits that are set.
getNumBitsSet
()

Get a count of the number of bits that are set.

public int

Returns:

the formatID of this class
getTypeFormatId
()

Implements org.apache.derby.iapi.services.io.TypedFormat.getTypeFormatId.

Get the formatID which corresponds to this class.

public void
grow(int
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.
n
)

Grow (widen) a FormatableBitSet so that it contains at least N bits.

public int

Returns:

the hash code
hashCode
()

Overrides java.lang.Object.hashCode.

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

private void
initializeBits(int numBits)

public boolean

Returns:

true if invariant holds, false otherwise
invariantHolds
()

This method returns true if the following conditions hold: 1.

public final boolean

Returns:

true if the bit is set
isSet
(int
the bit to check
position
)

Bit isSet

public static int

Returns:

the corresponding bit count
maxBitsForSpace
(int
Number of bits in given number of bytes
numBytes
)

Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized.

private static byte

Returns:

the number of bits
numBitsInLastByte
(int
bits
bits
)

Figure out how many bits are in the last byte from the total number of bits.

private static int

Returns:

the number of bytes
numBytesFromBits
(int
bits
bits
)

Figure out how many bytes are needed to store the input number of bits.

public void
or(FormatableBitSet
bitset operand
otherBit
)

Bitwise OR this FormatableBitSet with another FormatableBitSet.

public void
readExternal(ObjectInput
the stream to read data from in order to restore the object
in
)

Implements java.io.Externalizable.readExternal.

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.

public void
set(int
the bit to set
position
)

Bit set

public void
shrink(int
The number of bits the caller wants. The bits are always removed from the least significant end of the bit array.
n
)

Shrink (narrow) a FormatableBitSet to N bits.

public int

Returns:

The length in bits for this value
size
()

Get the length in bits -- alias for getLength()

public String

Returns:

A new String containing the formatted Bit value
toString
()

Overrides java.lang.Object.toString.

Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.

private static int
udiv8(int i)

private static byte
umod8(int i)

private static int
umul8(int i)

public void
writeExternal(ObjectOutput
the stream to write the object to
out
)

Implements java.io.Externalizable.writeExternal.

Format:

  • int length in bits
  • byte[]
public void
xor(FormatableBitSet
bitset operand
otherBit
)

Bitwise XOR this FormatableBitSet with another FormatableBitSet.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

bitsInLastByteback to summary
private byte bitsInLastByte
lengthAsBitsback to summary
private transient int lengthAsBits
valueback to summary
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.

Constructor Detail

FormatableBitSetback to summary
public FormatableBitSet()

Niladic Constructor

FormatableBitSetback to summary
public FormatableBitSet(int numBits)

Constructs a Bit with the initial number of bits

Parameters
numBits:int

The number of bits in the set

FormatableBitSetback to summary
public FormatableBitSet(byte[] newValue)

Constructs a Bit from an array of bytes. Assume bytes are all being used.

Parameters
newValue:byte[]

The array of bytes to make up the new Bit

FormatableBitSetback to summary
public FormatableBitSet(FormatableBitSet original)

Copy constructor

Parameters
original:FormatableBitSet

the FormatableBitSet to make a copy from

Method Detail

andback to summary
public void and(FormatableBitSet otherBit)

Bitwise AND this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. clearing this bitset). A bitset that is smaller than its operand is expanded to the same size.

Parameters
otherBit:FormatableBitSet

bitset operand

anySetBitback to summary
public int anySetBit()

If any bit is set, return the zero-based bit index of the first bit that is set. If no bit is set, return -1. By using anySetBit() and anySetBit(beyondBit), one can quickly go thru the entire bit array to return all set bit.

Returns:int

the zero-based index of the first bit that is set, or -1 if no bit is set

anySetBitback to summary
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:int

Only look at bit that is greater than this bit number. Supplying a value of -1 makes the call equivalent to anySetBit().

Returns:int

the bit number of a bit that is set, or -1 if no bit after beyondBit is set

calculateLengthback to summary
private int calculateLength(int realByteLength)
checkPositionback to summary
private final void checkPosition(int p)
clearback to summary
public void clear(int position)

Bit clear

Parameters
position:int

the bit to clear

clearback to summary
public void clear()

Clear all the bits in this FormatableBitSet

cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:Object

a clone of this instance.

compareback to summary
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:FormatableBitSet

the other bit to compare to

Returns:int

-1 - if other < this 0 - if other == this 1 - if other > this

copyFromback to summary
public void copyFrom(FormatableBitSet that)

Copy the bytes from another FormatableBitSet. Assumes that this bit set is at least as large as the argument's bit set.

Parameters
that:FormatableBitSet

Bit set to copy

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

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:Object

the other bit to compare to

Returns:boolean

TRUE|FALSE

firstSetback to summary
private static byte firstSet(byte v)

A utility method which treats the byte argument as an 8-bit bitset and finds the first set bit in that byte. Assumes that at least one bit in v is set (v!=0).

Parameters
v:byte

a non-zero byte to check for set bits

Returns:byte

the zero-based index of the first set bit in the argument byte

getback to summary
public final boolean get(int position)

Bit get -- alias for isSet()

Parameters
position:int

the bit to check

Returns:boolean

true if the bit is set

getByteArrayback to summary
public byte[] getByteArray()

Get the value of the byte array

Returns:byte[]

The value of the byte array

getLengthback to summary
public int getLength()

Get the length in bits

Returns:int

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.

getLengthInBytesback to summary
public int getLengthInBytes()

Get the length in bytes of a Bit value

Returns:int

The length in bytes of this value

getNumBitsSetback to summary
public int getNumBitsSet()

Get a count of the number of bits that are set.

Returns:int

The number of bits that are set.

getTypeFormatIdback to summary
public int getTypeFormatId()

Implements org.apache.derby.iapi.services.io.TypedFormat.getTypeFormatId.

Get the formatID which corresponds to this class.

Returns:int

the formatID of this class

growback to summary
public void grow(int n)

Grow (widen) a FormatableBitSet so that it contains at least N bits. If the bitset already has more than n bits, this is a noop. Negative values of n are not allowed. ASSUMPTIONS: that all extra bits in the last byte are zero.

Parameters
n:int

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.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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

Returns:int

the hash code

initializeBitsback to summary
private void initializeBits(int numBits)
invariantHoldsback to summary
public boolean invariantHolds()

This method returns true if the following conditions hold: 1. The number of bits in the bitset will fit into the allocated byte array. 2. 'lengthAsBits' and 'bitsInLastByte' are consistent. 3. All unused bits in the byte array are unset. This represents an invariant for the class, so this method should always return true. The method is public, but is primarily intended for testing and ASSERTS.

Returns:boolean

true if invariant holds, false otherwise

isSetback to summary
public final boolean isSet(int position)

Bit isSet

Parameters
position:int

the bit to check

Returns:boolean

true if the bit is set

maxBitsForSpaceback to summary
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.

Parameters
numBytes:int

Number of bits in given number of bytes

Returns:int

the corresponding bit count

numBitsInLastByteback to summary
private static byte numBitsInLastByte(int bits)

Figure out how many bits are in the last byte from the total number of bits.

Parameters
bits:int

bits

Returns:byte

the number of bits

numBytesFromBitsback to summary
private static int numBytesFromBits(int bits)

Figure out how many bytes are needed to store the input number of bits.

Parameters
bits:int

bits

Returns:int

the number of bytes

orback to summary
public void or(FormatableBitSet otherBit)

Bitwise OR this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. a noop). A bitset that is smaller than its operand is expanded to the same size.

Parameters
otherBit:FormatableBitSet

bitset operand

readExternalback to summary
public void readExternal(ObjectInput in) throws IOException

Implements java.io.Externalizable.readExternal.

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.

Parameters
in:ObjectInput

Doc from java.io.Externalizable.readExternal.

the stream to read data from in order to restore the object

Exceptions
IOException:

Doc from java.io.Externalizable.readExternal.

if I/O errors occur

See Also
java.io.Externalizable#readExternal
setback to summary
public void set(int position)

Bit set

Parameters
position:int

the bit to set

shrinkback to summary
public void shrink(int n)

Shrink (narrow) a FormatableBitSet to N bits. N may not be larger than the current bitset size, or negative.

Parameters
n:int

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

sizeback to summary
public int size()

Get the length in bits -- alias for getLength()

Returns:int

The length in bits for this value

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.

Returns:String

A new String containing the formatted Bit value

udiv8back to summary
private static int udiv8(int i)
umod8back to summary
private static byte umod8(int i)
umul8back to summary
private static int umul8(int i)
writeExternalback to summary
public void writeExternal(ObjectOutput out) throws IOException

Implements java.io.Externalizable.writeExternal.

Format:

  • int length in bits
  • byte[]
Parameters
out:ObjectOutput

Doc from java.io.Externalizable.writeExternal.

the stream to write the object to

Exceptions
IOException:

Doc from java.io.Externalizable.writeExternal.

Includes any I/O exceptions that may occur

See Also
java.io.Externalizable#writeExternal
xorback to summary
public void xor(FormatableBitSet otherBit)

Bitwise XOR this FormatableBitSet with another FormatableBitSet. The result is stored in this bitset. The operand is unaffected. A null operand is treated as an empty bitset (i.e. a noop). A bitset that is smaller than its operand is expanded to the same size.

Parameters
otherBit:FormatableBitSet

bitset operand