org.opcfoundation.ua.builtintypes
Class Variant

java.lang.Object
  extended by org.opcfoundation.ua.builtintypes.Variant

public class Variant
extends Object

Variant wraps an arbitrary builtin variable, an array of builtin variables or a multi-dimension array of builtin variable. Variant is equals-comparable.

An example: Variant v = new Variant( new UnsignedInteger[4][5][6] );

The value may be builtin primitive or a Structure. e.g. new Variant( new NotificationData() ); Encoders write a structure as an ExtensionObject.


Field Summary
static Variant NULL
          Constant NULL
 
Constructor Summary
Variant(Object value)
          Create variant.
 
Method Summary
<T> T
asClass(Class<T> clazz, T defaultValue)
          Convert the variant value to any class.
<T extends Enum<T> & Enumeration>
Object
asEnum(Class<T> clazz)
          Converts the value to the given Enumeration objects.
 boolean booleanValue()
          Returns the value of the specified Variant as a boolean
 byte byteValue()
          Returns the value of the specified Variant as a byte.
 int compareTo(Variant value2)
          Compares the value of a variant with another variant.
protected  String compositeClassToString()
          compositeClassToString.
 double doubleValue()
          Returns the value of the specified Variant as a double.
 boolean equals(Object obj)
          
 float floatValue()
          Returns the value of the specified Variant as a float.
 int[] getArrayDimensions()
          getArrayDimensions.
 Class<?> getCompositeClass()
          The class type of the variant value.
 int getDimension()
          getDimension.
 Object getValue()
          Getter for the field value.
 int hashCode()
          
 int intValue()
          Returns the value of the specified Variant as an int.
 boolean isArray()
          isArray.
 boolean isComparable()
          isComparable.
 boolean isEmpty()
          isEmpty.
 boolean isNumber()
          isNumber.
 long longValue()
          Returns the value of the specified Variant as a long.
 short shortValue()
          Returns the value of the specified Variant as a short.
 Number toNumber()
          Returns the value of the specified Variant as a Number
 String toString()
          
 String toString(boolean includeCompositeClass)
          Convert the value to string, optionally including the compositeClass.
 String toStringWithType()
          Convert the value to string, including the compositeClass.
 boolean valueEquals(Variant value2)
          Compares the value of a variant with another variant.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final Variant NULL
Constant NULL

Constructor Detail

Variant

public Variant(Object value)
Create variant.

Parameters:
value - scalar, array or multi-dimension array
Method Detail

isEmpty

public boolean isEmpty()

isEmpty.

Returns:
a boolean.

isArray

public boolean isArray()

isArray.

Returns:
a boolean.

getValue

public Object getValue()

Getter for the field value.

Returns:
a Object object.

toString

public String toString()

Overrides:
toString in class Object

toStringWithType

public String toStringWithType()
Convert the value to string, including the compositeClass.

Returns:
a String object.

toString

public String toString(boolean includeCompositeClass)
Convert the value to string, optionally including the compositeClass.

Parameters:
includeCompositeClass - whether to also add the name of the compositeClass
Returns:
the value as string

compositeClassToString

protected String compositeClassToString()

compositeClassToString.

Returns:
a String object.

getCompositeClass

public Class<?> getCompositeClass()
The class type of the variant value. If the value is an array, the type may be an ExtensionObject, in which case the array elements must be decoded to find out the actual type of each.

Returns:
the class of Value

getArrayDimensions

public int[] getArrayDimensions()

getArrayDimensions.

Returns:
an array of int.

getDimension

public int getDimension()

getDimension.

Returns:
a int.

hashCode

public int hashCode()

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

asClass

public <T> T asClass(Class<T> clazz,
                     T defaultValue)
Convert the variant value to any class. If it cannot be converted returns defaultValue.

Type Parameters:
T - a T object.
Parameters:
clazz - The class type to convert to.
defaultValue - A default value to return, if the conversion cannot be done
Returns:
Value as the requested class or defaultValue, if it cannot be converted.

asEnum

public <T extends Enum<T> & Enumeration> Object asEnum(Class<T> clazz)
Converts the value to the given Enumeration objects. NOTE! this works only if the value of this Variant is Integer or somedimensional array of them. NOTE! If there is no matching Enumeration value for internal value Integers, null is used.

Parameters:
clazz - enumeration class
Returns:
equivalent scalar/X-dim array of Enumerations of given class or null if internal value is null
Throws:
ClassCastException - if internal value is not Integer (or somedimensional array of them)

booleanValue

public boolean booleanValue()
Returns the value of the specified Variant as a boolean

Returns:
the Variant value as boolean, if it can be cast to such
Throws:
ClassCastException - if the value cannot be cast to boolean

toNumber

public Number toNumber()
Returns the value of the specified Variant as a Number

Returns:
the Variant value as Number, if it can be cast to such
Throws:
ClassCastException - if the value cannot be cast to Number

isNumber

public boolean isNumber()

isNumber.

Returns:
true if the current value is an instance of Number or Boolean (which can be used as Integer)

isComparable

public boolean isComparable()

isComparable.

Returns:
a boolean.

intValue

public int intValue()
Returns the value of the specified Variant as an int. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type int.
Throws:
ClassCastException - if the value cannot be cast to Number

longValue

public long longValue()
Returns the value of the specified Variant as a long. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type long.
Throws:
ClassCastException - if the value cannot be cast to Number

floatValue

public float floatValue()
Returns the value of the specified Variant as a float. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type float.
Throws:
ClassCastException - if the value cannot be cast to Number

doubleValue

public double doubleValue()
Returns the value of the specified Variant as a double. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type double.
Throws:
ClassCastException - if the value cannot be cast to Number

byteValue

public byte byteValue()
Returns the value of the specified Variant as a byte. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type byte.
Throws:
ClassCastException - if the value cannot be cast to Number

shortValue

public short shortValue()
Returns the value of the specified Variant as a short. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type short.
Throws:
ClassCastException - if the value cannot be cast to Number

compareTo

public int compareTo(Variant value2)
Compares the value of a variant with another variant. If both variants isNumber(), compares the floatValue() of each.

Otherwise, it will check if isComparable(), in which case it will compare the values as Comparables. If the values are of different class, it will try to cast the values to the same type.

Parameters:
value2 - the value to compare to
Returns:
true if the values of the variants are equal
Throws:
ClassCastException - if the values cannot be compared

valueEquals

public boolean valueEquals(Variant value2)
Compares the value of a variant with another variant. If the values are of the same class, equals(Object) is called. If both variants isNumber(), compares the floatValue() of each.

Parameters:
value2 - the value to compare to
Returns:
true if the values of the variants are equal


Copyright © 2018. All rights reserved.