01: /*
02: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
07: *
08: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
09: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
10: * the license for the specific language governing your rights and limitations.
11: *
12: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
13: */
14: package org.pentaho.jfreereport.legacy;
15:
16: import org.jetbrains.annotations.NotNull;
17: import org.jfree.report.function.ColumnAggregationExpression;
18:
19: import java.math.BigDecimal;
20:
21: /**
22: * User: Martin
23: * Date: 26.06.2006
24: * Time: 18:50:02
25: */
26: @Deprecated
27: public class ColumnAverageExpression extends
28: ColumnAggregationExpression {
29: private static final int DEFAULT_SCALE = 14;
30:
31: private boolean onlyValidFields;
32:
33: public ColumnAverageExpression() {
34: }
35:
36: public boolean isOnlyValidFields() {
37: return onlyValidFields;
38: }
39:
40: public void setOnlyValidFields(final boolean onlyValidFields) {
41: this .onlyValidFields = onlyValidFields;
42: }
43:
44: /**
45: * Return the current expression value. <P> The value depends (obviously) on
46: * the expression implementation.
47: *
48: * @return the value of the function.
49: */
50: @NotNull
51: public Object getValue() {
52: Object[] values = getFieldValues();
53: BigDecimal computedResult = new BigDecimal("0");
54: int count = 0;
55: for (final Object value : values) {
56: if (value instanceof Number) {
57:
58: Number n = (Number) value;
59: //noinspection ObjectToString
60: computedResult = computedResult.add(new BigDecimal(n
61: .toString()));
62: count++;
63: }
64: }
65:
66: if (onlyValidFields) {
67: return computedResult.divide(new BigDecimal(count),
68: DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP)
69: .stripTrailingZeros();
70: }
71:
72: if (values.length > 0) {
73: return computedResult.divide(new BigDecimal(values.length),
74: DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP)
75: .stripTrailingZeros();
76: } else {
77: return BigDecimal.ZERO;
78: }
79: }
80: }
|