net.wimpi.modbus.util
Class BitVector

java.lang.Object
  extended by net.wimpi.modbus.util.BitVector

public final class BitVector
extends Object

Class that implements a collection for bits, storing them packed into bytes. Per default the access operations will index from the LSB (rightmost) bit.

Version:
@version@ (@date@)
Author:
Dieter Wimberger

Constructor Summary
BitVector(int size)
          Constructs a new BitVector instance with a given size.
 
Method Summary
 int byteSize()
          Returns the number of bytes used to store the collection of bits as int.
static BitVector createBitVector(byte[] data)
          Factory method for creating a BitVector instance wrapping the given byte data.
static BitVector createBitVector(byte[] data, int size)
          Factory method for creating a BitVector instance wrapping the given byte data.
 void forceSize(int size)
          Forces the number of bits in this BitVector.
 boolean getBit(int index)
          Returns the state of the bit at the given index of this BitVector.
 byte[] getBytes()
          Returns the byte[] which is used to store the bits of this BitVector.
 boolean isLSBAccess()
          Tests if this BitVector has the LSB (rightmost) as the first bit (i.e.
 boolean isMSBAccess()
          Tests if this BitVector has the MSB (leftmost) as the first bit (i.e.
static void main(String[] args)
           
 void setBit(int index, boolean b)
          Sets the state of the bit at the given index of this BitVector.
 void setBytes(byte[] data)
          Sets the byte[] which stores the bits of this BitVector.
 void setBytes(byte[] data, int size)
          Sets the byte[] which stores the bits of this BitVector.
 int size()
          Returns the number of bits in this BitVector as int.
 void toggleAccess(boolean b)
          Toggles the flag deciding whether the LSB or the MSB of the byte corresponds to the first bit (index=0).
 String toString()
          Returns a String representing the contents of the bit collection in a way that can be printed to a screen or log.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitVector

public BitVector(int size)
Constructs a new BitVector instance with a given size.

Parameters:
size - the number of bits the BitVector should be able to hold.
Method Detail

toggleAccess

public void toggleAccess(boolean b)
Toggles the flag deciding whether the LSB or the MSB of the byte corresponds to the first bit (index=0).

Parameters:
b - true if LSB=0 up to MSB=7, false otherwise.

isLSBAccess

public boolean isLSBAccess()
Tests if this BitVector has the LSB (rightmost) as the first bit (i.e. at index 0).

Returns:
true if LSB=0 up to MSB=7, false otherwise.

isMSBAccess

public boolean isMSBAccess()
Tests if this BitVector has the MSB (leftmost) as the first bit (i.e. at index 0).

Returns:
true if LSB=0 up to MSB=7, false otherwise.

getBytes

public final byte[] getBytes()
Returns the byte[] which is used to store the bits of this BitVector.

Returns:
the byte[] used to store the bits.

setBytes

public final void setBytes(byte[] data)
Sets the byte[] which stores the bits of this BitVector.

Parameters:
data - a byte[].

setBytes

public final void setBytes(byte[] data,
                           int size)
Sets the byte[] which stores the bits of this BitVector.

Parameters:
data - a byte[].

getBit

public final boolean getBit(int index)
                     throws IndexOutOfBoundsException
Returns the state of the bit at the given index of this BitVector.

Parameters:
index - the index of the bit to be returned.
Returns:
true if the bit at the specified index is set, false otherwise.
Throws:
IndexOutOfBoundsException - if the index is out of bounds.

setBit

public final void setBit(int index,
                         boolean b)
                  throws IndexOutOfBoundsException
Sets the state of the bit at the given index of this BitVector.

Parameters:
index - the index of the bit to be set.
b - true if the bit should be set, false if it should be reset.
Throws:
IndexOutOfBoundsException - if the index is out of bounds.

size

public final int size()
Returns the number of bits in this BitVector as int.

Returns:
the number of bits in this BitVector.

forceSize

public final void forceSize(int size)
Forces the number of bits in this BitVector.

Parameters:
size -
Throws:
IllegalArgumentException - if the size exceeds the byte[] store size multiplied by 8.

byteSize

public final int byteSize()
Returns the number of bytes used to store the collection of bits as int.

Returns:
the number of bits in this BitVector.

toString

public String toString()
Returns a String representing the contents of the bit collection in a way that can be printed to a screen or log.

Note that this representation will ALLWAYS show the MSB to the left and the LSB to the right in each byte.

Overrides:
toString in class Object
Returns:
a String representing this BitVector.

createBitVector

public static BitVector createBitVector(byte[] data,
                                        int size)
Factory method for creating a BitVector instance wrapping the given byte data.

Parameters:
data - a byte[] containing packed bits.
Returns:
the newly created BitVector instance.

createBitVector

public static BitVector createBitVector(byte[] data)
Factory method for creating a BitVector instance wrapping the given byte data.

Parameters:
data - a byte[] containing packed bits.
Returns:
the newly created BitVector instance.

main

public static void main(String[] args)


Copyright © 2010. All Rights Reserved.