org.jfree.report.function
public class: TotalGroupSumFunction [javadoc |
source]
java.lang.Object
org.jfree.report.function.AbstractExpression
org.jfree.report.function.AbstractFunction
org.jfree.report.function.TotalGroupSumFunction
All Implemented Interfaces:
Function, Expression, Serializable
A report function that calculates the sum of one field (column) from the Data-Row. This function produces a global
total. The total sum of the group is known when the group processing starts and the report is not performing a
prepare-run. The sum is calculated in the prepare run and recalled in the printing run.
The function can be used in two ways:
- to calculate a sum for the entire report;
- to calculate a sum
within a particular group;
This function expects its input values to be either java.lang.Number instances
or Strings that can be parsed to java.lang.Number instances using a java.text.DecimalFormat.
The function undestands two parameters, the
field parameter is required and denotes the name of an
ItemBand-field which gets summed up.
The parameter
group denotes the name of a group. When this group is started, the counter gets reseted to
null. This parameter is optional.
| Methods from org.jfree.report.function.AbstractExpression: |
|---|
|
clone, getDataRow, getDependencyLevel, getInstance, getName, getReportConfiguration, getResourceBundleFactory, getRuntime, isActive, isDeepTraversing, isPreserve, setActive, setDependencyLevel, setName, setPreserve, setRuntime |
| Method from org.jfree.report.function.TotalGroupSumFunction Detail: |
public String getField() {
return field;
}
Returns the field used by the function. The field name corresponds to a column name in the report's data-row. |
public String getGroup() {
return group;
}
Returns the name of the group to be totalled. |
public Expression getInstance() {
final TotalGroupSumFunction function = (TotalGroupSumFunction) super.getInstance();
function.result = null;
function.results = new HashMap();
return function;
}
Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
original function. |
public Object getValue() {
return result;
}
Return the current function value. The value depends (obviously) on the function implementation. For example,
a page counting function will return the current page number. |
public void groupStarted(ReportEvent event) {
if (FunctionUtilities.isDefinedGroup(getGroup(), event) == false)
{
// wrong group ...
return;
}
groupStateKey = event.getState().getProcessKey();
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event))
{
result = new BigDecimal(0);
results.put(globalStateKey, result);
results.put(groupStateKey, result);
}
else
{
// Activate the current group, which was filled in the prepare run.
result = (BigDecimal) results.get(groupStateKey);
}
}
Receives notification that a group has started. |
public void itemsAdvanced(ReportEvent event) {
if (field == null)
{
return;
}
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event) == false)
{
return;
}
final Object fieldValue = event.getDataRow().get(getField());
try
{
final Number n = (Number) fieldValue;
if (n instanceof BigDecimal)
{
final BigDecimal bd = (BigDecimal) n;
result = result.add(bd);
results.put(globalStateKey, result);
if (groupStateKey != null)
{
results.put(groupStateKey, result);
}
}
else if (n instanceof Number)
{
result = result.add(new BigDecimal(String.valueOf(n)));
results.put(globalStateKey, result);
if (groupStateKey != null)
{
results.put(groupStateKey, result);
}
}
}
catch (Exception e)
{
TotalGroupSumFunction.logger.error("TotalGroupSumFunction.itemsAdvanced(): problem adding number." + fieldValue, e);
}
}
Receives notification that a row of data is being processed. |
public void reportInitialized(ReportEvent event) {
globalStateKey = event.getState().getProcessKey();
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event))
{
results.clear();
result = new BigDecimal(0);
results.put(globalStateKey, result);
}
else
{
result = (BigDecimal) results.get(globalStateKey);
}
}
Receives notification that the report has started. |
public void setField(String field) {
this.field = field;
}
Sets the field name for the function. The field name corresponds to a column name in the report's data-row. |
public void setGroup(String group) {
this.group = group;
}
Defines the name of the group to be totalled. If the name is null, all groups are totalled. |