1 28 package net.sf.jasperreports.crosstabs.fill.calculation; 29 30 import java.util.Comparator ; 31 32 import net.sf.jasperreports.engine.JRException; 33 34 import org.apache.commons.collections.comparators.ReverseComparator; 35 36 42 public class BucketDefinition 43 { 44 47 public static final byte ORDER_ASCENDING = 1; 48 49 52 public static final byte ORDER_DESCENDING = 2; 53 54 57 public static final byte TOTAL_POSITION_NONE = 0; 58 59 62 public static final byte TOTAL_POSITION_START = 1; 63 64 67 public static final byte TOTAL_POSITION_END = 2; 68 69 72 protected static final byte VALUE_TYPE_VALUE = 0; 73 74 77 protected static final byte VALUE_TYPE_NULL = 1; 78 79 82 protected static final byte VALUE_TYPE_TOTAL = 2; 83 84 87 protected final Bucket VALUE_TOTAL = new Bucket(VALUE_TYPE_TOTAL); 88 89 92 protected final Bucket VALUE_NULL = new Bucket(VALUE_TYPE_NULL); 93 94 protected final Comparator comparator; 95 private final byte totalPosition; 96 97 private boolean computeTotal; 98 99 100 109 public BucketDefinition(Class valueClass, Comparator comparator, byte order, byte totalPosition) throws JRException 110 { 111 if (comparator == null && !Comparable .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 148 public boolean computeTotal() 149 { 150 return computeTotal; 151 } 152 153 154 159 public void setComputeTotal() 160 { 161 computeTotal = true; 162 } 163 164 165 170 public byte getTotalPosition() 171 { 172 return totalPosition; 173 } 174 175 176 181 public Comparator getComparator() 182 { 183 return comparator; 184 } 185 186 187 193 public Bucket create(Object value) 194 { 195 if (value == null) 196 { 197 return VALUE_NULL; 198 } 199 200 return new Bucket(value); 201 } 202 203 204 209 public class Bucket implements Comparable 210 { 211 private final Object value; 212 private final byte type; 213 214 215 220 protected Bucket(byte type) 221 { 222 this.value = null; 223 this.type = type; 224 } 225 226 227 232 protected Bucket(Object value) 233 { 234 this.value = value; 235 this.type = VALUE_TYPE_VALUE; 236 } 237 238 239 244 public Object getValue() 245 { 246 return value; 247 } 248 249 public boolean equals (Object 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 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 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 ) value).compareTo(val.value); 316 } 317 318 319 324 public boolean isTotal() 325 { 326 return type == VALUE_TYPE_TOTAL; 327 } 328 } 329 } 330 | Popular Tags |