The following lines apply to both functions and expressions.
JFreeReport supports 2 kinds of userdefined calculations: Functions are statefull calculations, when the report proceedes they change their state, they sum up, calculate averages, count etc. Statefull means in that case, given you feed the same event multiple times into the function,you are not guaranteed to get the same result.
Expressions in contrast do not maintain a state, they calculate a value or change the report-elements depending on the current Event feed to them. If you feed the same Event into the Expression multiple times, you will always get the same result.
Common to both variants is: Data is fed into the function/expression by using a DataRow object. This object provides the (among others) these methods
Object get(String name);
Object get(int columnName);
Expressions are simple and easily explained: Expression have the method
Object getValue()
which is called when the expression is queried.
All expression have to override this method to perform the calculation and to return
the value.
Functions are more complex:
Functions have several notification methods, where the system informs the function
that a new event is processed.
public void pageStarted (ReportEvent event);
public void reportStarted (ReportEvent event);
public void groupStarted (ReportEvent event);
public void itemsStarted (ReportEvent event);
public void itemsAdvanced (ReportEvent event);
public void itemsFinished (ReportEvent event);
public void groupFinished (ReportEvent event);
public void reportFinished (ReportEvent event);
public void pageFinished (ReportEvent event);
Functions live in an own environment, separated from the outside world. Changing the original JFreeReport object does not affect the inner JFreeReport-Object. You are also unable to add new Functions or expressions to the report from inside, so all functions and expression have to be set and initialized before the report processing has begun.
All Functions have to be cloneable. It is not guaranteed that the report is processed in an linear order, but it is guaranteed that a single instance of a function does pass a reportstate only once.
That means: When processing the report, the system clones the functions on certain save-points (for instance whenever a page is finished). This saved copy can later be used to restart the report processing on that saved point. The copy-state made will never return to a previous save point, but it can (and certainly will occur) that this copy is used to process the report from that saved point on.
So when saving a state after page 2, it will never happen, that this page-2-copy gets used to process page 2 a second time, but it could happen that copies of this page-2-state process the page 3 again and again.
The best way to avoid cloning problems is to use Unmodifiable objects or primitive
datatypes when storing a state. Using java.lang.String, one of the java.lang.Number
implementers or any other unmodifiable object type is always save. If you use complex
objects, lists or hashtables and you get weird results, try to implement a
deep-object-copy in the clone()
method of the function for these objects.
Using the functions
The function implementation is loaded via its default constructor by the report parser, all function implementations which should be used from within the xml-definitions must define a public default constructor. The functions implementation is defined by specifiying the "class" attribute of the "function" tag and is loaded by using the default class loader. Your function implementation must be in the classloaders classpath.
As with every function, a single function can return one value, something mathematicans express like y = f(x) where x is the data from your report processing.
A function is called several times, for every state change (whenever the report advances) one of the ReportListener methods are called (reportStarted, groupStarted etc). Expressions do not receive events, they just perform their computations based on the current state.
The functions value is returned by the "getValue()" function. You can return any Object, but make sure, that this object does not get modified anymore after it was returned to the caller. (A simple way to obey to this rule is to create a new object whenever the computed object changes).
Functions may reuse results from other functions by using and querying the DataRow object supplied in the report state or the function.
The order of the functions and the order for receiving events is undefined for functions of the same dependency level. So if you define a function which reads values from an other function, it is wise to define the dependencies for these functions.
Defining dependencies is easy: A function with an higher dependency is called before any function with a lower dependency. By default all user functions have the dependency of "0", the lowest possible level. The dependency is defined by specifying the "deplevel" attribute of the function or expression tag.
When using the API, the dependency level is read by "getDependencyLevel()" and can be set with "setDependencyLevel(int)".
For the example above, the caller function would have the dependency level of '0' and the called function the level of '1' or higher.