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: * AverageExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.math.BigDecimal;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034:
035: /**
036: * An expression that takes values from one or more fields and returns the average of
037: * them.
038: *
039: * @deprecated this has been replaced by the ColumnAverageExpression.
040: * @author Thomas Morgner
041: */
042: public class AverageExpression extends AbstractExpression {
043: /**
044: * An ordered list containing the fieldnames used in the expression.
045: */
046: private ArrayList fieldList;
047: /**
048: * The scale-property defines the precission of the divide-operation.
049: */
050: private int scale;
051: /**
052: * The rounding-property defines the precission of the divide-operation.
053: */
054: private int roundingMode;
055:
056: /**
057: * Creates a new expression. The fields used by the expression are defined using
058: * properties named '0', '1', ... 'N'. These fields should contain {@link Number}
059: * instances.
060: */
061: public AverageExpression() {
062: this .fieldList = new ArrayList();
063: scale = 14;
064: roundingMode = BigDecimal.ROUND_HALF_UP;
065: }
066:
067: /**
068: * Returns the defined rounding mode. This influences the precision of the divide-operation.
069: *
070: * @return the rounding mode.
071: * @see java.math.BigDecimal#divide(java.math.BigDecimal,int)
072: */
073: public int getRoundingMode() {
074: return roundingMode;
075: }
076:
077: /**
078: * Defines the rounding mode. This influences the precision of the divide-operation.
079: *
080: * @param roundingMode the rounding mode.
081: * @see java.math.BigDecimal#divide(java.math.BigDecimal,int)
082: */
083: public void setRoundingMode(final int roundingMode) {
084: this .roundingMode = roundingMode;
085: }
086:
087: /**
088: * Returns the scale for the divide-operation. The scale influences the precision of the division.
089: *
090: * @return the scale.
091: */
092: public int getScale() {
093: return scale;
094: }
095:
096: /**
097: * Defines the scale for the divide-operation. The scale influences the precision of the division.
098: *
099: * @param scale the scale.
100: */
101: public void setScale(final int scale) {
102: this .scale = scale;
103: }
104:
105: /**
106: * Returns the average of the values.
107: *
108: * @return a BigDecimal instance.
109: */
110: public Object getValue() {
111: final Number[] values = collectValues();
112: BigDecimal total = new BigDecimal(0.0);
113: int count = 0;
114: for (int i = 0; i < values.length; i++) {
115: final Number n = values[i];
116: if (n != null) {
117: total = total.add(new BigDecimal(n.toString()));
118: count++;
119: }
120: }
121: if (count > 0) {
122: return total.divide(new BigDecimal(count), scale,
123: roundingMode);
124: }
125: return new BigDecimal(0.0);
126: }
127:
128: /**
129: * collects the values of all fields defined in the fieldList.
130: *
131: * @return an Objectarray containing all defined values from the datarow
132: */
133: private Number[] collectValues() {
134: final Number[] retval = new Number[this .fieldList.size()];
135: for (int i = 0; i < this .fieldList.size(); i++) {
136: final String field = (String) this .fieldList.get(i);
137: retval[i] = (Number) getDataRow().get(field);
138: }
139: return retval;
140: }
141:
142: /**
143: * Clones the expression.
144: *
145: * @return A copy of this expression.
146: */
147: public Expression getInstance() {
148: final AverageExpression ae = (AverageExpression) super
149: .getInstance();
150: ae.fieldList = (ArrayList) fieldList.clone();
151: return ae;
152: }
153:
154: /**
155: * Returns the defined fields as array.
156: *
157: * @return the fields
158: */
159: public String[] getField() {
160: return (String[]) fieldList
161: .toArray(new String[fieldList.size()]);
162: }
163:
164: /**
165: * Defines all fields as array. This completely replaces any previously defined fields.
166: *
167: * @param fields the new list of fields.
168: */
169: public void setField(final String[] fields) {
170: this .fieldList.clear();
171: this .fieldList.addAll(Arrays.asList(fields));
172: }
173:
174: /**
175: * Returns the defined field at the given index-position.
176: *
177: * @param index the position of the field name that should be queried.
178: * @return the field name at the given position.
179: */
180: public String getField(final int index) {
181: return (String) this .fieldList.get(index);
182: }
183:
184: /**
185: * Defines the field in the field-list at the given index.
186: *
187: * @param index the position in the list, where the field should be defined.
188: * @param field the name of the field.
189: */
190: public void setField(final int index, final String field) {
191: if (this .fieldList.size() == index) {
192: this .fieldList.add(field);
193: } else {
194: this .fieldList.set(index, field);
195: }
196: }
197:
198: /**
199: * Returns the number of fields defined in this expression.
200: * @return the number of fields.
201: */
202: public int getFieldCount() {
203: return fieldList.size();
204: }
205: }
|