001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ConditionalItemSumFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import org.jfree.report.event.ReportEvent;
032: import org.jfree.util.ObjectUtilities;
033:
034: /**
035: * A item sum function that only sums up the current value, if the value read from the conditionField is the same as the
036: * value from the conditionValue property.
037: *
038: * @author Thomas Morgner
039: */
040: public class ConditionalItemSumFunction extends ItemSumFunction {
041: /**
042: * The name of the data-row column from where to read the comparison value for the condition.
043: */
044: private String conditionField;
045: /**
046: * The static comparison value for the condition.
047: */
048: private Object conditionValue;
049:
050: /**
051: * Default Constructor.
052: */
053: public ConditionalItemSumFunction() {
054: }
055:
056: /**
057: * Returns the name of the data-row column from where to read the comparison value for the condition.
058: *
059: * @return a field name.
060: */
061: public String getConditionField() {
062: return conditionField;
063: }
064:
065: /**
066: * Defines the name of the data-row column from where to read the comparison value for the condition.
067: *
068: * @param conditionField a field name.
069: */
070: public void setConditionField(final String conditionField) {
071: this .conditionField = conditionField;
072: }
073:
074: /**
075: * Returns the static comparison value for the condition.
076: *
077: * @return the static value.
078: */
079: public Object getConditionValue() {
080: return conditionValue;
081: }
082:
083: /**
084: * Defines the static comparison value for the condition.
085: *
086: * @param conditionValue the static value.
087: */
088: public void setConditionValue(final Object conditionValue) {
089: this .conditionValue = conditionValue;
090: }
091:
092: /**
093: * Receives notification that a row of data is being processed.
094: *
095: * @param event Information about the event.
096: */
097: public void itemsAdvanced(final ReportEvent event) {
098: if (getConditionField() == null) {
099: return;
100: }
101:
102: final Object currentValue = getDataRow().get(
103: getConditionField());
104: // ObjectUtils-equal does not crash if both values are 'null'.
105: // You could use the ordinary equals as well, but thats more code to write
106: if (ObjectUtilities.equal(currentValue, getConditionValue())) {
107: // this does the addition of the values.
108: super.itemsAdvanced(event);
109: }
110: }
111: }
|