Source Code Cross Referenced for AverageExpression.java in  » Report » pentaho-report » org » jfree » report » function » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » pentaho report » org.jfree.report.function 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.