2.17.2: 2011-06-17

net.sf.basedb.util.parser
Class ColFunction

java.lang.Object
  extended by net.sf.basedb.util.parser.ColFunction
All Implemented Interfaces:
JepFunction, org.nfunk.jep.function.PostfixMathCommandI

public class ColFunction
extends Object
implements JepFunction

Adds a col() function to Jep. The col function is used with the FlatFileParser to get the value of a data column. The argument to the col function is either the column name or index (0-based). Use the FlatFileParser.getMapper(String) with an argument starting with an equal sign to create a mapper that uses Jep.

FlatFileParser ffp = ...
Mapper diameterMapper = ffp.getMapper("=col('Radius') * 2");
while (ffp.hasMoreData())
{
        Data data = ffp.nextData();
        float diameter = diameterMapper.getFloat(data);
}

Version:
2.0
Author:
nicklas
Last modified
$Date: 2008-09-11 22:08:14 +0200 (Thu, 11 Sep 2008) $

Field Summary
private  Map<String,Integer> columnHeaders
           
private  FlatFileParser.Data data
           
private  boolean ignoreNonExistingColumns
           
private  NumberFormat numberFormat
           
private  ParsePosition pos
           
 
Constructor Summary
ColFunction(List<String> columnHeaders, NumberFormat numberFormat)
           
ColFunction(Map<String,Integer> columnHeaders, NumberFormat numberFormat)
           
 
Method Summary
 boolean checkNumberOfParameters(int n)
           
private  Integer findColumn(String regex)
          Find column index by checking each column header against a regular expression.
 String getFunctionName()
          Get the name of this function.
 int getNumberOfParameters()
           
 void run(Stack stack)
           
 void setCurNumberOfParameters(int n)
           
 void setData(FlatFileParser.Data data)
           
 void setIgnoreNonExistingColumns(boolean ignoreNonExistingColumns)
          Set to TRUE to ignore non-existing columns.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

columnHeaders

private final Map<String,Integer> columnHeaders

numberFormat

private final NumberFormat numberFormat

pos

private final ParsePosition pos

data

private FlatFileParser.Data data

ignoreNonExistingColumns

private boolean ignoreNonExistingColumns
Constructor Detail

ColFunction

public ColFunction(Map<String,Integer> columnHeaders,
                   NumberFormat numberFormat)

ColFunction

public ColFunction(List<String> columnHeaders,
                   NumberFormat numberFormat)
Method Detail

getFunctionName

public String getFunctionName()
Description copied from interface: JepFunction
Get the name of this function. It is used when registering the function with a JEP parser.

Specified by:
getFunctionName in interface JepFunction
Returns:
The string "col"
See Also:
JEP.addFunction(String, PostfixMathCommandI)

getNumberOfParameters

public int getNumberOfParameters()
Specified by:
getNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI
Returns:
Always 1

setCurNumberOfParameters

public void setCurNumberOfParameters(int n)
Specified by:
setCurNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI

checkNumberOfParameters

public boolean checkNumberOfParameters(int n)
Specified by:
checkNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI

run

public void run(Stack stack)
         throws ParseException
Specified by:
run in interface org.nfunk.jep.function.PostfixMathCommandI
Throws:
ParseException

setIgnoreNonExistingColumns

public void setIgnoreNonExistingColumns(boolean ignoreNonExistingColumns)
Set to TRUE to ignore non-existing columns. Set to FALSE to throw an exception (default).

Since:
2.6

setData

public void setData(FlatFileParser.Data data)

findColumn

private Integer findColumn(String regex)
Find column index by checking each column header against a regular expression. The first match found is returned. The map is updated with a new regex -> index entry.

Returns:
The column index or null if no column is found or if the string is not a valid regular expression

2.17.2: 2011-06-17