Matrix: An example host object class that implements the Scriptable interface. Built-in JavaScript arrays don't handle multiple dimensions gracefully: the script writer must create every array in an array of arrays. The Matrix class takes care of that by automatically allocating arrays for every
| 47 | * @author Norris Boyd |
| 48 | */ |
| 49 | public class Matrix implements Scriptable { |
| 50 | |
| 51 | /** |
| 52 | * The zero-parameter constructor. |
| 53 | * |
| 54 | * <p>When ScriptableObject.defineClass is called with this class, it will construct |
| 55 | * Matrix.prototype using this constructor. |
| 56 | */ |
| 57 | public Matrix() {} |
| 58 | |
| 59 | /** |
| 60 | * The Java constructor, also used to define the JavaScript constructor. |
| 61 | * |
| 62 | * @param dimension the number of dimensions |
| 63 | */ |
| 64 | public Matrix(int dimension) { |
| 65 | if (dimension <= 0) { |
| 66 | throw Context.reportRuntimeError("Dimension of Matrix must be greater than zero"); |
| 67 | } |
| 68 | dim = dimension; |
| 69 | list = new ArrayList<Object>(); |
| 70 | } |
| 71 | |
| 72 | /** Returns the name of this JavaScript class, "Matrix". */ |
| 73 | @Override |
| 74 | public String getClassName() { |
| 75 | return "Matrix"; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Defines the "dim" property by returning true if name is equal to "dim". |
| 80 | * |
| 81 | * <p>Defines no other properties, i.e., returns false for all other names. |
| 82 | * |
| 83 | * @param name the name of the property |
| 84 | * @param start the object where lookup began |
| 85 | */ |
| 86 | @Override |
| 87 | public boolean has(String name, Scriptable start) { |
| 88 | return name.equals("dim"); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Defines all numeric properties by returning true. |
| 93 | * |
| 94 | * @param index the index of the property |
| 95 | * @param start the object where lookup began |
| 96 | */ |
| 97 | @Override |
| 98 | public boolean has(int index, Scriptable start) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the named property. |
| 104 | * |
| 105 | * <p>Handles the "dim" property and returns NOT_FOUND for all other names. |
| 106 | * |
nothing calls this directly
no outgoing calls
no test coverage detected