Skip to content
dhemery edited this page Apr 24, 2011 · 16 revisions

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 Strings.

Get one row at a time as a list of Strings.

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();

Deliver values for use as parameters to a method

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.

Notes on Slicer's automatic type conversion

Though the values have the appropriate types, each row is still delivered as an array of Objects. 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 a Boolean or boolean parameter.
  • Double, which you can pass to a Double or double parameter.
  • Integer, which you can pass to an Integer or int parameter.
  • String, which you can pass to a String 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 to Boolean or boolean. If the text is the word "true" (regardless of case), the result is true. Otherwise, result is false.
  • To convert to Double or double, the cell value must be numeric.
  • To convert to Integer or int, the cell value must be numeric. The conversion will truncate the value to 0 decimal places.
  • Any String text can be "converted" to String. The result has the same value as the given text.