com.mathworks.toolbox.javabuilder
Class MWJavaObjectRef

java.lang.Object
  extended by com.mathworks.toolbox.javabuilder.MWArray
      extended by com.mathworks.toolbox.javabuilder.MWJavaObjectRef
All Implemented Interfaces:
Disposable, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable

public class MWJavaObjectRef
extends MWArray

MWJavaObjectRef, a special subclass of MWArray, can be used to create a MATLAB array that references a Java object.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.mathworks.toolbox.javabuilder.MWArray
EMPTY_ARRAY
 
Constructor Summary
MWJavaObjectRef(java.lang.Object o)
          Example: Instantiate a new Builder component object:
 
Method Summary
<T> T
applyVisitor(AbstractMWArrayVisitor<T> v)
           
 MWClassID classID()
          Returns the MATLAB type of this array.
 java.lang.Object clone()
          Example: Cloning (deep copying) an array
 int[] columnIndex()
          This method returns an array containing the column index of each element in the underlying MATLAB array.
 int compareTo(java.lang.Object obj)
          This method compares the MWArray object with the input object.
 void dispose()
          Frees the native MATLAB array contained by this array.
 boolean equals(java.lang.Object obj)
          Indicates whether some other array is equal to this one.
 java.lang.Object get()
          Example: Get information about a referenced object
 java.lang.Object get(int index)
          Returns the element at the specified 1-based offset in this array.
 java.lang.Object get(int[] index)
          Returns the element at the specified 1-based index-array in this array.
 java.lang.Object getData()
          Returns a 1-D array containing a copy of the data in the underlying MATLAB array.
 int[] getDimensions()
          Returns an array containing the size of each dimension of this array.
 int hashCode()
          Returns a hash code value for this array.
 boolean isEmpty()
          This method returns true if the array object contains no elements, and false otherwise.
 boolean isSparse()
          This method returns true if the MWArray object is sparse, and false otherwise.
 int maximumNonZeros()
          Returns the allocated capacity of a sparse array.
 int numberOfDimensions()
          Returns the number of dimensions of this array.
 int numberOfElements()
          Returns the total number of elements in this array.
 int numberOfNonZeros()
          Returns the number of non-zero elements in a sparse array.
 int[] rowIndex()
          This method returns an array containing the row index of each element in the underlying MATLAB array.
 void set(int[] index, java.lang.Object element)
          Replaces the element at the specified 1-based index-array in this array with the specified element.
 void set(int index, java.lang.Object element)
          Replaces the element at the specified 1-based offset in this array with the specified element.
 void set(java.lang.Object o)
          Example: Setting an array to specified values
 void setData(java.lang.Object data)
           
 java.lang.Object sharedCopy()
          Example: Creating a shared copy of an array
 java.lang.Object[] toArray()
          Returns an array containing a copy of the data in the underlying MATLAB array.
 java.lang.String toString()
          Example: Return string representation of array
static java.lang.Object unwrapJavaObjectRefs(java.lang.Object obj)
          Example:
static java.lang.Object[] unwrapJavaObjectRefs(java.lang.Object[] array)
          Example:
 
Methods inherited from class com.mathworks.toolbox.javabuilder.MWArray
disposeArray
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MWJavaObjectRef

public MWJavaObjectRef(java.lang.Object o)
Example: Instantiate a new Builder component object:
    Hashtable myHash = new Hashtable();
    myHash.put("a", new Integer(3));
    myHash.put("b", new Integer(5));
    MWJavaObjectRef A = new MWJavaObjectRef(myHash);
    System.out.println("A = " + A.toString());
 
    MWArray.disposeArray(A);
 
The result is:
    A = {b=5, a=3}
 

Parameters:
o - The object to reference.
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Example: Cloning (deep copying) an array
       Hashtable myHash = new Hashtable();
       myHash.put("a", new Integer(3));
       myHash.put("b", new Integer(5));
       MWJavaObjectRef A = new MWJavaObjectRef(myHash);
 
       Object C = A.clone();
       System.out.println("A = " + A.toString() +
                ", Clone of A = " +  
                        C.toString());
 
       myHash.put("c", new Integer(7));
 
       System.out.println("After change: A = " + A.toString() +
                        ", Clone of A = " +  
                        C.toString());
 
       A.set(new Integer(12));
 
       System.out.println("After set: A = " + A.toString() +
                                ", Clone of A = " +  
                                C.toString());
 
       MWArray.disposeArray(A);
       MWArray.disposeArray(C);
 
Initial results:
 A = {b=5, a=3}, Clone of A = {b=5, a=3}
 
Results after change:
 A = {b=5, a=3, c=7}, Clone of A = {b=5, a=3, c=7}
 
Results after set:
 A = 12, Clone of A = {b=5, a=3, c=7}
 

Overrides:
clone in class MWArray
Returns:
An MWArray instance representing a deep copy of the underlying MATLAB array.
Throws:
java.lang.CloneNotSupportedException - - The object's class does not implement the Cloneable interface.

sharedCopy

public java.lang.Object sharedCopy()
Example: Creating a shared copy of an array
       Hashtable myHash = new Hashtable();
       myHash.put("a", new Integer(3));
       myHash.put("b", new Integer(5));
       MWJavaObjectRef A = new MWJavaObjectRef(myHash);
 
       Object S = A.sharedCopy();
       System.out.println("A = " + A.toString() +
                ", Shared copy of A = " +  
                        S.toString());
 
       myHash.put("c", new Integer(7));
 
       System.out.println("After change: A = " + A.toString() +
                                ", Shared copy of A = " +  
                                S.toString());
 
       MWArray.disposeArray(A);
       MWArray.disposeArray(S);
 
Initial results are as follows:
       A = {b=5, a=3}, Shared copy of A = {b=5, a=3}
 
Results after change:
       A = {b=5, a=3, c=7}, Shared copy of A = {b=5, a=3, c=7}
 

Specified by:
sharedCopy in class MWArray
Returns:
An MWArray instance representing a shared copy of the underlying MATLAB array.

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other array is equal to this one.

Example: Indicating if arrays are equal:
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        Hashtable myHash2 = new Hashtable();
                        myHash2.put("a", new Integer(3));
                        myHash2.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        MWJavaObjectRef B = new MWJavaObjectRef(myHash);
                        MWJavaObjectRef C = new MWJavaObjectRef(myHash2);
 
                        System.out.println("Do A and B refer to the same object? " + A.equals(B));
                        System.out.println("Do A and C? " + A.equals(C));
 
                        MWArray.disposeArray(A);
                        MWArray.disposeArray(B);
                        MWArray.disposeArray(C);
 
Results are as follows:
          Do A and B refer to the same object? true
          Do A and C? false
 

Specified by:
equals in class MWArray
Parameters:
obj - Object Array to compare with this MWArray object

hashCode

public int hashCode()
Returns a hash code value for this array.

Example: Return hash of array
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(8));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        int h = A.hashCode();
                        System.out.println("Hash of A is 0x" + Integer.toHexString(h));
 
                        MWArray.disposeArray(A);
 
Results are as follows:
          Hash of A is 0xffffff33
 

Specified by:
hashCode in class MWArray

toString

public java.lang.String toString()
Example: Return string representation of array
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        System.out.println("String representation of A is " + A.toString());
 
                        MWArray.disposeArray(A);
 
Results are as follows:
          String representation of A is {b=5, a=3}
 

Specified by:
toString in class MWArray

numberOfElements

public int numberOfElements()
Returns the total number of elements in this array.

Example: Return number of elements in array
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        System.out.println("Number of elements in A is " + A.numberOfElements());
 
                        MWArray.disposeArray(A);
 
Results are as follows:
 Number of elements in A is 1
 

Specified by:
numberOfElements in class MWArray

numberOfNonZeros

public int numberOfNonZeros()
Returns the number of non-zero elements in a sparse array. If the underlying array is non-sparse, this method returns the same value as numberOfElements().

Specified by:
numberOfNonZeros in class MWArray
Returns:
Current number of non-zero elements in a sparse array.

maximumNonZeros

public int maximumNonZeros()
Returns the allocated capacity of a sparse array. If the underlying array is non-sparse, this method returns the same value as numberOfElements().

Specified by:
maximumNonZeros in class MWArray
Returns:
Currently allocated number of non-zero elements in a sparse array.

Example: Display maximum number of non-zero elements in array
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
          myHash.put("b", new Integer(5));
                MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        System.out.println("Maximum number of nonzero elements in A is " + A.maximumNonZeros());
 
                        MWArray.disposeArray(A);
 
Results are as follows:
          Maximum number of nonzero elements in A is 1
 

dispose

public void dispose()
Frees the native MATLAB array contained by this array.

Specified by:
dispose in interface Disposable
Specified by:
dispose in class MWArray

get

public java.lang.Object get()
Example: Get information about a referenced object
          Hashtable myHash = new Hashtable();
                myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        System.out.println("A.get() = " + A.get());
                        System.out.println("Class of referenced object is " + A.get().getClass().toString());
                        System.out.println("Class of A is " + A.getClass().toString());
 
                        MWArray.disposeArray(A);
 
Results are as follows:
          A.get() = {b=5, a=3}
          Class of referenced object is class java.util.Hashtable
          Class of A is class com.mathworks.toolbox.javabuilder.MWJavaObjectRef
 


set

public void set(java.lang.Object o)
Example: Setting an array to specified values
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                        System.out.println("A = " + A.toString());
                        A.set(new Integer(12));
                        System.out.println("After set(), A = " + A.toString());
 
                        MWArray.disposeArray(A);
 
Initial results are as follows:
          A = {b=5, a=3}
 
Results after set:
          A = 12
 


classID

public MWClassID classID()
Returns the MATLAB type of this array.

Specified by:
classID in class MWArray
Returns:
MWClassID of this array

Example: Returning MATLAB type of a given array:
          Hashtable myHash = new Hashtable();
                                myHash.put("a", new Integer(3));
                                myHash.put("b", new Integer(5));
                                MWJavaObjectRef A = new MWJavaObjectRef(myHash);
                                System.out.println("ClassID of A is " + A.classID());

                                MWArray.disposeArray(A);
 
The result is:
          ClassID of A is opaque
 

toArray

public java.lang.Object[] toArray()
Returns an array containing a copy of the data in the underlying MATLAB array. The returned array has the same dimensionality as the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, toArray returns the real part. If the underlying array is sparse, a full representation of the array is returned. Care should be taken when calling toArray on a sparse array with large row and column dimensions, as this action may exhaust system memory. If the underlying array is a cell or struct array, toArray is recursively called on each cell.

Specified by:
toArray in class MWArray
Returns:
An array with the same dimensionality of the MATLAB array.

Example: Return array containing a copy of the data in underlying array plus additional information about array
          Integer myInt = new Integer(2);
                        System.out.println("myInts = " + myInt.toString());
                        MWJavaObjectRef A = new MWJavaObjectRef(myInt);
                        Object[] B = A.toArray();
                        System.out.println("Array form is an instance of " + B.getClass().toString());
                        System.out.println("B = " + B);
                        System.out.println("Each element is an instance of " + B[0].getClass().toString());
                        System.out.println("B[0] = " + B[0]);
                        System.out.println("A is an instance of " + A.getClass().toString());
 
                        MWArray.disposeArray(A);
 
Results are as follows:
          myInts = 2
          Array form is an instance of class [Ljava.lang.Object;
          B = [Ljava.lang.Object;@1d53f5b
          Each element is an instance of class java.lang.Integer
          B[0] = 2
          A is an instance of class com.mathworks.toolbox.javabuilder.MWJavaObjectRef
 

applyVisitor

public <T> T applyVisitor(AbstractMWArrayVisitor<T> v)
Specified by:
applyVisitor in class MWArray

unwrapJavaObjectRefs

public static java.lang.Object unwrapJavaObjectRefs(java.lang.Object obj)
Example:
          Hashtable myHash = new Hashtable();
                        myHash.put("a", new Integer(3));
                        myHash.put("b", new Integer(5));
                        MWJavaObjectRef A = new MWJavaObjectRef(new Integer(12));
                        System.out.println("A referenced the object: " + MWJavaObjectRef.unwrapJavaObjectRefs(A));
 
                        MWJavaObjectRef B = new MWJavaObjectRef(myHash);
                        Object bObj = (Object)B;
                        System.out.println("B referenced the object: " + MWJavaObjectRef.unwrapJavaObjectRefs(bObj));
 
Results are as follows:
          A referenced the object: 12
          B referenced the object: {b=5, a=3}
 


unwrapJavaObjectRefs

public static java.lang.Object[] unwrapJavaObjectRefs(java.lang.Object[] array)
Example:
          MWJavaObjectRef A = new MWJavaObjectRef(new Integer(12));
                        MWJavaObjectRef B = new MWJavaObjectRef(new Integer(104));
                        Object[] refArr = new Object[2];
                        refArr[0] = A;
                        refArr[1] = B;
                        Object[] objArr = MWJavaObjectRef.unwrapJavaObjectRefs(refArr);
                        System.out.println("refArr referenced the objects: " + objArr[0] + " and " + objArr[1]);
 
Results are as follows:
          refArr referenced the objects: 12 and 104
 


numberOfDimensions

public int numberOfDimensions()
Description copied from class: MWArray
Returns the number of dimensions of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code:
 int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
                  { 7, 8, 9, 10, 11, 12},
                  {13, 14, 15, 16, 17, 18}};
 
 MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
 
Example: Getting the Number of Dimensions of an MWArray Display the number of dimensions for array object A:
 System.out.println("Matrix A has " + A.numberOfDimensions() +
                    " dimensions");
 
When run, the example displays this output:
 Matrix A has 2 dimensions
 

Specified by:
numberOfDimensions in class MWArray

getDimensions

public int[] getDimensions()
Description copied from class: MWArray
Returns an array containing the size of each dimension of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code:
 int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
                  { 7, 8, 9, 10, 11, 12},
                  {13, 14, 15, 16, 17, 18}};
 
 MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
 
Example: Getting Array Dimensions of an MWArray
 int[] dimA = A.getDimensions();
 System.out.println("Dimensions of A are " +
                    dimA[0] + " x " + dimA[1]);
 
When run, the example displays this output:
 Dimensions of A are 3 x 6
 

Specified by:
getDimensions in class MWArray

isEmpty

public boolean isEmpty()
Description copied from class: MWArray
This method returns true if the array object contains no elements, and false otherwise. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code:
 int[][] Adata = {{ 1, 2, 3, 4, 5, 6},
                  { 7, 8, 9, 10, 11, 12},
                  {13, 14, 15, 16, 17, 18}};
 
 MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
 
Example: Testing for an Empty MWArray Display a message if array object A is an empty array. Otherwise, display the contents of A:
 if (A.isEmpty())
    System.out.println("Matrix A is empty");
 else
    System.out.println("A = " + A.toString());
 
When run, the example displays the contents of A:
 A= 1    2   3   4   5   6
    7    8   9  10  11  12
    13  14  15  16  17  18
 

Specified by:
isEmpty in class MWArray

isSparse

public boolean isSparse()
Description copied from class: MWArray
This method returns true if the MWArray object is sparse, and false otherwise.

The contents of the sparse MWArray, created by the newSparse method, used in the following example is:
   (2,1) 50
   (1,2) 10
   (2,2) 60
   (1,5) 40
   (2,5) 90
 
Example: Testing an MWArray for Sparseness Test the MWArray object A created previously for sparseness:
 if (A.isSparse())
     System.out.println("Matrix A is sparse");
 
When run, the example displays this output:
 Matrix A is sparse
 

Specified by:
isSparse in class MWArray

compareTo

public int compareTo(java.lang.Object obj)
Description copied from class: MWArray
This method compares the MWArray object with the input object. It returns a negative integer, zero, or a positive integer if the MWArray object is less than, equal to, or greater than the specified object, respectively.

Example: Comparing MWArrays with compareTo Create a shared copy of the MWArray object and then compare it to the original object. A return value of zero indicates that the two objects are equal:
 Object S = A.sharedCopy();
 if (A.compareTo(S) == 0)
     System.out.println("Matrix S is equal to matrix A");
 
When run, the example displays this output:
 Matrix S is equal to matrix A
 

Specified by:
compareTo in interface java.lang.Comparable
Specified by:
compareTo in class MWArray
Parameters:
obj - Object Array to compare with this MWArray object

get

public java.lang.Object get(int index)
Description copied from class: MWArray
Returns the element at the specified 1-based offset in this array. Offers better performance than public Object get(int[] index).

Example: Getting an MWArray Value with get
 int[] cdims = {1, 3};
          MWArray C = new MWArray(cdims);
          Integer val = new Integer(15);
          int[] index2 = {1, 3};
          C.set(index2, val);
          Object x = C.get(index2);
          if (x instanceof int[][])
          {
           int[][] y = (int[][])x;
               System.out.println("B: Cell data C(1,3) is " + y[0][0]);
          }
 
When run, the example displays this output:
 B: Cell data C(1,3) is 15
 

Specified by:
get in class MWArray
Parameters:
index - The index of the requested element. Valid range: 1 <= index <= N, where N = total number of elements in the array.
Returns:
Object containing the requested element.

get

public java.lang.Object get(int[] index)
Description copied from class: MWArray
Returns the element at the specified 1-based index-array in this array.

Specified by:
get in class MWArray
Parameters:
index - Array of indices specifying the location of the requested element. The length of the index array must be exactly the number of dimensions of this array. Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.
Returns:
Object containing the requested element.

set

public void set(int index,
                java.lang.Object element)
Description copied from class: MWArray
Replaces the element at the specified 1-based offset in this array with the specified element. Offers better performance than public void set(int[] index, Object element).

Example: Setting an MWArray Value Modify the data in element (2, 4) of MWArray object A:
 int[] index = {2, 4};
 A.set(index, 555);
 
 Object d_out = A.get(index);
 System.out.println("Data read from A(2,4) is " +
                     d_out.toString());
 
When run, the example displays this output:
 Data read from A(2,4) is 555
 

Specified by:
set in class MWArray
Parameters:
index - The index of the element to replace. Valid range: 1 <= index <= N, where N = total number of elements in the array.
element - New element to replace at index.

set

public void set(int[] index,
                java.lang.Object element)
Description copied from class: MWArray
Replaces the element at the specified 1-based index-array in this array with the specified element.

Specified by:
set in class MWArray
Parameters:
index - Array of indices specifying the location of the element to replace. The length of the index array must be exactly the number of dimensions of this array. Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.
element - New element to replace at index.

getData

public java.lang.Object getData()
Description copied from class: MWArray
Returns a 1-D array containing a copy of the data in the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, getData returns the real part. If the underlying array is a cell or struct array, toArray is recursively called on each cell.

Example: Getting an MWArray Value with getData Get the data from MWArray object A, casting the type from Object to int:
 System.out.println("Data read from matrix A is:");
 int[] x = (int[]) A.getData();
 for (int i = 0; i < x.length; i++)
     System.out.print(" " + x[i]);
 
 System.out.println();
 
When run, the example displays this output:
 Data read from matrix A is:
   1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18
 

Specified by:
getData in class MWArray
Returns:
A 1-D array of elements stored in column-wise order. The length of the returned array equals MWArray.numberOfElements() for a non-sparse array, and MWArray.numberOfNonZeros() for a sparse array.

setData

public void setData(java.lang.Object data)
Specified by:
setData in class MWArray

rowIndex

public int[] rowIndex()
Description copied from class: MWArray
This method returns an array containing the row index of each element in the underlying MATLAB array.

The contents of the sparse MWArray, created by the newSparse method, used in the following example is:
   (2,1) 50
   (1,2) 10
   (2,2) 60
   (1,5) 40
   (2,5) 90
 
Example: Getting the Row Indices of a Sparse MWArray Get the row indices of the elements of the sparse array:
 System.out.print("Row indices are: ");
 int[] rowidx = A.rowIndex();
 for (int i = 0; i < 5; i++)
     System.out.print(rowidx[i] + " ");
 System.out.println();
 
When run, the example displays this output:
 Row indices are: 2 1 2 1 2
 

Specified by:
rowIndex in class MWArray
Returns:
Array of indices.

columnIndex

public int[] columnIndex()
Description copied from class: MWArray
This method returns an array containing the column index of each element in the underlying MATLAB array.

The contents of the sparse MWArray, created by the newSparse method, used in the following example is:
   (2,1) 50
   (1,2) 10
   (2,2) 60
   (1,5) 40
   (2,5) 90
 
Example: Getting the Column Indices of a Sparse MWArray Get the column indices of the elements of the sparse array:
 System.out.print("Column indices are: ");
 int[] colidx = A.columnIndex();
 for (int i = 0; i < 5; i++)
     System.out.print(colidx[i] + " ");
 System.out.println();
 
When run, the example displays this output:
 Column indices are: 1 2 2 5 5
 

Specified by:
columnIndex in class MWArray
Returns:
Array of indices.


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS