org.apache.derby.iapi.services.io
Class FormatableBitSet
- java.lang.Object
-
- org.apache.derby.iapi.services.io.FormatableBitSet
-
- All Implemented Interfaces:
- java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, Formatable, TypedFormat
public final class FormatableBitSet extends java.lang.Object implements Formatable, java.lang.Cloneable
FormatableBitSet is implemented as a packed array of bytes.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description FormatableBitSet()Niladic ConstructorFormatableBitSet(byte[] newValue)Constructs a Bit from an array of bytes.FormatableBitSet(FormatableBitSet original)Copy constructorFormatableBitSet(int numBits)Constructs a Bit with the initial number of bits
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidand(FormatableBitSet otherBit)Bitwise AND this FormatableBitSet with another FormatableBitSet.intanySetBit()If any bit is set, return the zero-based bit index of the first bit that is set.intanySetBit(int beyondBit)Like anySetBit(), but return any set bit whose number is bigger than beyondBit.voidclear()Clear all the bits in this FormatableBitSetvoidclear(int position)Bit clearjava.lang.Objectclone()intcompare(FormatableBitSet other)Bit comparison.voidcopyFrom(FormatableBitSet that)Copy the bytes from another FormatableBitSet.booleanequals(java.lang.Object other)Bit equivalence.booleanget(int position)Bit get -- alias for isSet()byte[]getByteArray()Get the value of the byte arrayintgetLength()Get the length in bitsintgetLengthInBytes()Get the length in bytes of a Bit valueintgetNumBitsSet()Get a count of the number of bits that are set.intgetTypeFormatId()Get the formatID which corresponds to this class.voidgrow(int n)Grow (widen) a FormatableBitSet so that it contains at least N bits.inthashCode()Produce a hash code by putting the value bytes into an int, exclusive OR'ing if there are more than 4 bytes.booleaninvariantHolds()This method returns true if the following conditions hold: 1.booleanisSet(int position)Bit isSetstatic intmaxBitsForSpace(int numBytes)Statically calculates how many bits can fit into the number of bytes if this Bit object is externalized.voidor(FormatableBitSet otherBit)Bitwise OR this FormatableBitSet with another FormatableBitSet.voidreadExternal(java.io.ObjectInput in)Note: gracefully handles zero length bits -- will create a zero length array with no bits being used.voidset(int position)Bit setvoidshrink(int n)Shrink (narrow) a FormatableBitSet to N bits.intsize()Get the length in bits -- alias for getLength()java.lang.StringtoString()Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.voidwriteExternal(java.io.ObjectOutput out)Format: int length in bits byte[]voidxor(FormatableBitSet otherBit)Bitwise XOR this FormatableBitSet with another FormatableBitSet.
-
-
-
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(FormatableBitSet original)
Copy constructor- Parameters:
original- the FormatableBitSet to make a copy from
-
-
Method Detail
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
invariantHolds
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:
- true if invariant holds, false otherwise
-
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.
-
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
-
grow
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- 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.
-
shrink
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- 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(java.lang.Object 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.- Overrides:
equalsin classjava.lang.Object- 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
-
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.- Overrides:
hashCodein classjava.lang.Object- 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
-
toString
public java.lang.String toString()
Format the string into BitSet format: {0, 2, 4, 8} if bits 0, 2, 4, 8 are set.- Overrides:
toStringin classjava.lang.Object- 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 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:
- the zero-based index of the first 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. Supplying a value of -1 makes the call equivalent to anySetBit().- 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 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- bitset operand
-
copyFrom
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.
-
and
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- bitset operand
-
xor
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- bitset operand
-
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.IOExceptionFormat:- int length in bits
- byte[]
- Specified by:
writeExternalin interfacejava.io.Externalizable- Throws:
java.io.IOException- See Also:
Externalizable.writeExternal(java.io.ObjectOutput)
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOExceptionNote: 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:
readExternalin interfacejava.io.Externalizable- Throws:
java.io.IOException- See Also:
Externalizable.readExternal(java.io.ObjectInput)
-
getTypeFormatId
public int getTypeFormatId()
Get the formatID which corresponds to this class.- Specified by:
getTypeFormatIdin interfaceTypedFormat- Returns:
- the formatID of this class
-
-
DataMelt 3.0 © DataMelt by jWork.ORG