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: * ColumnDivisionExpression.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:
033: /**
034: * Divides all values read from the field-list. This is almost the same as the formula <code>[field1] / [field2] /
035: * field[3] / .. / [fieldn]</code>. Values that are non-numeric or null are ignored.
036: *
037: * @author Thomas Morgner
038: */
039: public class ColumnDivisionExpression extends
040: ColumnAggregationExpression {
041: /**
042: * The scale-property defines the precission of the divide-operation.
043: */
044: private int scale;
045: /**
046: * The rounding-property defines the precission of the divide-operation.
047: */
048: private int roundingMode;
049:
050: /**
051: * Default constructor.
052: */
053: public ColumnDivisionExpression() {
054: scale = 14;
055: roundingMode = BigDecimal.ROUND_HALF_UP;
056: }
057:
058: /**
059: * Returns the defined rounding mode. This influences the precision of the divide-operation.
060: *
061: * @return the rounding mode.
062: * @see java.math.BigDecimal#divide(java.math.BigDecimal, int)
063: */
064: public int getRoundingMode() {
065: return roundingMode;
066: }
067:
068: /**
069: * Defines the rounding mode. This influences the precision of the divide-operation.
070: *
071: * @param roundingMode the rounding mode.
072: * @see java.math.BigDecimal#divide(java.math.BigDecimal, int)
073: */
074: public void setRoundingMode(final int roundingMode) {
075: this .roundingMode = roundingMode;
076: }
077:
078: /**
079: * Returns the scale for the divide-operation. The scale influences the precision of the division.
080: *
081: * @return the scale.
082: */
083: public int getScale() {
084: return scale;
085: }
086:
087: /**
088: * Defines the scale for the divide-operation. The scale influences the precision of the division.
089: *
090: * @param scale the scale.
091: */
092: public void setScale(final int scale) {
093: this .scale = scale;
094: }
095:
096: /**
097: * Return the current expression value.
098: *
099: * @return the value of the function.
100: */
101: public Object getValue() {
102: final Object[] values = getFieldValues();
103: BigDecimal computedResult = null;
104:
105: for (int i = 0; i < values.length; i++) {
106: final Object value = values[i];
107: if (value instanceof Number == false) {
108: continue;
109: }
110:
111: final Number n = (Number) value;
112: final BigDecimal nval = new BigDecimal(n.toString());
113: if (computedResult == null) {
114: computedResult = nval;
115: if (n.doubleValue() == 0) {
116: // No matter what goes in next, we will always result in zero.
117: return n;
118: }
119: } else {
120: if (n.doubleValue() == 0) {
121: return null;
122: }
123: computedResult = computedResult.divide(nval, scale,
124: roundingMode);
125: }
126: }
127:
128: return computedResult;
129: }
130: }
|