Casting Returned Data
Any data returned by getReturnData is of type Object. This object may be a scalar or a multi-dimensional array, depending on what was returned by the JWAVE wrapper.
Numerical scalars are returned as one of the java.lang.Number subclasses (java.lang.Integer, java.lang.Float, and so on.). Numerical arrays are returned as an array of one of the primitive numeric types (such as int[] or float[][]). Strings are returned as java.lang.String, java.lang.String[], and so on.
In order to make the data returned by getReturnData useful, you need to cast it to something. For example, if you are sure of the data type and array dimensions returned by the wrapper, you can write a statement such as:
int[][] result = (int[][]) myJWaveExecute.getReturnData(”DATA_NAME”);
This is the easiest technique, and thus it is valuable to make sure that the data types returned by the wrapper are explicitly specified.
If you are unsure of the data type (PV‑WAVE is a loosely-typed language), then you must test the returned Object for its type. Usually, you will know something about the data, such as its number of array dimensions, whether it is a string or a number, and so on. So usually you will only need to test for (or assume) things like array size and then just cast or assign the result to whatever variable you will use in the Java program.
Of course, you can use the instanceof operator to test for particular types.
The classes java.lang.Class, java.lang.reflect.Array, and
com.visualnumerics.util.ArrayUtils are other useful tools for dealing with the object returned by getReturnData. Some examples of using these classes are shown in the following example:
Array handling
Object result = myJWaveExecute.getReturnData(”DATA_NAME”);
// Test if the returned object is an array
if ( result.getClass().isArray() ) {
System.out.println(”Is Array”);
// Get the size of the array (i.e. [3][4][5])
int[] dims = ArrayUtils.getArrayDimensions(result);
System.out.println(”Num Dims = ”+ dims.length);
System.out.print(”Dims = ”);
for (int i=0; i<dims.length; ++i)
System.out.println(”[” + dims[i] + ”]”);
System.out.println();
// Get data type of array’s contents
// Note that result.getClass() just tells you that it is an array
// And Class.getComponentType() is only useful for 1D arrays
// This gives you the Class of the contents of the array,
// no matter the dimensional size of the array
Class c = ArrayUtils.getBaseComponentType(result);
System.out.println(”Type = ” + c.getName());
// store into double[]
// Ensure 1D numeric array
if (dims.length == 1 && !String.class.isAssignableFrom(c)) {
double[] dblResult = new double[dims[0]];
// Store into double array
for (int i=0; i<dblResult.length; ++i)
dblResult[i] = Array.getDouble(result, i);
System.out.println(”Stored into double[].”);
} else {
// Do different things for multi-dim arrays, strings...
// See ArrayUtils.getAsOneDimArray(), for example
}
} else { // not array
System.out.println(”Is Scalar”);
System.out.println(”Type = ” + result.getClass().getName());
// Store into int scalar
if (result instanceof Number) {
double dblResult = ((Number)result).doubleValue();
System.out.println(”Stored into double = ”+ dblResult);
} else {
// Do different things for strings...
}
}