1 14 package org.compiere.print; 15 16 import java.math.*; 17 import org.compiere.util.*; 18 19 25 public class PrintDataFunction 26 { 27 30 public PrintDataFunction () 31 { 32 } 34 35 private BigDecimal m_sum = Env.ZERO; 36 37 private int m_count = 0; 38 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 47 static private final char[] FUNCTIONS = new char[] 48 {F_SUM, F_AVERAGE, F_COUNT}; 49 50 static private final String [] FUNCTION_SYMBOLS = new String [] 51 {" \u2211", " \u00D8", " \u2116"}; 52 53 static private final String [] FUNCTION_NAMES = new String [] 54 {"Sum", "Average", "Count"}; 55 56 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 } 70 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 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 } 91 94 public void reset() 95 { 96 m_count = 0; 97 m_totalCount = 0; 98 m_sum = Env.ZERO; 99 } 101 105 public String toString() 106 { 107 StringBuffer sb = new StringBuffer ("["); 108 sb.append("Count=").append(m_count).append(",Sum=").append(m_sum); 109 sb.append("]"); 110 return sb.toString(); 111 } 113 114 115 120 static public String 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 } 130 135 static public String 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 } 145 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 return DisplayType.Number; 158 } 160 } | Popular Tags |