HOME

com.ireasoning.protocol.snmp
Class SnmpBits

java.lang.Object
  extended bycom.ireasoning.protocol.snmp.SnmpOctetString
      extended bycom.ireasoning.protocol.snmp.SnmpBits
All Implemented Interfaces:
Msg, Serializable, SnmpDataType

public class SnmpBits
extends SnmpOctetString

This class represents SMIv2 BITS data type. The SMIv2 BITS construct is mapped to the string-value selection type of the SimpleSyntax choice. A BITS value is encoded as an OCTET STRING, in which all the named bits in (the definition of) the bitstring, commencing with the first bit and proceeding to the last bit, are placed in bits 8 (high order bit) to 1 (low order bit) of the first octet, followed by bits 8 to 1 of each subsequent octet in turn, followed by as many bits as are needed of the final subsequent octet, commencing with bit 8. Remaining bits, if any, of the final octet are set to zero on generation and ignored on receipt.

Usage example:

 SnmpBits bits = null;
 bits = new SnmpBits(2);
 bits.setValue("100011101");
 //assert(p1, p2) method throws a runtime exception if p1 and p2 are not equal
 assert(bits.toString(), "{0, 4, 5, 6, 8}");//"1000111010000000"
 bits.setValue(new byte[]{(byte)1, (byte)1, (byte)0, (byte)1, (byte)1, (byte)0, (byte)1, (byte)1, (byte)1});
 assert(bits.toString(), "{0, 1, 3, 4, 6, 7, 8}");//"1101101110000000"
 bits.set(2);
 bits.set(5);
 assert(bits.toString(), "{0, 1, 2, 3, 4, 5, 6, 7, 8}");//"1111111110000000"

 bits = new SnmpBits();
 bits.setValue("80");
 assert(bits.toString(), "{0}");//only first bit is set
 bits.clear();//reset all bits
 bits.setValue("1ABCDF2");
 assert(bits.toString(), "{3, 4, 6, 8, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 23, 26}");//"1A BC DF 20"
 bits.setValue(new byte[]{(byte)221, (byte)1, (byte)110, (byte)111, (byte)81, (byte)0, (byte)12, (byte)11, (byte)8});
 assert(bits.toString(), "{0, 1, 3, 4, 5, 7, 15, 17, 18, 20, 21, 22, 25, 26, 28, 29, 30, 31, 33, 35, 39, 52, 53, 60, 62, 63, 68}");//"DD 01 6E 6F 51 00 0C 0B 08"
 bits.set(2);
 bits.set(12);
 assert(bits.toString(), "{0, 1, 2, 3, 4, 5, 7, 12, 15, 17, 18, 20, 21, 22, 25, 26, 28, 29, 30, 31, 33, 35, 39, 52, 53, 60, 62, 63, 68}");// "FD 09 6E 6F 51 00 0C 0B 08"
 

See Also:
Serialized Form

Field Summary
protected  int _radix
          Either 2 or 16
 
Fields inherited from interface com.ireasoning.protocol.snmp.SnmpDataType
BITS, BITSTRING, COUNTER32, COUNTER64, END_OF_MIB_VIEW, END_OF_MIB_VIEW_OBJECT, GAUGE32, INTEGER, IPADDRESS, NO_SUCH_INSTANCE, NO_SUCH_INSTANCE_OBJECT, NO_SUCH_OBJECT, NO_SUCH_OBJECT_OBJECT, NULL, OCTETSTRING, OID, OPAQUE, PDU, SEQUENCE, SEQUENCEOF, serialVersionUID, TIMETICKS, UNSIGNED32, VARBIND
 
Fields inherited from interface com.ireasoning.protocol.Msg
ERROR_TYPE
 
Constructor Summary
SnmpBits()
          Default constructor, use radix 16
SnmpBits(int radix)
          Constructor
SnmpBits(SnmpBits o)
          Makes a copy of passed in SnmpBits object
 
Method Summary
 void and(SnmpBits set)
          Performs a logical AND of this target bit set with the argument bit set.
 void andNot(SnmpBits set)
          Clears all of the bits in this SnmpBits whose corresponding bit is set in the specified SnmpBits.
 int cardinality()
          Returns the number of bits set to true in this SnmpBits.
 void clear()
          Sets all of the bits in this SnmpBits to false.
 void clear(int bitIndex)
          Sets the bit specified by the index to false.
 void clear(int fromIndex, int toIndex)
          Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to false.
 SnmpDataType copy()
          Makes a copy of itself
 int encode(com.ireasoning.protocol.snmp.SnmpEncoder encoder)
          For internal use
 boolean equals(Object obj)
          Compares this object against the specified object.
 void flip(int bitIndex)
          Sets the bit at the specified index to to the complement of its current value.
 void flip(int fromIndex, int toIndex)
          Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value.
 boolean get(int bitIndex)
          Returns the value of the bit with the specified index.
 int getType()
          Returns SnmpDataType.BITS (whichs is equals to SnmpDataType.OCTETSTRING)
 String getTypeString()
          Returns "BITS"
 byte[] getValue()
          Returns byte array representation of this object
 int hashCode()
          Returns a hash code value for this bit set.
 boolean isEmpty()
          Returns true if this SnmpBits contains no bits that are set to true.
static void main(String[] args)
           
 void or(SnmpBits set)
          Performs a logical OR of this bit set with the bit set argument.
 void set(int bitIndex)
          Sets the bit at the specified index to true.
 void set(int bitIndex, boolean value)
          Sets the bit at the specified index to the specified value.
 void set(int fromIndex, int toIndex)
          Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to true.
 void set(int fromIndex, int toIndex, boolean value)
          Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value.
 void setFixedLength(int length)
          Sets the fixed length.
 void setValue(byte[] v)
          Sets a new value.
 void setValue(String str)
          Sets a new value.
 void setValue(String str, boolean useMathSetNotation)
          Sets a new value
 String toString()
          Returns a string representation.
 void xor(SnmpBits set)
          Performs a logical XOR of this bit set with the bit set argument.
 
Methods inherited from class com.ireasoning.protocol.snmp.SnmpOctetString
convertPhysAddress, getBytes, getLength, toHexString, toPrintableString, toString, toString2
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_radix

protected int _radix
Either 2 or 16

Constructor Detail

SnmpBits

public SnmpBits()
Default constructor, use radix 16


SnmpBits

public SnmpBits(int radix)
Constructor

Parameters:
radix - either 2 or 16

SnmpBits

public SnmpBits(SnmpBits o)
Makes a copy of passed in SnmpBits object

Method Detail

getType

public int getType()
Returns SnmpDataType.BITS (whichs is equals to SnmpDataType.OCTETSTRING)

Specified by:
getType in interface SnmpDataType
Overrides:
getType in class SnmpOctetString

getTypeString

public String getTypeString()
Returns "BITS"

Specified by:
getTypeString in interface SnmpDataType
Overrides:
getTypeString in class SnmpOctetString

getValue

public byte[] getValue()
Returns byte array representation of this object

Overrides:
getValue in class SnmpOctetString
Returns:
byte array representation of this object

setValue

public void setValue(byte[] v)
Sets a new value.

Examples:
If radix is 16:
if passed byte array is {0x80}, only the first bit is set, so toString() of this object should be {0}. If passed byte array is {0x80, 0x80}, the 1st and the 8th bits are set, the string format of this object is {0, 8}

Overrides:
setValue in class SnmpOctetString

setValue

public void setValue(String str,
                     boolean useMathSetNotation)
Sets a new value

Parameters:
str - new value in String format
useMathSetNotation - if true, the passed str uses mathematical notation for a set of integers, that is, something like {1, 3, 8}. Otherwise, see setValue(String str)
See Also:
setValue(String str)

setValue

public void setValue(String str)
Sets a new value.

For example:

 SnmpBits bits = new SnmpBits();
 bits.setValue("80");
 assert(bits.toString(), "{0}");//only first bit is set
 bits.clear();//reset all
 bits.setValue("1ABCDF2");
 assert(bits.toString(), "{3, 4, 6, 8, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 23, 26}");//"1A BC DF 20"
 

See Also:
setValue(String str, boolean useMathSetNotation)

toString

public String toString()
Returns a string representation. For every index for which this SnmpBits contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

Overrides the toString method of Object.

Example:

 SnmpBits drPepper = new SnmpBits();
Now drPepper.toString() returns "{}".

 drPepper.set(2);
Now drPepper.toString() returns "{2}".

 drPepper.set(4);
 drPepper.set(10);
Now drPepper.toString() returns "{2, 4, 10}".

Overrides:
toString in class SnmpOctetString
Returns:
a string representation of this bit set.

copy

public SnmpDataType copy()
Description copied from interface: SnmpDataType
Makes a copy of itself

Specified by:
copy in interface SnmpDataType
Overrides:
copy in class SnmpOctetString

encode

public int encode(com.ireasoning.protocol.snmp.SnmpEncoder encoder)
           throws SnmpEncodingException
Description copied from interface: SnmpDataType
For internal use

Specified by:
encode in interface SnmpDataType
Overrides:
encode in class SnmpOctetString
Throws:
SnmpEncodingException

flip

public void flip(int bitIndex)
Sets the bit at the specified index to to the complement of its current value.

Parameters:
bitIndex - the index of the bit to flip.
Throws:
IndexOutOfBoundsException - if the specified index is negative.

flip

public void flip(int fromIndex,
                 int toIndex)
Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value.

Parameters:
fromIndex - index of the first bit to flip.
toIndex - index after the last bit to flip.
Throws:
IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

set

public void set(int bitIndex)
Sets the bit at the specified index to true.

Parameters:
bitIndex - a bit index.
Throws:
IndexOutOfBoundsException - if the specified index is negative.

set

public void set(int bitIndex,
                boolean value)
Sets the bit at the specified index to the specified value.

Parameters:
bitIndex - a bit index.
value - a boolean value to set.
Throws:
IndexOutOfBoundsException - if the specified index is negative.

set

public void set(int fromIndex,
                int toIndex)
Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to true.

Parameters:
fromIndex - index of the first bit to be set.
toIndex - index after the last bit to be set.
Throws:
IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

set

public void set(int fromIndex,
                int toIndex,
                boolean value)
Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value.

Parameters:
fromIndex - index of the first bit to be set.
toIndex - index after the last bit to be set
value - value to set the selected bits to
Throws:
IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

clear

public void clear(int bitIndex)
Sets the bit specified by the index to false.

Parameters:
bitIndex - the index of the bit to be cleared.
Throws:
IndexOutOfBoundsException - if the specified index is negative.

clear

public void clear(int fromIndex,
                  int toIndex)
Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to false.

Parameters:
fromIndex - index of the first bit to be cleared.
toIndex - index after the last bit to be cleared.
Throws:
IndexOutOfBoundsException - if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

clear

public void clear()
Sets all of the bits in this SnmpBits to false.


get

public boolean get(int bitIndex)
Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this SnmpBits; otherwise, the result is false.

Parameters:
bitIndex - the bit index.
Returns:
the value of the bit with the specified index.
Throws:
IndexOutOfBoundsException - if the specified index is negative.

isEmpty

public boolean isEmpty()
Returns true if this SnmpBits contains no bits that are set to true.

Returns:
boolean indicating whether this SnmpBits is empty.

cardinality

public int cardinality()
Returns the number of bits set to true in this SnmpBits.

Returns:
the number of bits set to true in this SnmpBits.

and

public void and(SnmpBits set)
Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.

Parameters:
set - a bit set.

or

public void or(SnmpBits set)
Performs a logical OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.

Parameters:
set - a bit set.

xor

public void xor(SnmpBits set)
Performs a logical XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:

Parameters:
set - a bit set.

andNot

public void andNot(SnmpBits set)
Clears all of the bits in this SnmpBits whose corresponding bit is set in the specified SnmpBits.

Parameters:
set - the SnmpBits with which to mask this SnmpBits.

hashCode

public int hashCode()
Returns a hash code value for this bit set. The has code depends only on which bits have been set within this SnmpBits. The algorithm used to compute it may be described as follows.

Suppose the bits in the SnmpBits were to be stored in an array of long integers called, say, bits, in such a manner that bit k is set in the SnmpBits (for nonnegative values of k) if and only if the expression

((k>>6) < bits.length) && ((bits[k>>6] & (1L << (bit & 0x3F))) != 0)
is true. Then the following definition of the hashCode method would be a correct implementation of the actual algorithm:
 public int hashCode() {
      long h = 1234;
      for (int i = bits.length; --i >= 0; ) {
           h ^= bits[i] * (i + 1);
      }
      return (int)((h >> 32) ^ h);
 }
Note that the hash code values change if the set of bits is altered.

Overrides the hashCode method of Object.

Specified by:
hashCode in interface SnmpDataType
Overrides:
hashCode in class SnmpOctetString
Returns:
a hash code value for this bit set.

equals

public boolean equals(Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Bitset object that has exactly the same set of bits set to true as this bit set. That is, for every nonnegative int index k,
((SnmpBits)obj).get(k) == this.get(k)
must be true. The current sizes of the two bit sets are not compared.

Overrides the equals method of Object.

Specified by:
equals in interface SnmpDataType
Overrides:
equals in class SnmpOctetString
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.

setFixedLength

public void setFixedLength(int length)
Sets the fixed length. It can be used if the value is all '0', whose length is 0 if setFixedLength is not invoked.

For example,

   bits = new SnmpBits(2);
   bits.setValue("00000000");
   bits.setFixedLength(8);
 
Then bits.getValue() will return a byte array with 8 elements instead of 0.

Parameters:
length - the number of bits

main

public static void main(String[] args)

HOME

Copyright © 2002 iReasoning Inc. All Rights Reserved.