Skip to content

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml authored and gbrail committed Oct 15, 2021
1 parent edbd080 commit d9c0e40
Show file tree
Hide file tree
Showing 10 changed files with 1,795 additions and 2,335 deletions.
117 changes: 45 additions & 72 deletions examples/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@

import java.util.ArrayList;
import java.util.List;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
* 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 index that
* is accessed. What's more, the Matrix constructor takes a integer argument
* that specifies the dimension of the Matrix. If m is a Matrix with dimension 3,
* <p>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 index that is accessed. What's more, the Matrix constructor takes a
* integer argument that specifies the dimension of the Matrix. If m is a Matrix with dimension 3,
* then m[0] will be a Matrix with dimension 1, and m[0][0] will be an Array.
*
* Here's a shell session showing the Matrix object in action:
* <p>Here's a shell session showing the Matrix object in action:
*
* <pre>
* js> defineClass("Matrix")
* js> var m = new Matrix(2); // A constructor call, see "Matrix(int dimension)"
Expand All @@ -43,46 +42,37 @@
*
* @see org.mozilla.javascript.Context
* @see org.mozilla.javascript.Scriptable
*
* @author Norris Boyd
*/
public class Matrix implements Scriptable {

/**
* The zero-parameter constructor.
*
* When ScriptableObject.defineClass is called with this class, it will
* construct Matrix.prototype using this constructor.
* <p>When ScriptableObject.defineClass is called with this class, it will construct
* Matrix.prototype using this constructor.
*/
public Matrix() {
}
public Matrix() {}

/**
* The Java constructor, also used to define the JavaScript constructor.
*/
/** The Java constructor, also used to define the JavaScript constructor. */
public Matrix(int dimension) {
if (dimension <= 0) {
throw Context.reportRuntimeError(
"Dimension of Matrix must be greater than zero");
throw Context.reportRuntimeError("Dimension of Matrix must be greater than zero");
}
dim = dimension;
list = new ArrayList<Object>();
}

/**
* Returns the name of this JavaScript class, "Matrix".
*/
/** Returns the name of this JavaScript class, "Matrix". */
@Override
public String getClassName() {
return "Matrix";
}

/**
* Defines the "dim" property by returning true if name is
* equal to "dim".
* <p>
* Defines no other properties, i.e., returns false for
* all other names.
* Defines the "dim" property by returning true if name is equal to "dim".
*
* <p>Defines no other properties, i.e., returns false for all other names.
*
* @param name the name of the property
* @param start the object where lookup began
Expand All @@ -105,25 +95,27 @@ public boolean has(int index, Scriptable start) {

/**
* Get the named property.
* <p>
* Handles the "dim" property and returns NOT_FOUND for all
* other names.
*
* <p>Handles the "dim" property and returns NOT_FOUND for all other names.
*
* @param name the property name
* @param start the object where the lookup began
*/
@Override
public Object get(String name, Scriptable start) {
if (name.equals("dim"))
return Integer.valueOf(dim);
if (name.equals("dim")) return Integer.valueOf(dim);

return NOT_FOUND;
}

/**
* Get the indexed property.
*
* <p>Look up the element in the associated list and return it if it exists. If it doesn't
* exist, create it.
*
* <p>
* Look up the element in the associated list and return
* it if it exists. If it doesn't exist, create it.<p>
*
* @param index the index of the integral property
* @param start the object where the lookup began
*/
Expand All @@ -133,10 +125,9 @@ public Object get(int index, Scriptable start) {
list.add(null);
}
Object result = list.get(index);
if (result != null)
return result;
if (result != null) return result;
if (dim > 2) {
Matrix m = new Matrix(dim-1);
Matrix m = new Matrix(dim - 1);
m.setParentScope(getParentScope());
m.setPrototype(getPrototype());
result = m;
Expand All @@ -152,68 +143,54 @@ public Object get(int index, Scriptable start) {
/**
* Set a named property.
*
* We do nothing here, so all properties are effectively read-only.
* <p>We do nothing here, so all properties are effectively read-only.
*/
@Override
public void put(String name, Scriptable start, Object value) {
}
public void put(String name, Scriptable start, Object value) {}

/**
* Set an indexed property.
*
* We do nothing here, so all properties are effectively read-only.
* <p>We do nothing here, so all properties are effectively read-only.
*/
@Override
public void put(int index, Scriptable start, Object value) {
}
public void put(int index, Scriptable start, Object value) {}

/**
* Remove a named property.
*
* This method shouldn't even be called since we define all properties
* as PERMANENT.
* <p>This method shouldn't even be called since we define all properties as PERMANENT.
*/
@Override
public void delete(String id) {
}
public void delete(String id) {}

/**
* Remove an indexed property.
*
* This method shouldn't even be called since we define all properties
* as PERMANENT.
* <p>This method shouldn't even be called since we define all properties as PERMANENT.
*/
@Override
public void delete(int index) {
}
public void delete(int index) {}

/**
* Get prototype.
*/
/** Get prototype. */
@Override
public Scriptable getPrototype() {
return prototype;
}

/**
* Set prototype.
*/
/** Set prototype. */
@Override
public void setPrototype(Scriptable prototype) {
this.prototype = prototype;
}

/**
* Get parent.
*/
/** Get parent. */
@Override
public Scriptable getParentScope() {
return parent;
}

/**
* Set parent.
*/
/** Set parent. */
@Override
public void setParentScope(Scriptable parent) {
this.parent = parent;
Expand All @@ -222,7 +199,7 @@ public void setParentScope(Scriptable parent) {
/**
* Get properties.
*
* We return an empty array since we define all properties to be DONTENUM.
* <p>We return an empty array since we define all properties to be DONTENUM.
*/
@Override
public Object[] getIds() {
Expand All @@ -232,8 +209,7 @@ public Object[] getIds() {
/**
* Default value.
*
* Use the convenience method from Context that takes care of calling
* toString, etc.
* <p>Use the convenience method from Context that takes care of calling toString, etc.
*/
@Override
public Object getDefaultValue(Class<?> typeHint) {
Expand All @@ -243,26 +219,23 @@ public Object getDefaultValue(Class<?> typeHint) {
/**
* instanceof operator.
*
* We mimick the normal JavaScript instanceof semantics, returning
* true if <code>this</code> appears in <code>value</code>'s prototype
* chain.
* <p>We mimick the normal JavaScript instanceof semantics, returning true if <code>this</code>
* appears in <code>value</code>'s prototype chain.
*/
@Override
public boolean hasInstance(Scriptable value) {
Scriptable proto = value.getPrototype();
while (proto != null) {
if (proto.equals(this))
return true;
if (proto.equals(this)) return true;
proto = proto.getPrototype();
}

return false;
}

/**
* Some private data for this class.
*/
/** Some private data for this class. */
private int dim;

private List<Object> list;
private Scriptable prototype, parent;
}
13 changes: 4 additions & 9 deletions examples/RunScript4.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import org.mozilla.javascript.ScriptableObject;

/**
* RunScript4: Execute scripts in an environment that includes the
* example Counter class.
* RunScript4: Execute scripts in an environment that includes the example Counter class.
*
* @author Norris Boyd
*/
public class RunScript4 {
public static void main(String args[])
throws Exception
{
public static void main(String args[]) throws Exception {
Context cx = Context.enter();
try {
Scriptable scope = cx.initStandardObjects();
Expand All @@ -30,12 +27,12 @@ public static void main(String args[])
// the top-level variable "myCounter". This is
// equivalent to the JavaScript code
// myCounter = new Counter(7);
Object[] arg = { Integer.valueOf(7) };
Object[] arg = {Integer.valueOf(7)};
Scriptable myCounter = cx.newObject(scope, "Counter", arg);
scope.put("myCounter", scope, myCounter);

String s = "";
for (int i=0; i < args.length; i++) {
for (int i = 0; i < args.length; i++) {
s += args[i];
}
Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
Expand All @@ -44,6 +41,4 @@ public static void main(String args[])
Context.exit();
}
}

}

Loading

0 comments on commit d9c0e40

Please sign in to comment.