by Kasper B. Graversen, 2006-2007
%menuBlock
%menuBlockCodeExamples
%googleAddsTop
Code examples
Extending SuperCSV with new Cell processors
You can easily extend Super CSV if you need to. Just implement the interface
CellProcessor and you are set. Most of the
existing processors are around 10 lines of code. Let's investigate the implementations by looking at a cell processor which reads
a column and converts it into a
Long object. The cell processors are build around the patters "null object pattern" and
"chain of responsibility" hence all the infrastructure is set up outset the processors.
public class ParseLong extends CellProcessorAdaptor {
/** important to invoke super */
public ParseLong() {
super();
}
/** important to invoke super */
public ParseLong(final LongCellProcessor next) {
super(next);
}
/** simplify conversion of column to Long */
public Object execute(final Object value, final CSVContext context) throws NumberFormatException {
final Long result = Long.parseLong((String) value);
return next.execute(result, context);
}
}
All the magic is taking place in the
execute() method. It really need no further explaining.
%googleAddsBottom