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: * ItemMaxFunction.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.Group;
032: import org.jfree.report.event.ReportEvent;
033: import org.jfree.util.Log;
034:
035: /**
036: * A report function that calculates the maximum value of one field (column) from the data-row.
037: * <p/>
038: * The function can be used in two ways: <ul> <li>to calculate a maximum value for the entire report;</li> <li>to
039: * calculate a maximum value within a particular group;</li> </ul> This function expects its input values to be either
040: * java.lang.Number instances or Strings that can be parsed to java.lang.Number instances using a
041: * java.text.DecimalFormat.
042: * <p/>
043: * The function undestands two parameters, the <code>field</code> parameter is required and denotes the name of an
044: * ItemBand-field which gets summed up.
045: * <p/>
046: * The parameter <code>group</code> denotes the name of a group. When this group is started, the counter gets reseted to
047: * null.
048: *
049: * @author Thomas Morgner
050: */
051: public class ItemMaxFunction extends AbstractFunction {
052: /**
053: * The name of the group on which to reset the count. This can be set to null to compute the maximum for the whole
054: * report.
055: */
056: private String group;
057: /**
058: * The name of the field from where to read the values.
059: */
060: private String field;
061: /**
062: * The maximum value.
063: */
064: private transient Comparable max;
065:
066: /**
067: * Constructs an unnamed function. Make sure to set a Name or function initialisation will fail.
068: */
069: public ItemMaxFunction() {
070: max = null;
071: }
072:
073: /**
074: * Constructs a named function. <P> The field must be defined before using the function.
075: *
076: * @param name The function name.
077: */
078: public ItemMaxFunction(final String name) {
079: this ();
080: setName(name);
081: }
082:
083: /**
084: * Receives notification that a new report is about to start. <P> Does nothing.
085: *
086: * @param event Information about the event.
087: */
088: public void reportInitialized(final ReportEvent event) {
089: this .max = null;
090: }
091:
092: /**
093: * Receives notification that a new group is about to start. If this is the group defined for the function, then the
094: * maximum value is reset to zero.
095: *
096: * @param event Information about the event.
097: */
098: public void groupStarted(final ReportEvent event) {
099: final String mygroup = getGroup();
100: if (mygroup == null) {
101: return;
102: }
103:
104: final Group group = event.getReport().getGroup(
105: event.getState().getCurrentGroupIndex());
106: if (getGroup().equals(group.getName())) {
107: this .max = null;
108: }
109: }
110:
111: /**
112: * Returns the group name.
113: *
114: * @return The group name.
115: */
116: public String getGroup() {
117: return group;
118: }
119:
120: /**
121: * Sets the group name. <P> If a group is defined, the maximum value is reset to zero at the start of every instance
122: * of this group.
123: *
124: * @param name The group name (null permitted).
125: */
126: public void setGroup(final String name) {
127: this .group = name;
128: }
129:
130: /**
131: * Returns the field used by the function. The field name corresponds to a column name in the report's data-row.
132: *
133: * @return The field name.
134: */
135: public String getField() {
136: return field;
137: }
138:
139: /**
140: * Sets the field name for the function. The field name corresponds to a column name in the report's data-row.
141: *
142: * @param field the field name.
143: */
144: public void setField(final String field) {
145: this .field = field;
146: }
147:
148: /**
149: * Receives notification that a row of data is being processed. Reads the data from the field defined for this
150: * function and performs the maximum value comparison with its old value.
151: *
152: * @param event Information about the event.
153: */
154: public void itemsAdvanced(final ReportEvent event) {
155: final Object fieldValue = event.getDataRow().get(getField());
156: if (fieldValue instanceof Comparable == false) {
157: return;
158: }
159:
160: try {
161: final Comparable compare = (Comparable) fieldValue;
162: if (max == null) {
163: max = compare;
164: } else if (max.compareTo(compare) < 0) {
165: max = compare;
166: }
167: } catch (Exception e) {
168: Log
169: .error("ItemMaxFunction.advanceItems(): problem comparing number.");
170: }
171: }
172:
173: /**
174: * Returns the function value, in this case the running total of a specific column in the report's data-set.
175: *
176: * @return The function value.
177: */
178: public Object getValue() {
179: return max;
180: }
181:
182: /**
183: * Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
184: * original function.
185: *
186: * @return a copy of this function.
187: */
188: public Expression getInstance() {
189: final ItemMaxFunction function = (ItemMaxFunction) super
190: .getInstance();
191: function.max = null;
192: return function;
193: }
194:
195: }
|