KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.sf.jasperreports.engine.JRVariable;
34
35
36 /**
37  * @author Teodor Danciu (teodord@users.sourceforge.net)
38  * @version $Id: JRDistinctCountIncrementerFactory.java 1311 2006-06-23 12:19:26 +0300 (Fri, 23 Jun 2006) teodord $
39  */

40 public class JRDistinctCountIncrementerFactory implements JRIncrementerFactory
41 {
42
43
44     /**
45      *
46      */

47     private static JRDistinctCountIncrementerFactory mainInstance = new JRDistinctCountIncrementerFactory();
48
49
50     /**
51      *
52      */

53     public JRDistinctCountIncrementerFactory()
54     {
55     }
56
57
58     /**
59      *
60      */

61     public static JRDistinctCountIncrementerFactory getInstance()
62     {
63         return mainInstance;
64     }
65
66
67     /**
68      *
69      */

70     public JRIncrementer getIncrementer(byte calculation)
71     {
72         return new JRDistinctCountIncrementer();
73     }
74 }
75
76
77 /**
78  *
79  */

80 class JRDistinctCountIncrementer implements JRIncrementer
81 {
82
83     private DistinctCountHolder lastHolder = new DistinctCountHolder();
84     
85     
86     /**
87      *
88      */

89     public JRDistinctCountIncrementer()
90     {
91     }
92
93
94     /**
95      *
96      */

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

129 class DistinctCountHolder
130 {
131     private Set JavaDoc distinctValues = null;
132     private Object JavaDoc lastValue = null;
133
134     public DistinctCountHolder()
135     {
136         distinctValues = new HashSet JavaDoc();
137     }
138
139     public DistinctCountHolder(Set JavaDoc distinctValues)
140     {
141         this.distinctValues = distinctValues;
142     }
143
144     public DistinctCountHolder(DistinctCountHolder holder, Object JavaDoc lastValue)
145     {
146         this.distinctValues = holder.getDistinctValues();
147         this.lastValue = lastValue;
148     }
149
150     public void init()
151     {
152         distinctValues = new HashSet JavaDoc();
153     }
154
155     public Set JavaDoc getDistinctValues()
156     {
157         return distinctValues;
158     }
159
160     public Object JavaDoc getLastValue()
161     {
162         return lastValue;
163     }
164
165     public void addLastValue()
166     {
167         if (lastValue != null)
168         {
169             distinctValues.add(lastValue);
170         }
171         lastValue = null;
172     }
173
174     public long getCount()
175     {
176         return distinctValues.size() + (lastValue == null || distinctValues.contains(lastValue) ? 0 : 1);
177     }
178 }
179
Popular Tags