Package org.snmp4j.smi
Class VariantVariable
java.lang.Object
org.snmp4j.smi.AbstractVariable
org.snmp4j.smi.VariantVariable
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<Variable>
,BERSerializable
,AssignableFromByteArray
,AssignableFromInteger
,AssignableFromLong
,AssignableFromString
,Variable
public class VariantVariable
extends AbstractVariable
implements AssignableFromInteger, AssignableFromLong, AssignableFromString, AssignableFromByteArray
The
VariantVariable
provides a decorator for any type
of Variable instance, to be able to intercept or monitor variable
value modification by using a VariantVariableCallback
.
This class will work for implementations that use getSyntax()
method to determine the variables syntax. However "instanceof" will not
work.
In contrast to the native Variable
implementations,
VariantVariable
can be modified dynamically (i.e. while
a PDU is being BER encoded where this variable has been added to) without
causing BER encoding errors.
- Since:
- 1.7
- Version:
- 1.8
- Author:
- Frank Fock
- See Also:
-
Field Summary
Fields inherited from class org.snmp4j.smi.AbstractVariable
SMISYNTAXES_PROPERTIES
-
Constructor Summary
ConstructorDescriptionVariantVariable
(Variable initialVariable) Creates a variant variable wrapping the specified value.VariantVariable
(Variable initialVariable, VariantVariableCallback callback) Creates a variant variable wrapping the specified value and a callback that monitors value modifications. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Clones this variable.int
void
decodeBER
(BERInputStream inputStream) Decodes aVariable
from anBERInputStream
.void
encodeBER
(OutputStream outputStream) Encodes aVariable
to anOutputStream
.boolean
void
fromSubIndex
(OID subIndex, boolean impliedLength) Sets the value of thisVariable
from the supplied (sub-)index.int
Returns the length of thisVariable
in bytes when encoded according to the Basic Encoding Rules (BER).int
Gets the ASN.1 syntax identifier value of this SNMP variable.Returns the typed variable that holds the wrapped value.int
hashCode()
boolean
Indicates whether this variable is dynamic, which means that it might change its value while it is being (BER) serialized.void
setValue
(byte[] value) Sets the value of this object from the supplied byte array.void
setValue
(int value) void
setValue
(long value) void
void
setValue
(OctetString value) byte[]
Returns the value of this object as a byte array.int
toInt()
Returns an integer representation of this variable if such a representation exists.long
toLong()
Returns a long representation of this variable if such a representation exists.toString()
Gets a string representation of the variable.toSubIndex
(boolean impliedLength) Converts the value of thisVariable
to a (sub-)index value.protected void
protected void
Methods inherited from class org.snmp4j.smi.AbstractVariable
createFromBER, createFromSyntax, equal, getBERPayloadLength, getSyntaxFromString, getSyntaxString, getSyntaxString, isException
-
Constructor Details
-
VariantVariable
Creates a variant variable wrapping the specified value.- Parameters:
initialVariable
- aVariable
.
-
VariantVariable
Creates a variant variable wrapping the specified value and a callback that monitors value modifications.- Parameters:
initialVariable
- aVariable
.callback
- a callback handler that is called before the value is to be modified and after it has been modified.
-
-
Method Details
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Variable>
- Specified by:
compareTo
in interfaceVariable
- Specified by:
compareTo
in classAbstractVariable
-
updateVariable
protected void updateVariable() -
variableUpdated
protected void variableUpdated() -
decodeBER
Description copied from class:AbstractVariable
Decodes aVariable
from anBERInputStream
.- Specified by:
decodeBER
in interfaceBERSerializable
- Specified by:
decodeBER
in classAbstractVariable
- Parameters:
inputStream
- anBERInputStream
containing a BER encoded byte stream.- Throws:
IOException
- if the stream could not be decoded by using BER rules.
-
encodeBER
Description copied from class:AbstractVariable
Encodes aVariable
to anOutputStream
.- Specified by:
encodeBER
in interfaceBERSerializable
- Specified by:
encodeBER
in classAbstractVariable
- Parameters:
outputStream
- anOutputStream
.- Throws:
IOException
- if an error occurs while writing to the stream.
-
fromSubIndex
Description copied from class:AbstractVariable
Sets the value of thisVariable
from the supplied (sub-)index.- Specified by:
fromSubIndex
in interfaceVariable
- Specified by:
fromSubIndex
in classAbstractVariable
- Parameters:
subIndex
- the sub-index OID.impliedLength
- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetString
andOID
). For other variables it has no effect.
-
getBERLength
public int getBERLength()Description copied from class:AbstractVariable
Returns the length of thisVariable
in bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERLength
in interfaceBERSerializable
- Specified by:
getBERLength
in classAbstractVariable
- Returns:
- the BER encoded length of this variable.
-
getSyntax
public int getSyntax()Description copied from class:AbstractVariable
Gets the ASN.1 syntax identifier value of this SNMP variable.- Specified by:
getSyntax
in interfaceVariable
- Specified by:
getSyntax
in classAbstractVariable
- Returns:
- an integer value less than 128 for regular SMI objects and a value greater or equal than 128 for exception values like noSuchObject, noSuchInstance, and endOfMibView.
-
toInt
public int toInt()Description copied from class:AbstractVariable
Returns an integer representation of this variable if such a representation exists.- Specified by:
toInt
in interfaceAssignableFromInteger
- Specified by:
toInt
in interfaceVariable
- Specified by:
toInt
in classAbstractVariable
- Returns:
- an integer value (if the native representation of this variable would be a long, then the long value will be casted to int).
-
toLong
public long toLong()Description copied from class:AbstractVariable
Returns a long representation of this variable if such a representation exists.- Specified by:
toLong
in interfaceAssignableFromLong
- Specified by:
toLong
in interfaceVariable
- Specified by:
toLong
in classAbstractVariable
- Returns:
- a long value.
-
toByteArray
public byte[] toByteArray()Description copied from interface:AssignableFromByteArray
Returns the value of this object as a byte array.- Specified by:
toByteArray
in interfaceAssignableFromByteArray
- Returns:
- a byte array.
-
toSubIndex
Description copied from class:AbstractVariable
Converts the value of thisVariable
to a (sub-)index value.- Specified by:
toSubIndex
in interfaceVariable
- Specified by:
toSubIndex
in classAbstractVariable
- Parameters:
impliedLength
- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetString
andOID
). For other variables it has no effect.- Returns:
- an OID that represents this value as an (sub-)index.
-
equals
- Specified by:
equals
in interfaceVariable
- Specified by:
equals
in classAbstractVariable
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceVariable
- Specified by:
hashCode
in classAbstractVariable
-
toString
Description copied from class:AbstractVariable
Gets a string representation of the variable.- Specified by:
toString
in interfaceVariable
- Specified by:
toString
in classAbstractVariable
- Returns:
- a string representation of the variable's value.
-
clone
Description copied from interface:Variable
Clones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a clone for internal processing. The content of this object is independent to the content of the clone. Thus, changes to the clone will have no effect to this object.- Specified by:
clone
in interfaceVariable
- Specified by:
clone
in classAbstractVariable
- Returns:
- a new instance of this
Variable
with the same value.
-
setValue
public void setValue(int value) - Specified by:
setValue
in interfaceAssignableFromInteger
-
setValue
public void setValue(long value) - Specified by:
setValue
in interfaceAssignableFromLong
-
setValue
-
setValue
public void setValue(byte[] value) Description copied from interface:AssignableFromByteArray
Sets the value of this object from the supplied byte array.- Specified by:
setValue
in interfaceAssignableFromByteArray
- Parameters:
value
- a byte array.
-
setValue
- Specified by:
setValue
in interfaceAssignableFromString
-
getVariable
Returns the typed variable that holds the wrapped value.- Returns:
- a Variable instance.
-
isDynamic
public boolean isDynamic()Description copied from class:AbstractVariable
Indicates whether this variable is dynamic, which means that it might change its value while it is being (BER) serialized. If a variable is dynamic, it will be cloned on-the-fly when it is added to aPDU
withPDU.add(VariableBinding)
. By cloning the value, it is ensured that there are no inconsistent changes between determining the length withAbstractVariable.getBERLength()
for encoding enclosing SEQUENCES and the actual encoding of the Variable itself withAbstractVariable.encodeBER(java.io.OutputStream)
.- Specified by:
isDynamic
in interfaceVariable
- Overrides:
isDynamic
in classAbstractVariable
- Returns:
false
by default. Derived classes may override this if implementing dynamicVariable
instances.
-