001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import net.sf.jasperreports.engine.JRVariable;
031:
032: /**
033: * @author Teodor Danciu (teodord@users.sourceforge.net)
034: * @version $Id: JRByteIncrementerFactory.java 1347 2006-07-19 12:31:07Z teodord $
035: */
036: public class JRByteIncrementerFactory extends
037: JRAbstractExtendedIncrementerFactory {
038:
039: /**
040: *
041: */
042: protected static final Byte ZERO = new Byte((byte) 0);
043:
044: /**
045: *
046: */
047: private static JRByteIncrementerFactory mainInstance = new JRByteIncrementerFactory();
048:
049: /**
050: *
051: */
052: private JRByteIncrementerFactory() {
053: }
054:
055: /**
056: *
057: */
058: public static JRByteIncrementerFactory getInstance() {
059: return mainInstance;
060: }
061:
062: /**
063: *
064: */
065: public JRExtendedIncrementer getExtendedIncrementer(byte calculation) {
066: JRExtendedIncrementer incrementer = null;
067:
068: switch (calculation) {
069: case JRVariable.CALCULATION_COUNT: {
070: incrementer = JRByteCountIncrementer.getInstance();
071: break;
072: }
073: case JRVariable.CALCULATION_SUM: {
074: incrementer = JRByteSumIncrementer.getInstance();
075: break;
076: }
077: case JRVariable.CALCULATION_AVERAGE: {
078: incrementer = JRByteAverageIncrementer.getInstance();
079: break;
080: }
081: case JRVariable.CALCULATION_LOWEST:
082: case JRVariable.CALCULATION_HIGHEST: {
083: incrementer = JRComparableIncrementerFactory.getInstance()
084: .getExtendedIncrementer(calculation);
085: break;
086: }
087: case JRVariable.CALCULATION_STANDARD_DEVIATION: {
088: incrementer = JRByteStandardDeviationIncrementer
089: .getInstance();
090: break;
091: }
092: case JRVariable.CALCULATION_VARIANCE: {
093: incrementer = JRByteVarianceIncrementer.getInstance();
094: break;
095: }
096: case JRVariable.CALCULATION_DISTINCT_COUNT: {
097: incrementer = JRByteDistinctCountIncrementer.getInstance();
098: break;
099: }
100: case JRVariable.CALCULATION_SYSTEM:
101: case JRVariable.CALCULATION_NOTHING:
102: case JRVariable.CALCULATION_FIRST:
103: default: {
104: incrementer = JRDefaultIncrementerFactory.getInstance()
105: .getExtendedIncrementer(calculation);
106: break;
107: }
108: }
109:
110: return incrementer;
111: }
112:
113: }
114:
115: /**
116: *
117: */
118: class JRByteCountIncrementer extends JRAbstractExtendedIncrementer {
119: /**
120: *
121: */
122: private static JRByteCountIncrementer mainInstance = new JRByteCountIncrementer();
123:
124: /**
125: *
126: */
127: private JRByteCountIncrementer() {
128: }
129:
130: /**
131: *
132: */
133: public static JRByteCountIncrementer getInstance() {
134: return mainInstance;
135: }
136:
137: /**
138: *
139: */
140: public Object increment(JRCalculable variable,
141: Object expressionValue, AbstractValueProvider valueProvider) {
142: Number value = (Number) variable.getIncrementedValue();
143:
144: if (value == null || variable.isInitialized()) {
145: value = JRByteIncrementerFactory.ZERO;
146: }
147:
148: if (expressionValue == null) {
149: return value;
150: }
151:
152: return new Byte((byte) (value.byteValue() + 1));
153: }
154:
155: public Object combine(JRCalculable calculable,
156: JRCalculable calculableValue,
157: AbstractValueProvider valueProvider) {
158: Number value = (Number) calculable.getIncrementedValue();
159: Number combineValue = (Number) calculableValue.getValue();
160:
161: if (value == null || calculable.isInitialized()) {
162: value = JRByteIncrementerFactory.ZERO;
163: }
164:
165: if (combineValue == null) {
166: return value;
167: }
168:
169: return new Byte((byte) (value.byteValue() + combineValue
170: .byteValue()));
171: }
172:
173: public Object initialValue() {
174: return JRByteIncrementerFactory.ZERO;
175: }
176: }
177:
178: /**
179: *
180: */
181: class JRByteDistinctCountIncrementer extends
182: JRAbstractExtendedIncrementer {
183: /**
184: *
185: */
186: private static JRByteDistinctCountIncrementer mainInstance = new JRByteDistinctCountIncrementer();
187:
188: /**
189: *
190: */
191: private JRByteDistinctCountIncrementer() {
192: }
193:
194: /**
195: *
196: */
197: public static JRByteDistinctCountIncrementer getInstance() {
198: return mainInstance;
199: }
200:
201: /**
202: *
203: */
204: public Object increment(JRCalculable variable,
205: Object expressionValue, AbstractValueProvider valueProvider) {
206: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
207: .getValue(variable
208: .getHelperVariable(JRCalculable.HELPER_COUNT));
209:
210: if (variable.isInitialized()) {
211: holder.init();
212: }
213:
214: return new Byte((byte) holder.getCount());
215: }
216:
217: public Object combine(JRCalculable calculable,
218: JRCalculable calculableValue,
219: AbstractValueProvider valueProvider) {
220: DistinctCountHolder holder = (DistinctCountHolder) valueProvider
221: .getValue(calculable
222: .getHelperVariable(JRCalculable.HELPER_COUNT));
223:
224: return new Byte((byte) holder.getCount());
225: }
226:
227: public Object initialValue() {
228: return JRByteIncrementerFactory.ZERO;
229: }
230: }
231:
232: /**
233: *
234: */
235: class JRByteSumIncrementer extends JRAbstractExtendedIncrementer {
236: /**
237: *
238: */
239: private static JRByteSumIncrementer mainInstance = new JRByteSumIncrementer();
240:
241: /**
242: *
243: */
244: private JRByteSumIncrementer() {
245: }
246:
247: /**
248: *
249: */
250: public static JRByteSumIncrementer getInstance() {
251: return mainInstance;
252: }
253:
254: /**
255: *
256: */
257: public Object increment(JRCalculable variable,
258: Object expressionValue, AbstractValueProvider valueProvider) {
259: Number value = (Number) variable.getIncrementedValue();
260: Number newValue = (Number) expressionValue;
261:
262: if (newValue == null) {
263: if (variable.isInitialized()) {
264: return null;
265: }
266:
267: return value;
268: }
269:
270: if (value == null || variable.isInitialized()) {
271: value = JRByteIncrementerFactory.ZERO;
272: }
273:
274: return new Byte((byte) (value.byteValue() + newValue
275: .byteValue()));
276: }
277:
278: public Object initialValue() {
279: return JRByteIncrementerFactory.ZERO;
280: }
281: }
282:
283: /**
284: *
285: */
286: class JRByteAverageIncrementer extends JRAbstractExtendedIncrementer {
287: /**
288: *
289: */
290: private static JRByteAverageIncrementer mainInstance = new JRByteAverageIncrementer();
291:
292: /**
293: *
294: */
295: private JRByteAverageIncrementer() {
296: }
297:
298: /**
299: *
300: */
301: public static JRByteAverageIncrementer getInstance() {
302: return mainInstance;
303: }
304:
305: /**
306: *
307: */
308: public Object increment(JRCalculable variable,
309: Object expressionValue, AbstractValueProvider valueProvider) {
310: if (expressionValue == null) {
311: if (variable.isInitialized()) {
312: return null;
313: }
314: return variable.getValue();
315: }
316: Number countValue = (Number) valueProvider.getValue(variable
317: .getHelperVariable(JRCalculable.HELPER_COUNT));
318: Number sumValue = (Number) valueProvider.getValue(variable
319: .getHelperVariable(JRCalculable.HELPER_SUM));
320: return new Byte((byte) (sumValue.byteValue() / countValue
321: .byteValue()));
322: }
323:
324: public Object initialValue() {
325: return JRByteIncrementerFactory.ZERO;
326: }
327: }
328:
329: /**
330: *
331: */
332: class JRByteStandardDeviationIncrementer extends
333: JRAbstractExtendedIncrementer {
334: /**
335: *
336: */
337: private static JRByteStandardDeviationIncrementer mainInstance = new JRByteStandardDeviationIncrementer();
338:
339: /**
340: *
341: */
342: private JRByteStandardDeviationIncrementer() {
343: }
344:
345: /**
346: *
347: */
348: public static JRByteStandardDeviationIncrementer getInstance() {
349: return mainInstance;
350: }
351:
352: /**
353: *
354: */
355: public Object increment(JRCalculable variable,
356: Object expressionValue, AbstractValueProvider valueProvider) {
357: if (expressionValue == null) {
358: if (variable.isInitialized()) {
359: return null;
360: }
361: return variable.getValue();
362: }
363: Number varianceValue = (Number) valueProvider.getValue(variable
364: .getHelperVariable(JRCalculable.HELPER_VARIANCE));
365: return new Byte((byte) Math.sqrt(varianceValue.doubleValue()));
366: }
367:
368: public Object initialValue() {
369: return JRByteIncrementerFactory.ZERO;
370: }
371: }
372:
373: /**
374: *
375: */
376: class JRByteVarianceIncrementer extends JRAbstractExtendedIncrementer {
377: /**
378: *
379: */
380: private static JRByteVarianceIncrementer mainInstance = new JRByteVarianceIncrementer();
381:
382: /**
383: *
384: */
385: private JRByteVarianceIncrementer() {
386: }
387:
388: /**
389: *
390: */
391: public static JRByteVarianceIncrementer getInstance() {
392: return mainInstance;
393: }
394:
395: /**
396: *
397: */
398: public Object increment(JRCalculable variable,
399: Object expressionValue, AbstractValueProvider valueProvider) {
400: Number value = (Number) variable.getIncrementedValue();
401: Number newValue = (Number) expressionValue;
402:
403: if (newValue == null) {
404: if (variable.isInitialized()) {
405: return null;
406: }
407: return value;
408: } else if (value == null || variable.isInitialized()) {
409: return JRByteIncrementerFactory.ZERO;
410: } else {
411: Number countValue = (Number) valueProvider
412: .getValue(variable
413: .getHelperVariable(JRCalculable.HELPER_COUNT));
414: Number sumValue = (Number) valueProvider.getValue(variable
415: .getHelperVariable(JRCalculable.HELPER_SUM));
416: return new Byte(
417: (byte) ((countValue.byteValue() - 1)
418: * value.byteValue()
419: / countValue.byteValue() + (sumValue
420: .byteValue()
421: / countValue.byteValue() - newValue
422: .byteValue())
423: * (sumValue.byteValue()
424: / countValue.byteValue() - newValue
425: .byteValue())
426: / (countValue.byteValue() - 1)));
427: }
428: }
429:
430: public Object combine(JRCalculable calculable,
431: JRCalculable calculableValue,
432: AbstractValueProvider valueProvider) {
433: Number value = (Number) calculable.getIncrementedValue();
434:
435: if (calculableValue.getValue() == null) {
436: if (calculable.isInitialized()) {
437: return null;
438: }
439:
440: return value;
441: } else if (value == null || calculable.isInitialized()) {
442: return new Byte(((Number) calculableValue
443: .getIncrementedValue()).byteValue());
444: }
445:
446: float v1 = value.floatValue();
447: float c1 = ((Number) valueProvider.getValue(calculable
448: .getHelperVariable(JRCalculable.HELPER_COUNT)))
449: .floatValue();
450: float s1 = ((Number) valueProvider.getValue(calculable
451: .getHelperVariable(JRCalculable.HELPER_SUM)))
452: .floatValue();
453:
454: float v2 = ((Number) calculableValue.getIncrementedValue())
455: .floatValue();
456: float c2 = ((Number) valueProvider.getValue(calculableValue
457: .getHelperVariable(JRCalculable.HELPER_COUNT)))
458: .floatValue();
459: float s2 = ((Number) valueProvider.getValue(calculableValue
460: .getHelperVariable(JRCalculable.HELPER_SUM)))
461: .floatValue();
462:
463: c1 -= c2;
464: s1 -= s2;
465:
466: float c = c1 + c2;
467:
468: return new Byte((byte) (c1 / c * v1 + c2 / c * v2 + c2 / c1
469: * s1 / c * s1 / c + c1 / c2 * s2 / c * s2 / c - 2 * s1
470: / c * s2 / c));
471: }
472:
473: public Object initialValue() {
474: return JRByteIncrementerFactory.ZERO;
475: }
476: }
|