KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > PrintDataFunction


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.math.*;
17 import org.compiere.util.*;
18
19 /**
20  * Print Data Function
21  *
22  * @author Jorg Janke
23  * @version $Id: PrintDataFunction.java,v 1.4 2003/10/10 01:03:10 jjanke Exp $
24  */

25 public class PrintDataFunction
26 {
27     /**
28      * Constructor
29      */

30     public PrintDataFunction ()
31     {
32     } // PrintDataFunction
33

34     /** The Sum */
35     private BigDecimal m_sum = Env.ZERO;
36     /** The Count */
37     private int m_count = 0;
38     /** Total Count */
39     private int m_totalCount = 0;
40
41
42     static public final char F_SUM = 'S';
43     static public final char F_AVERAGE = 'A';
44     static public final char F_COUNT = 'C';
45
46     /** Function Keys */
47     static private final char[] FUNCTIONS = new char[]
48         {F_SUM, F_AVERAGE, F_COUNT};
49     /** Symbols */
50     static private final String JavaDoc[] FUNCTION_SYMBOLS = new String JavaDoc[]
51         {" \u2211", " \u00D8", " \u2116"};
52     /** AD_Message Names of Functions */
53     static private final String JavaDoc[] FUNCTION_NAMES = new String JavaDoc[]
54         {"Sum", "Average", "Count"};
55
56     /**
57      * Add Value to Counter
58      * @param bd data
59      */

60     public void addValue (BigDecimal bd)
61     {
62         if (bd != null)
63         {
64             m_sum = m_sum.add(bd);
65             m_count++;
66         }
67         m_totalCount++;
68     } // addValue
69

70     /**
71      * Get Function Value
72      * @param function function
73      * @return function value
74      */

75     public BigDecimal getValue(char function)
76     {
77         if (function == F_SUM)
78             return m_sum;
79         BigDecimal count = new BigDecimal(m_count);
80         if (function == F_COUNT)
81             return count;
82         // Average
83
if (m_count == 0)
84             return Env.ZERO;
85         BigDecimal result = m_sum.divide(count, BigDecimal.ROUND_HALF_UP);
86         if (result.scale() > 4)
87             result = result.setScale(4, BigDecimal.ROUND_HALF_UP);
88         return result;
89     } // getValue
90

91     /**
92      * Reset Value
93      */

94     public void reset()
95     {
96         m_count = 0;
97         m_totalCount = 0;
98         m_sum = Env.ZERO;
99     } // reset
100

101     /**
102      * String Representation
103      * @return info
104      */

105     public String JavaDoc toString()
106     {
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[");
108         sb.append("Count=").append(m_count).append(",Sum=").append(m_sum);
109         sb.append("]");
110         return sb.toString();
111     } // toString
112

113     /*************************************************************************/
114
115     /**
116      * Get Function Symbol of function
117      * @param function function
118      * @return function symbol
119      */

120     static public String JavaDoc getFunctionSymbol (char function)
121     {
122         for (int i = 0; i < FUNCTIONS.length; i++)
123         {
124             if (FUNCTIONS[i] == function)
125                 return FUNCTION_SYMBOLS[i];
126         }
127         return "UnknownFunction=" + function;
128     } // getFunctionSymbol
129

130     /**
131      * Get Function Name of function
132      * @param function function
133      * @return function name
134      */

135     static public String JavaDoc getFunctionName (char function)
136     {
137         for (int i = 0; i < FUNCTIONS.length; i++)
138         {
139             if (FUNCTIONS[i] == function)
140                 return FUNCTION_NAMES[i];
141         }
142         return "UnknownFunction=" + function;
143     } // getFunctionName
144

145     /**
146      * Get Funcuion Name of function
147      * @param function function
148      * @return function name
149      */

150     static public int getFunctionDisplayType (char function)
151     {
152         if (function == F_SUM)
153             return DisplayType.Amount;
154         if (function == F_COUNT)
155             return DisplayType.Integer;
156         // Average
157
return DisplayType.Number;
158     } // getFunctionName
159

160 } // PrintDataFunction
161
Popular Tags