-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
Slicer creates iterators that iterate over a series of rows in a grid.
The grid may be either a CSV file or a 2-dimensional array of String
s.
Slicer's asStrings()
iterator yields each row from the grid.
Each row is delivered as a List<String>
,
and includes an element for each cell in the row.
import static com.dhemery.slicer.Slicer.slice;
...
Iterator<List<String>> myIterator = slice(filename).asStrings();
A common use of Slicer is to repeatedly call a method,
passing the values from the spreadsheet to the method's parameters.
To get the values to pass to the method,
use Slicer's asParametersFor(Method)
iterator.
This iterator can convert the values to the appropriate types automatically.
You tell Slicer what method you will hand the values to,
and it converts them to the appropriate types.
import static com.dhemery.slicer.Slicer.slice;
...
Method m = ...; // Use reflection to locate the method.
Iterator<Object[]> myIterator = slice(filename).asParametersFor(m);
Each element of the array is of a type appropriate for the corresponding parameter of m
.
If a row includes more values than are needed to supply m
's parameters,
the values from any excess values are ignored.
Though the values have the appropriate types,
each row is still delivered as an array of Object
s.
When you pass the values to the method,
you will have to cast them to the appropriate parameter types.
Slicer knows how to deliver only the following types:
-
Boolean
, which you can pass to aBoolean
orboolean
parameter. -
Double
, which you can pass to aDouble
ordouble
parameter. -
Integer
, which you can pass to anInteger
orint
parameter. -
String
, which you can pass to aString
parameter.
If a method parameter is of any other type, Slicer will throw an exception. Or deliver a null value. Or do something else that you don't want.
This automatic conversion works only if the cells (in the CSV file or 2-dimensional array of Strings) have values that can be parsed as the appropriate types:
- Any
String
text can be converted toBoolean
orboolean
. If the text is the word "true" (regardless of case), the result istrue
. Otherwise, result isfalse
. - To convert to
Double
ordouble
, the cell value must be numeric. - To convert to
Integer
orint
, the cell value must be numeric. The conversion will truncate the value to 0 decimal places. - Any
String
text can be "converted" toString
. The result has the same value as the given text.