Class JSCSArray
- java.lang.Object
-
- com.jstatcom.model.AbstractJSCData
-
- com.jstatcom.model.JSCSArray
-
- All Implemented Interfaces:
- JSCData
public final class JSCSArray extends AbstractJSCData
A wrapper for string arrays that conforms to theJSCDatainterface.A
JSCSArrayis empty, if the stored array isnullor has either zero rows or columns. All rows of array values must have the same length.All array arguments are copied before they are set as values. The original arrays are untouched. In a similar way all
getmethods return either a copy of a mutable object (likeString[][]) or a reference to an immutable object.Whenever the value of this data object changes, registered
JSCDataListenersare notified via aJSCDataEvent. If not otherwise stated, the event objects always contain the values before the change and after the change.This class is thread-safe. All access to mutable data is synchronized. All events are fired outside the synchronized blocks.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classJSCSArray.RowInner class for storing rows of this array as XML elements.
-
Field Summary
Fields Modifier and Type Field and Description static XmlFormat<JSCSArray>JSCSArray_XMLXML format field for (de)serialization.
-
Constructor Summary
Constructors Constructor and Description JSCSArray(JSCSArray sarray)Copy-Constructor.JSCSArray(java.lang.String name)Creates an emptyJSCSArray.JSCSArray(java.lang.String name, java.lang.String... value)Creates aJSCSArrayfromvalue.JSCSArray(java.lang.String name, java.lang.String[][] value)Creates aJSCSArrayfromvalue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidappendCols(JSCSArray newCols)AppendsnewColsto the right end of the value array.voidappendRows(JSCSArray newRows)AppendsnewRowsto the bottom the value array.voidclear()Clearsthisdata object.intcols()Gets the number of columns in the stored array.JSCSArraycopy()Gets a copy of the current object.voiddelCol(int i)Deletes columnifrom the value array.voiddelColsIf(int[] index)Deletes all columns ofthisthat have a corresponding nonzero element inindex.voiddelRow(int i)Deletes rowifrom the value array.voiddelRowsIf(int[] index)Deletes all rows ofthisthat have a corresponding nonzero element inindex.java.lang.Stringdisplay()Gets a string representing theString[][]stored.java.lang.String[]getCol(int colIndex)Gets the column of the underlying string array specified withcolIndex.booleanisEmpty()Gets whetherthisis empty.booleanisEqual(JSCData o)Gets whether the values of this data object andotherare logically equal.java.lang.Stringname()Gets the name of the data object.introws()Gets the number of rows in the stored array.java.lang.String[][]selRowsIf(int[] rowIndex)Gets all rows of the underlying string array that have a nonzero corresponding element inrowIndex.voidsetFormatString(java.lang.String newFormatString)Sets a new format string.voidsetVal(JSCSArray val)Sets the values stored invalto thisJSCSArray.voidsetVal(java.lang.String... newValue)Sets the value of this tonewValue, seesetVal(String[][] newValue).voidsetVal(java.lang.String[][] newValue)Sets the value of this tonewValue.voidsetValAt(java.lang.String val, int i, int j)Sets theStringelement stored at index i, j.java.lang.String[][]stringArray()Gets a copy of the underlying value as aString[][].java.lang.StringstringAt(int i, int j)Gets theStringelement stored at index i, j.java.lang.StringtoString()Gets a string representation of thisJSCSArrayinstance.voidtranspose()Transposes the underlying array.JSCTypestype()Gets the type of thisJSCDataobject.java.lang.Objectvalue()Gets a copy of the underlying value, which is stored as aString[][].java.lang.String[]vec()Gets the columns of this object stacked into aString[].-
Methods inherited from class com.jstatcom.model.AbstractJSCData
addJSCDataListener, getJSCProperty, removeJSCDataListener, removeJSCProperty, setJSCProperty
-
-
-
-
Constructor Detail
-
JSCSArray
public JSCSArray(java.lang.String name)
Creates an emptyJSCSArray.- Parameters:
name- the identifier of that object- Throws:
java.lang.IllegalArgumentException- ifnameis not valid orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCSArray
public JSCSArray(java.lang.String name, java.lang.String[][] value)Creates aJSCSArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- theString[][]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid
or if rows ofvaluehave different lengths orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCSArray
public JSCSArray(JSCSArray sarray)
Copy-Constructor.- Parameters:
sarray- the string array to copy- Throws:
java.lang.IllegalArgumentException-if (sarray == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCSArray
public JSCSArray(java.lang.String name, java.lang.String... value)Creates aJSCSArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- theString[]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid
orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
-
Method Detail
-
appendCols
public void appendCols(JSCSArray newCols)
AppendsnewColsto the right end of the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewColsis not emptyJSCDataEventTypes.EMPTY_STATEifnewColsis not empty and this was empty before
newColsis empty, nothing happens.- Parameters:
newCols- theJSCSArrayto be appended- Throws:
java.lang.IllegalArgumentException-if (newCols == null)orif (rows() > 0 && newCols.rows() > 0 && rows() != newCols.rows())
-
appendRows
public void appendRows(JSCSArray newRows)
AppendsnewRowsto the bottom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewRowsis not emptyJSCDataEventTypes.EMPTY_STATEifnewRowsis not empty and this was empty before
newRowsis empty, nothing happens.- Parameters:
newRows- theJSCSArrayto be appended- Throws:
java.lang.IllegalArgumentException-if (newRows == null)orif (cols() > 0 && newRows.cols() > 0 && cols() != newRows.cols())
-
clear
public void clear()
Clearsthisdata object. Fires:JSCDataEventTypes.EMPTY_STATEeventJSCDataEventTypes.VALUE_CHANGEDevent
isEmptyreturnstrue.
-
cols
public int cols()
Gets the number of columns in the stored array.- Returns:
- the number of columns or 0
if (isEmpty())
-
copy
public JSCSArray copy()
Description copied from interface:JSCDataGets a copy of the current object. Implementations of this method must fulfil the two conditions:a.isEqual(a.copy())- compares valuesa.getName().equals(a.copy().getName())- compares name
JSCDataListenersattached to the original object should not be copied.- Returns:
- a clonelike copy of the original object
- See Also:
JSCData
-
delCol
public void delCol(int i)
Deletes columnifrom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDin any caseJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
i- the index of the column to remove- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException- if column index is invalid
-
delColsIf
public void delColsIf(int[] index)
Deletes all columns ofthisthat have a corresponding nonzero element inindex. Ifindexcontains only zeros, nothing happens. Ifindexselects all columns,thisis cleared.This method fires a
JSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDif some column was deletedJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
index- thecols() x 1 intarray, nonzero entries select columns- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (index.length != cols())orif (index == null)
-
delRow
public void delRow(int i)
Deletes rowifrom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDin any caseJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
i- the index of the row to remove- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException- if row index is invalid
-
delRowsIf
public void delRowsIf(int[] index)
Deletes all rows ofthisthat have a corresponding nonzero element inindex. Ifindexcontains only zeros, nothing happens. Ifindexselects all rows,thisis cleared.This method fires a
JSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDif some row was deletedJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
index- therows() x 1 intarray, nonzero entries select rows- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (index.length != rows())orif (index == null)
-
display
public java.lang.String display()
Gets a string representing theString[][]stored.- Specified by:
displayin interfaceJSCData- Overrides:
displayin classAbstractJSCData- Returns:
- "{}" if
isEmpty == true, otherwise a formatted string with all elements in table form, the format parameters can be changed viasetFormatString
-
isEmpty
public boolean isEmpty()
Gets whetherthisis empty. This method returnstrueif one of the following conditions is met:value() == nullrows() == 0cols() == 0
- Returns:
trueif empty
-
isEqual
public boolean isEqual(JSCData o)
Description copied from interface:JSCDataGets whether the values of this data object andotherare logically equal.- Returns:
trueif considered equal according to the logic of the representation- See Also:
JSCData
-
name
public java.lang.String name()
Description copied from interface:JSCDataGets the name of the data object. It must fulfil the requirements for variable names defined inJSCConstants.- Returns:
- the name
- See Also:
JSCData
-
rows
public int rows()
Gets the number of rows in the stored array.- Returns:
- the number of rows or 0
if (isEmpty())
-
setFormatString
public void setFormatString(java.lang.String newFormatString)
Sets a new format string. This string is used by thedisplaymethod to format the string array. The default format string is"%-12s ".For a complete description of all legal arguments, see the
PrintfFormatdocumentation. The string must contain exactly one conversion character.- Parameters:
newFormatString- the format string to be used fordisplay- Throws:
java.lang.IllegalArgumentException- ifnewFormatStringis not valid- See Also:
PrintfFormat
-
setVal
public void setVal(java.lang.String[][] newValue)
Sets the value of this tonewValue. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewValueis different from current valueJSCDataEventTypes.EMPTY_STATEifnewValueis not empty and this was empty beforeJSCDataEventTypes.EMPTY_STATEifnewValueis empty and this was not empty before
nullor at least one dimension is zero. ThenewValueis only compared to the existing value if a listener toJSCDataEventTypes.VALUE_CHANGEDis registered.- Parameters:
newValue- the new value- Throws:
java.lang.IllegalArgumentException- if rows ofnewValuehave different lengths
-
setVal
public void setVal(java.lang.String... newValue)
Sets the value of this tonewValue, seesetVal(String[][] newValue).- Parameters:
newValue- the new value
-
setVal
public void setVal(JSCSArray val)
Sets the values stored invalto thisJSCSArray. Future changes invalhave no effect onthis.- Parameters:
val- instance ofJSCSArray- Throws:
java.lang.IllegalArgumentException-if (val == null)
-
setValAt
public void setValAt(java.lang.String val, int i, int j)Sets theStringelement stored at index i, j. Fires aJSCDataEventof typeJSCDataEventTypes.VALUE_CHANGEDif the new value is different from the one currently stored at that position. The event object contains the following values:oldValue-new Object[] { oldString, new Integer(i), new Integer(j) }newValue-new Object[] { newString, new Integer(i), new Integer(j) }
- Parameters:
val- theStringto store at the given positioni- the row index, zero basedj- the column index, zero based- Throws:
java.lang.IllegalArgumentException- if indices out of range
-
stringArray
public java.lang.String[][] stringArray()
Gets a copy of the underlying value as aString[][].- Returns:
- the string array
ifthisis emptynew String[0][0]is returned
-
vec
public java.lang.String[] vec()
Gets the columns of this object stacked into aString[].- Returns:
- vectorized version of this
ifthisis emptynew String[0]is returned
-
stringAt
public java.lang.String stringAt(int i, int j)Gets theStringelement stored at index i, j.- Parameters:
i- the row index, zero basedj- the column index, zero based- Returns:
- the
Stringstored at this array position - Throws:
java.lang.IllegalArgumentException- if indices out of range
-
toString
public java.lang.String toString()
Gets a string representation of thisJSCSArrayinstance. The value is only represented with a summary string, because the underlying array might be large. To print the complete value, usedisplayinstead.- Overrides:
toStringin classAbstractJSCData- Returns:
- string with name, type and value information
-
type
public JSCTypes type()
Gets the type of thisJSCDataobject.- Returns:
JSCTypes.SARRAY- See Also:
JSCTypes
-
value
public java.lang.Object value()
Gets a copy of the underlying value, which is stored as aString[][].- Returns:
- the value object
ifthisis emptynew String[0][0]is returned
-
transpose
public void transpose()
Transposes the underlying array. Nothing happens ifthisis empty. Fires aJSCDataEventof typeJSCDataEventTypes.VALUE_CHANGEDif the stored array is not symmetric.
-
selRowsIf
public java.lang.String[][] selRowsIf(int[] rowIndex)
Gets all rows of the underlying string array that have a nonzero corresponding element inrowIndex.- Parameters:
rowIndex-this.rows() x 1vector selecting rows- Returns:
- new array with the extracted rows,
new string[0][0]if index contains only 0's, a copy of the underlying values ifrowIndexcontains only nonzero elements - Throws:
java.lang.IllegalArgumentException-if (rowIndex == null)orif (this.isEmpty() && rowIndex > 0)orif (this.rows() != rowIndex)
-
getCol
public java.lang.String[] getCol(int colIndex)
Gets the column of the underlying string array specified withcolIndex. The index starts with 0 as usual.- Parameters:
colIndex- the index of the column to extract (starting from 0)- Returns:
- a one dimensional string array with the column data
- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-colIndexis outside of valid array bounds
-
-
DMelt 3.0 © DataMelt by jWork.ORG