com.prosysopc.ua
Class DataTypeConverter

java.lang.Object
  extended by com.prosysopc.ua.DataTypeConverter

public class DataTypeConverter
extends Object

A converter class, used to convert UA values between different data types.

Note that this class is not meant to be instantiated directly. On the client side, UaClient.getAddressSpace().getDataTypeConverter() will return the relevant converter.

On the server side, the converter is available from UaServer.getNodeManagerRoot().getDataTypeConverter().


Constructor Summary
DataTypeConverter(UaAddressSpace addressSpace)
          Creates a new DataTypeConverter, using a given NodeMap that is used to look up the UaDataType nodes that are used.
 
Method Summary
protected  Boolean booleanValue(Object value)
          Returns the value of the specified Variant as a boolean
 org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value, Class<?> toClass)
          Convert a variant value to a new Variant of the specified dataType.
 org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value, org.opcfoundation.ua.builtintypes.NodeId dataTypeId)
          Convert a variant value to a new Variant of the specified dataType.
 org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value, UaDataType dataType)
          Convert a variant value to a new Variant of the specified dataType.
protected  Object convertValue(Object value, Class<?> fromClass, Class<?> toClass)
           
protected  Object convertVariant(org.opcfoundation.ua.builtintypes.Variant value, Class<?> fromClass, Class<?> toClass)
           
 UaDataType getDataTypeForJavaClass(Class<?> clazz)
          Returns a UA data type that corresponds to a Java class.
 UaDataType[] getDataTypesForJavaClass(Class<?> clazz)
          Returns the UA data type that corresponds to a Java class.
 boolean isNullable(org.opcfoundation.ua.builtintypes.NodeId dataType)
          Returns true for all data types, i.e. they are all accepting null values.
protected  Object numberToInteger(Number number, org.opcfoundation.ua.builtintypes.NodeId dataTypeId)
           
protected  Object numberToInteger(Number number, UaDataType dataType)
          Converts a Number to a UA integer value
 org.opcfoundation.ua.builtintypes.Variant parseVariant(String string, Class<?> javaClass)
          Parse a string to a Variant of the specified data type.
 org.opcfoundation.ua.builtintypes.Variant parseVariant(String string, org.opcfoundation.ua.builtintypes.NodeId dataTypeId)
          Parse a string to a Variant of the specified data type.
 org.opcfoundation.ua.builtintypes.Variant parseVariant(String string, UaDataType dataType)
          Parse a string to a Variant of the specified data type.
protected  Number toNumber(Object value, Class<?> toClass)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataTypeConverter

public DataTypeConverter(UaAddressSpace addressSpace)
Creates a new DataTypeConverter, using a given NodeMap that is used to look up the UaDataType nodes that are used.

Parameters:
nodeMap -
Method Detail

convert

public org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value,
                                                         Class<?> toClass)
                                                  throws org.opcfoundation.ua.common.ServiceResultException
Convert a variant value to a new Variant of the specified dataType.

The type conversions work in principle, according to the Conversion Rules specified in [OPC UA Part4. Table 113].

Parameters:
value - the value to convert.
toClass - Java class type, which defines the desired data type
Returns:
a new Variant of the specified dataType. If value is null, or cannot be converted to the requested type, the returned variant is null.
Throws:
org.opcfoundation.ua.common.ServiceResultException - if the conversion fails

convert

public org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value,
                                                         org.opcfoundation.ua.builtintypes.NodeId dataTypeId)
                                                  throws org.opcfoundation.ua.common.ServiceResultException
Convert a variant value to a new Variant of the specified dataType.

The type conversions work in principle, according to the Conversion Rules specified in [OPC UA Part4. Table 113].

The conversions rely on UaDataType nodes that define the proper JavaClass. This is true for the types in the default address space, both in the server and client nodes of the OPCUA Java SDK.

If you define your own data types, make sure that they inherit from the correct base data types or define the JavaClass for each node yourself.

Parameters:
value - the value to convert.
dataTypeId - the NodeId of the UA Data Type to convert to.
Returns:
a new Variant of the specified dataType. If value is null, or cannot be converted to the requested type, the returned variant is null.
Throws:
org.opcfoundation.ua.common.ServiceResultException - if the conversion fails

convert

public org.opcfoundation.ua.builtintypes.Variant convert(org.opcfoundation.ua.builtintypes.Variant value,
                                                         UaDataType dataType)
                                                  throws org.opcfoundation.ua.common.ServiceResultException
Convert a variant value to a new Variant of the specified dataType.

The type conversions work in principle, according to the Conversion Rules specified in [OPC UA Part4. Table 113].

The conversions rely on UaDataType nodes that define the proper JavaClass. This is true for the types in the default address space, both in the server and client nodes of the OPCUA Java SDK.

If you define your own data types, make sure that they inherit from the correct base data types or define the JavaClass for each node yourself.

Parameters:
value - the value to convert.
dataType - the UA Data Type to convert to.
Returns:
a new Variant of the specified dataType. If value is null, or cannot be converted to the requested type, the returned variant is null.
Throws:
org.opcfoundation.ua.common.ServiceResultException - if the conversion fails

getDataTypeForJavaClass

public UaDataType getDataTypeForJavaClass(Class<?> clazz)
Returns a UA data type that corresponds to a Java class. Note! this returns one of possible data types, there may be multiple data types for a Java class, use DataTypeConverter.getDataTypesForJavaClass to get them all

Parameters:
clazz - the Java class
Returns:
the UaDataType node
Throws:
IllegalArgumentException - if a suitable data type is not found

getDataTypesForJavaClass

public UaDataType[] getDataTypesForJavaClass(Class<?> clazz)
Returns the UA data type that corresponds to a Java class.

Parameters:
clazz - the Java class
Returns:
the UaDataType node
Throws:
IllegalArgumentException - if a suitable data type is not found

isNullable

public boolean isNullable(org.opcfoundation.ua.builtintypes.NodeId dataType)
Returns true for all data types, i.e. they are all accepting null values.

Parameters:
dataType -
Returns:
always true

parseVariant

public org.opcfoundation.ua.builtintypes.Variant parseVariant(String string,
                                                              Class<?> javaClass)
Parse a string to a Variant of the specified data type.

The method will try to find the UaDataType that corresponds to the javaClass. If no UaDataType node is found, the value is treated as a string value.

Parameters:
string - string to parse
javaClass - Java class type, which defines the desired data type.
Returns:
a new Variant with value initialized to the specified data type.
Throws:
IllegalArgumentException - if the parsing fails
NullPointerException - if string is null

parseVariant

public org.opcfoundation.ua.builtintypes.Variant parseVariant(String string,
                                                              org.opcfoundation.ua.builtintypes.NodeId dataTypeId)
Parse a string to a Variant of the specified data type.

If dataTypeId does not correspond to a UaDataType node, the value is treated as a string value.

Parameters:
string - string to parse
dataTypeId - NodeId of the data type to create: This must correspond to a UaDataType node in the NodeMap used by the converter.
Returns:
a new Variant with value initialized to the specified data type.
Throws:
IllegalArgumentException - if the parsing fails
NullPointerException - if string is null

parseVariant

public org.opcfoundation.ua.builtintypes.Variant parseVariant(String string,
                                                              UaDataType dataType)
Parse a string to a Variant of the specified data type.

Uses the JavaClass property of dataType to determine the target class. The conversion is done using that types parse-method, for example, Integr.parseInt(). The JavaClass of the default types are predefined.

Parameters:
string - string to parse. Must not be null
dataType - data type to create. The JavaClass of the data type must be defined: the conversion will be done using this class.
Returns:
a new Variant with value initialized to the specified data type.
Throws:
IllegalArgumentException - if the parsing fails
NullPointerException - if string is null

booleanValue

protected Boolean booleanValue(Object value)
                        throws org.opcfoundation.ua.common.ServiceResultException
Returns the value of the specified Variant as a boolean

Returns:
the Variant value as boolean, if it can be cast to such or null if value is null.
Throws:
org.opcfoundation.ua.common.ServiceResultException - if the value is not null, but cannot be cast to boolean

convertValue

protected Object convertValue(Object value,
                              Class<?> fromClass,
                              Class<?> toClass)
                       throws org.opcfoundation.ua.common.ServiceResultException
Parameters:
value -
fromClass -
fromClass -
toClass -
o -
Returns:
Throws:
org.opcfoundation.ua.common.ServiceResultException - if the conversion fails

convertVariant

protected Object convertVariant(org.opcfoundation.ua.builtintypes.Variant value,
                                Class<?> fromClass,
                                Class<?> toClass)
                         throws org.opcfoundation.ua.common.ServiceResultException
Parameters:
value -
fromClass -
toClass -
Returns:
Throws:
org.opcfoundation.ua.common.ServiceResultException

numberToInteger

protected Object numberToInteger(Number number,
                                 org.opcfoundation.ua.builtintypes.NodeId dataTypeId)

numberToInteger

protected Object numberToInteger(Number number,
                                 UaDataType dataType)
Converts a Number to a UA integer value

Parameters:
number -
dataType -
Returns:

toNumber

protected Number toNumber(Object value,
                          Class<?> toClass)
                   throws ClassCastException
Parameters:
value -
toClass -
Returns:
Throws:
ClassCastException


Copyright © 2018. All rights reserved.