java.lang.Object
org.bzdev.util.JSArray
- Direct Known Subclasses:
ExpressionParser.ESPArray
,NJSArray
Simplified JavaScript-like array (or list) class.
Formats such as JSON are syntactically similar to the source code
for JavaScript arrays and JavaScript objects that contain
properties but not methods, to the source code for JavaScript
arrays, and the source code for numbers, strings, boolean values,
and (of course) the value
null
. The types of the
values that can be inserted into this object are JSObject
,
JSArray
, Boolean
, Number
, and String
.
The class JSUtilities.JSON
can be used to create instances
of JSObject
and JSArray
, given a variety of
sources, and these instances can then be used as the input for
various computation.
This class is based on the ArrayList
class, but
with some run-time type checking. Only a subset of the methods for
ArrayList
are implemented. In addition to
primitive values, entries in the list can be other instances of
this class or instances of JSArray
, allowing trees or
directed graphs to be constructed. Iterators will list the values
in the order of their indices.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
Exception indicating that a JSArray could not be converted to aTemplateProcessor.KeyMapList
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add an instance ofBoolean
.boolean
Add an instance ofNumber
.boolean
Add any allowed object.boolean
Add an instance ofString
.boolean
Add an instance ofJSArray
.boolean
Add an instance ofJSObject
.boolean
Add any allowed object.void
Performs the given action for each element of the array until all elements have been processed or the action throws an exception.get
(int index) Get the value stored in this object for a specified index.<T> T
Get the value stored in this object for a specified index, cast to a specified type.long
identity()
Get an integer identifying an instance of a class.iterator()
Get a possibly parallel stream with this array as its source.Set any allowed object at a specified index.Set any object at a specified index.int
size()
Get the number of elements in this array.stream()
Get astream
with this array as its source.Object[]
toArray()
Convert this object to an array.<T> T[]
Convert this object to an array, storing it in an array with a specified component type.boolean[]
Convert this object to an array of boolean.boolean[][]
Convert this object to a matrix whose components are of the primitive type boolean.double[]
Convert this object to an array of double.double[][]
Convert this object to a matrix whose components are of the primitive type double.int[]
Convert this object to an array of int.int[][]
Convert this object to a matrix whose components are of the primitive type int.Convert this JSArray to aTemplateProcessor.KeyMapList
.toKeyMapList
(String defaultKey) Convert this JSArray to aTemplateProcessor.KeyMapList
.long[]
Convert this object to an array of long.long[][]
Convert this object to a matrix whose components are of the primitive type long.Object[][]
toMatrix()
Convert this object to a matrix of type double.<T> T[][]
Convert this object to an matrix, storing it in a matrix with a specified component type.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.bzdev.util.JSOps
nextIdentity
-
Constructor Details
-
JSArray
public JSArray()Constructor. -
JSArray
Constructor sharing the same tables. This is used byNJSArray
.- Parameters:
base
- the JSArray whose tables should be used
-
-
Method Details
-
identity
public long identity()Description copied from interface:JSOps
Get an integer identifying an instance of a class. -
add
Add an instance ofJSObject
.- Parameters:
object
- the object to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
add
Add an instance ofJSArray
.- Parameters:
array
- the object to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
add
Add an instance ofNumber
.- Parameters:
value
- the number to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
add
Add an instance ofString
.- Parameters:
string
- the string to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
add
Add an instance ofBoolean
.- Parameters:
value
- the boolean value to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
add
Add any allowed object. The object's type must be assignable- Parameters:
object
- the object to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).- Throws:
IllegalArgumentException
- if the object has the wrong type
-
addObject
Add any allowed object. This is used byNJSArray
andObjectNamerLauncher
.- Parameters:
object
- the object to append to this array/list- Returns:
true
(as specified byCollection.add(E)
).
-
get
Get the value stored in this object for a specified index.- Parameters:
index
- the index- Returns:
- the object stored at the specified index
-
set
public Object set(int index, Object object) throws IllegalArgumentException, IndexOutOfBoundsException Set any allowed object at a specified index. This is used byNJSArray
.- Parameters:
index
- the indexobject
- the object to insert at the specified index- Returns:
- the previous object stored at the specified index
- Throws:
IndexOutOfBoundsException
- if index < 0 or index ≥ size()IllegalArgumentException
- if the object has the wrong type
-
setObject
Set any object at a specified index. This is used byNJSArray
andObjectNamerLauncher
.- Parameters:
index
- the indexobject
- the object to insert at the specified index- Returns:
- the previous object stored at the specified index
- Throws:
IndexOutOfBoundsException
- if index < 0 or index ≥ size()
-
size
public int size()Get the number of elements in this array. -
stream
Get astream
with this array as its source.- Returns:
- a stream with this array as its source
-
iterator
-
spliterator
- Specified by:
spliterator
in interfaceIterable<Object>
-
toArray
Convert this object to an array.- Returns:
- the array
-
toArray
Convert this object to an array, storing it in an array with a specified component type.- Type Parameters:
T
- the type provided by the classclasz
- Parameters:
clasz
- the class of the array components- Returns:
- the array
- Throws:
ArrayStoreException
- if the argument is not a superclass of the runtime type of this object's elements
-
toDoubleArray
Convert this object to an array of double.- Returns:
- the array
- Throws:
IllegalStateException
- the conversion failed
-
toLongArray
Convert this object to an array of long.- Returns:
- the array
- Throws:
IllegalStateException
- the conversion failed
-
toIntArray
Convert this object to an array of int.- Returns:
- the array
- Throws:
IllegalStateException
- the conversion failed
-
toBooleanArray
Convert this object to an array of boolean.- Returns:
- the array
- Throws:
IllegalStateException
- the conversion failed
-
toMatrix
Convert this object to a matrix of type double. A matrix is represented by a two-dimensional Java array where each row has the same number of elements.- Returns:
- the matrix.
- Throws:
IllegalStateException
- the conversion failed
-
toMatrix
Convert this object to an matrix, storing it in a matrix with a specified component type. The matrix is represented by a two-dimensional Java array and all rows must have the same number of elements.- Type Parameters:
T
- the type provided by the classclasz
- Parameters:
clasz
- the class of the matrix components- Returns:
- the array
- Throws:
IllegalStateException
- the conversion failed
-
toDoubleMatrix
Convert this object to a matrix whose components are of the primitive type double. A matrix is represented by a two-dimensional Java array where each row has the same number of elements.- Returns:
- the matrix.
- Throws:
IllegalStateException
- the conversion failed
-
toLongMatrix
Convert this object to a matrix whose components are of the primitive type long. A matrix is represented by a two-dimensional Java array where each row has the same number of elements.- Returns:
- the matrix.
- Throws:
IllegalStateException
- the conversion failed
-
toIntMatrix
Convert this object to a matrix whose components are of the primitive type int. A matrix is represented by a two-dimensional Java array where each row has the same number of elements.- Returns:
- the matrix.
- Throws:
IllegalStateException
- the conversion failed
-
toBooleanMatrix
Convert this object to a matrix whose components are of the primitive type boolean. A matrix is represented by a two-dimensional Java array where each row has the same number of elements.- Returns:
- the matrix.
- Throws:
IllegalStateException
- the conversion failed
-
parallelStream
Get a possibly parallel stream with this array as its source.- Returns:
- a stream with this array as its source
-
forEach
Performs the given action for each element of the array until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.Note: the description is copied from the
ArrayList
documentation. -
get
Get the value stored in this object for a specified index, cast to a specified type.- Type Parameters:
T
- the type provided by the classclasz
- Parameters:
index
- the indexclasz
- the class of the result- Returns:
- the object stored at the specified index
-
toKeyMapList
public TemplateProcessor.KeyMapList toKeyMapList(String defaultKey) throws JSArray.ConversionException Convert this JSArray to aTemplateProcessor.KeyMapList
. The elements of this list are processed as follows:- if the element is an instance of
JSObject
, that instance is converted to a key map by using the methodJSObject.toKeyMap()
. - if the element is an instance of
JSArray
, the conversion proceeds as follows:- A new key-map list will be created by calling
the element's method
toKeyMapList(defaultKey)
, wheredefaultKey
is the argument to this method. - The element will be replaced with a new key map
that contains a single entry. That entry's key
is
defaultKey
and its value is the new key-map list created in step 1. - The modified element is added to the resulting key-map list.
- A new key-map list will be created by calling
the element's method
- if the element is not an instance of
JSObject
and not an instance ofJSArray
, a new key map will be added and a single entry will be added to that new key map with a key equal to the default key and its value equal to the element itself.
JSArray
contains anotherJSArray
, all the elements that are not instances ofJSObject
must be instances ofJSArray
.This method is used internally for the case where an element in a
JSArray
is anotherJSArray
.- Parameters:
defaultKey
- the key to use in single-entry key maps- Returns:
- a key-map list containing those list elements that are instances of JSObject
- Throws:
JSArray.ConversionException
- the list contained elements that are instances ofJSArray
and also elements that are not instances ofJSObject
- See Also:
- if the element is an instance of
-
toKeyMapList
Convert this JSArray to aTemplateProcessor.KeyMapList
. Elements in the list that are not instances ofJSObject
are ignored.- Returns:
- a key-map list containing those list elements that are instances of JSObject.
-