KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > crosstabs > fill > calculation > BucketDefinition


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.crosstabs.fill.calculation;
29
30 import java.util.Comparator JavaDoc;
31
32 import net.sf.jasperreports.engine.JRException;
33
34 import org.apache.commons.collections.comparators.ReverseComparator;
35
36 /**
37  * Bucket definition.
38  *
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  * @version $Id: BucketDefinition.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
41  */

42 public class BucketDefinition
43 {
44     /**
45      * Ascending order constant.
46      */

47     public static final byte ORDER_ASCENDING = 1;
48     
49     /**
50      * Descending order constant.
51      */

52     public static final byte ORDER_DESCENDING = 2;
53     
54     /**
55      * Constant indicating that total are not required for this bucket.
56      */

57     public static final byte TOTAL_POSITION_NONE = 0;
58     
59     /**
60      * Constants indicating that totals are to be positioned before the other buckets.
61      */

62     public static final byte TOTAL_POSITION_START = 1;
63     
64     /**
65      * Constants indicating that totals are to be positioned at the end of the other buckets.
66      */

67     public static final byte TOTAL_POSITION_END = 2;
68     
69     /**
70      * Value type used for non-null values.
71      */

72     protected static final byte VALUE_TYPE_VALUE = 0;
73     
74     /**
75      * Value type used for null bucket values.
76      */

77     protected static final byte VALUE_TYPE_NULL = 1;
78     
79     /**
80      * Value type used for total buckets.
81      */

82     protected static final byte VALUE_TYPE_TOTAL = 2;
83
84     /**
85      * The total value for this bucket.
86      */

87     protected final Bucket VALUE_TOTAL = new Bucket(VALUE_TYPE_TOTAL);
88     
89     /**
90      * The null bucket.
91      */

92     protected final Bucket VALUE_NULL = new Bucket(VALUE_TYPE_NULL);
93     
94     protected final Comparator JavaDoc comparator;
95     private final byte totalPosition;
96     
97     private boolean computeTotal;
98
99     
100     /**
101      * Creates a bucket.
102      *
103      * @param valueClass the class of the bucket values
104      * @param comparator the comparator to use for bucket sorting
105      * @param order the order type, {@link #ORDER_ASCENDING ORDER_ASCENDING} or {@link #ORDER_DESCENDING ORDER_DESCENDING} descending
106      * @param totalPosition the position of the total bucket
107      * @throws JRException
108      */

109     public BucketDefinition(Class JavaDoc valueClass, Comparator JavaDoc comparator, byte order, byte totalPosition) throws JRException
110     {
111         if (comparator == null && !Comparable JavaDoc.class.isAssignableFrom(valueClass))
112         {
113             throw new JRException("The bucket expression values are not comparable and no comparator specified.");
114         }
115         
116         switch (order)
117         {
118             case ORDER_DESCENDING:
119             {
120                 if (comparator == null)
121                 {
122                     this.comparator = new ReverseComparator();
123                 }
124                 else
125                 {
126                     this.comparator = new ReverseComparator(comparator);
127                 }
128                 break;
129             }
130             case ORDER_ASCENDING:
131             default:
132             {
133                 this.comparator = comparator;
134                 break;
135             }
136         }
137         
138         this.totalPosition = totalPosition;
139         computeTotal = totalPosition != TOTAL_POSITION_NONE;
140     }
141
142     
143     /**
144      * Whether this bucket needs total calculation.
145      *
146      * @return this bucket needs total calculation
147      */

148     public boolean computeTotal()
149     {
150         return computeTotal;
151     }
152
153     
154     /**
155      * Instructs that the bucket will need total bucket calculation.
156      *
157      * @see #computeTotal()
158      */

159     public void setComputeTotal()
160     {
161         computeTotal = true;
162     }
163
164     
165     /**
166      * Returns the total bucket position.
167      *
168      * @return the total bucket position
169      */

170     public byte getTotalPosition()
171     {
172         return totalPosition;
173     }
174
175     
176     /**
177      * Returns the comparator used for bucket ordering.
178      *
179      * @return the comparator used for bucket ordering
180      */

181     public Comparator JavaDoc getComparator()
182     {
183         return comparator;
184     }
185     
186     
187     /**
188      * Creates a {@link Bucket BucketValue} object for a given value.
189      *
190      * @param value the value
191      * @return the corresponding {@link Bucket BucketValue} object
192      */

193     public Bucket create(Object JavaDoc value)
194     {
195         if (value == null)
196         {
197             return VALUE_NULL;
198         }
199         
200         return new Bucket(value);
201     }
202     
203     
204     /**
205      * Bucket value class.
206      *
207      * @author Lucian Chirita (lucianc@users.sourceforge.net)
208      */

209     public class Bucket implements Comparable JavaDoc
210     {
211         private final Object JavaDoc value;
212         private final byte type;
213         
214         
215         /**
216          * Creates a bucket for a value type.
217          *
218          * @param type the value type
219          */

220         protected Bucket(byte type)
221         {
222             this.value = null;
223             this.type = type;
224         }
225         
226         
227         /**
228          * Creates a bucket for a value.
229          *
230          * @param value the value
231          */

232         protected Bucket(Object JavaDoc value)
233         {
234             this.value = value;
235             this.type = VALUE_TYPE_VALUE;
236         }
237         
238         
239         /**
240          * Returns the bucket value.
241          *
242          * @return the bucket value
243          */

244         public Object JavaDoc getValue()
245         {
246             return value;
247         }
248         
249         public boolean equals (Object JavaDoc o)
250         {
251             if (o == null || !(o instanceof Bucket))
252             {
253                 return false;
254             }
255             
256             if (o == this)
257             {
258                 return true;
259             }
260             
261             Bucket v = (Bucket) o;
262
263             if (type != VALUE_TYPE_VALUE)
264             {
265                 return type == v.type;
266             }
267             
268             return v.type == VALUE_TYPE_VALUE && value.equals(v.value);
269         }
270         
271         public int hashCode()
272         {
273             int hash = type;
274             
275             if (type == VALUE_TYPE_VALUE)
276             {
277                 hash = 37*hash + value.hashCode();
278             }
279             
280             return hash;
281         }
282         
283         public String JavaDoc toString()
284         {
285             switch(type)
286             {
287                 case VALUE_TYPE_NULL:
288                     return "NULL";
289                 case VALUE_TYPE_TOTAL:
290                     return "TOTAL";
291                 case VALUE_TYPE_VALUE:
292                 default:
293                     return String.valueOf(value);
294             }
295         }
296
297         public int compareTo(Object JavaDoc o)
298         {
299             Bucket val = (Bucket) o;
300             if (type != val.type)
301             {
302                 return type - val.type;
303             }
304             
305             if (type != VALUE_TYPE_VALUE)
306             {
307                 return 0;
308             }
309
310             if (comparator != null)
311             {
312                 return comparator.compare(value, val.value);
313             }
314             
315             return ((Comparable JavaDoc) value).compareTo(val.value);
316         }
317         
318         
319         /**
320          * Decides whether this is a total bucket.
321          *
322          * @return whether this is a total bucket
323          */

324         public boolean isTotal()
325         {
326             return type == VALUE_TYPE_TOTAL;
327         }
328     }
329 }
330
Popular Tags