KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRDistinctCountExtendedIncrementerFactory


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
32
33
34 /**
35  * @author Teodor Danciu (teodord@users.sourceforge.net)
36  * @version $Id: JRDistinctCountExtendedIncrementerFactory.java 1311 2006-06-23 12:19:26 +0300 (Fri, 23 Jun 2006) teodord $
37  */

38 public class JRDistinctCountExtendedIncrementerFactory extends JRAbstractExtendedIncrementerFactory
39 {
40
41
42     /**
43      *
44      */

45     private static JRDistinctCountExtendedIncrementerFactory mainInstance = new JRDistinctCountExtendedIncrementerFactory();
46
47
48     /**
49      *
50      */

51     public JRDistinctCountExtendedIncrementerFactory()
52     {
53     }
54
55
56     /**
57      *
58      */

59     public static JRDistinctCountExtendedIncrementerFactory getInstance()
60     {
61         return mainInstance;
62     }
63
64
65     /**
66      *
67      */

68     public JRExtendedIncrementer getExtendedIncrementer(byte calculation)
69     {
70         return new JRDistinctCountExtendedIncrementer();
71     }
72
73
74     public static JRExtendedIncrementerFactory getFactory (Class JavaDoc valueClass)
75     {
76         return JRDistinctCountExtendedIncrementerFactory.getInstance();
77     }
78 }
79
80
81 /**
82  *
83  */

84 class JRDistinctCountExtendedIncrementer extends JRAbstractExtendedIncrementer
85 {
86
87     private DistinctCountHolder lastHolder = new DistinctCountHolder();
88
89     
90     /**
91      *
92      */

93     public JRDistinctCountExtendedIncrementer()
94     {
95     }
96
97
98     /**
99      *
100      */

101     public Object JavaDoc increment(
102         JRCalculable variable,
103         Object JavaDoc expressionValue,
104         AbstractValueProvider valueProvider
105         )
106     {
107         DistinctCountHolder holder = (DistinctCountHolder)variable.getIncrementedValue();
108
109         if (holder == null)
110         {
111             holder = lastHolder;
112         }
113         else
114         {
115             lastHolder = holder;
116         }
117         
118         holder.addLastValue();
119
120         return new DistinctCountHolder(holder, expressionValue);
121     }
122
123
124     /**
125      *
126      */

127     public Object JavaDoc combine(JRCalculable calculable1, JRCalculable calculable2, AbstractValueProvider valueProvider)
128     {
129         Set JavaDoc distinctValues = new HashSet JavaDoc();
130         
131         DistinctCountHolder holder1 = (DistinctCountHolder)calculable1.getValue();
132         if (holder1 != null)
133         {
134             distinctValues.addAll(holder1.getDistinctValues());
135             if (holder1.getLastValue() != null)
136                 distinctValues.add(holder1.getLastValue());
137         }
138         
139         DistinctCountHolder holder2 = (DistinctCountHolder)calculable2.getValue();
140         if (holder2 != null)
141         {
142             distinctValues.addAll(holder2.getDistinctValues());
143             if (holder2.getLastValue() != null)
144                 distinctValues.add(holder2.getLastValue());
145         }
146         
147         return new DistinctCountHolder(distinctValues);
148     }
149
150
151     /**
152      *
153      */

154     public Object JavaDoc initialValue()
155     {
156         return null;
157     }
158
159
160 }
161
Popular Tags