Under the "src" folder of the plug-in project, create a Java™ class that extends from com.ghc.ghTester.expressions.Function (for example, com.samples.function.FormatDate).
Now the following methods need to be implemented:
Create a default, public constructor with an empty body. You can add other initialization as required, but it does not have to do anything, by default. The default constructor has to be public because of the way that functions are created.
public FormatDate() {
}
The create(int, Vector) method is a factory method that creates an instance of the particular function with the parameters provided. The vector of parameters needs to be treated as functions themselves and are likely to need processing at run time.
public Function create(int size, Vector params) {
Function outputFormat = null;
if (size == 3) {
outputFormat = (Function) params.get(2);
}
return new FormatDate((Function) params.get(0), (Function) params
.get(1), outputFormat);
}
The three-argument constructor that is used in the example can be found in the source that is provided in the examples folder of the Rational Integration Tester installation folder.
The evaluate(Object) method performs the actual work of the function. Rational Integration Tester passes the context of the current evaluation to your function. You can use this method to obtain the result from embedded functions and then return your function’s own result.
public Object evaluate(Object data) {
String date = m_fDate.evaluateAsString(data);
String inputFormat = m_fInputFormat.evaluateAsString(data);
String outputFormat = "yyyy-MM-dd"; // Default format
if (m_fOutputFormat != null) {
outputFormat = m_fOutputFormat.evaluateAsString(data);
}
//...
if (EvalUtils.isString(date)) {
date = EvalUtils.getString(date);
}
if (EvalUtils.isString(inputFormat)) {
inputFormat = EvalUtils.getString(inputFormat);
}
if (EvalUtils.isString(outputFormat)) {
outputFormat = EvalUtils.getString(outputFormat);
}
SimpleDateFormat inputFormatter = new
SimpleDateFormat(inputFormat);
String formattedDate = "";
try {
Date d = inputFormatter.parse(date);
SimpleDateFormat outputFormatter =new
SimpleDateFormat(outputFormat);
formattedDate = outputFormatter.format(d);
} catch (ParseException ex) {
// ...
}
return "\"" + formattedDate + "\"";
}