1 28 package net.sf.jasperreports.crosstabs.fill.calculation; 29 30 import net.sf.jasperreports.engine.JRException; 31 import net.sf.jasperreports.engine.JRVariable; 32 import net.sf.jasperreports.engine.fill.AbstractValueProvider; 33 import net.sf.jasperreports.engine.fill.JRCalculable; 34 import net.sf.jasperreports.engine.fill.JRDistinctCountExtendedIncrementerFactory; 35 import net.sf.jasperreports.engine.fill.JRExtendedIncrementer; 36 import net.sf.jasperreports.engine.fill.JRExtendedIncrementerFactory; 37 38 44 public class MeasureDefinition 45 { 46 protected final byte calculation; 47 protected final JRExtendedIncrementerFactory incrementerFactory; 48 protected final Class valueClass; 49 protected final boolean isSystemDefined; 50 51 52 59 public MeasureDefinition( 60 Class valueClass, 61 byte calculation, 62 JRExtendedIncrementerFactory incrementerFactory) 63 { 64 this(valueClass, calculation, incrementerFactory, false); 65 } 66 67 protected MeasureDefinition( 68 Class valueClass, 69 byte calculation, 70 JRExtendedIncrementerFactory incrementerFactory, 71 boolean isSystemDefined) 72 { 73 this.valueClass = valueClass; 74 this.calculation = calculation; 75 this.incrementerFactory = incrementerFactory; 76 this.isSystemDefined = isSystemDefined; 77 } 78 79 80 87 public static MeasureDefinition createHelperMeasure(MeasureDefinition measure, byte helperCalculation) 88 { 89 return new MeasureDefinition(measure.valueClass, helperCalculation, measure.incrementerFactory, true); 90 } 91 92 93 99 public static MeasureDefinition createDistinctCountHelperMeasure(MeasureDefinition measure) 100 { 101 return new MeasureDefinition(measure.valueClass, JRVariable.CALCULATION_NOTHING, JRDistinctCountExtendedIncrementerFactory.getInstance(), true); 102 } 103 104 105 110 public byte getCalculation() 111 { 112 return calculation; 113 } 114 115 116 121 public JRExtendedIncrementerFactory getIncrementerFactory() 122 { 123 return incrementerFactory; 124 } 125 126 127 132 public JRExtendedIncrementer getIncrementer() 133 { 134 return incrementerFactory.getExtendedIncrementer(calculation); 135 } 136 137 protected boolean isSystemDefined() 138 { 139 return isSystemDefined; 140 } 141 142 143 148 public Class getValueClass() 149 { 150 return valueClass; 151 } 152 153 154 157 protected static final AbstractValueProvider VALUE_PROVIDER = new AbstractValueProvider() 158 { 159 public Object getValue(JRCalculable calculable) 160 { 161 return calculable.getValue(); 162 } 163 }; 164 165 166 171 public class MeasureValue implements JRCalculable 172 { 173 private Object value; 174 private MeasureValue[] helpers; 175 private boolean initialized; 176 private JRExtendedIncrementer incrementer; 177 178 179 182 public MeasureValue() 183 { 184 this.value = null; 185 this.helpers = new MeasureValue[HELPER_SIZE]; 186 incrementer = getIncrementer(); 187 188 init(); 189 } 190 191 protected void init() 192 { 193 this.value = incrementer.initialValue(); 194 setInitialized(true); 195 } 196 197 198 204 public void addValue(Object addValue) throws JRException 205 { 206 this.value = incrementer.increment(this, addValue, VALUE_PROVIDER); 207 setInitialized(false); 208 } 209 210 211 219 public void addValue(MeasureValue measureValue) throws JRException 220 { 221 if (!measureValue.isInitialized()) 222 { 223 this.value = incrementer.combine(this, measureValue, VALUE_PROVIDER); 224 setInitialized(false); 225 } 226 } 227 228 public Object getValue() 229 { 230 return value; 231 } 232 233 public String toString() 234 { 235 return String.valueOf(getValue()); 236 } 237 238 239 246 public MeasureValue setHelper(MeasureValue helperVariable, byte type) 247 { 248 MeasureValue old = helpers[type]; 249 helpers[type] = helperVariable; 250 return old; 251 } 252 253 public boolean isInitialized() 254 { 255 return initialized; 256 } 257 258 public Object getIncrementedValue() 259 { 260 return value; 261 } 262 263 public JRCalculable getHelperVariable(byte helperType) 264 { 265 return helpers[helperType]; 266 } 267 268 public void setInitialized(boolean isInitialized) 269 { 270 this.initialized = isInitialized; 271 } 272 } 273 } 274 | Popular Tags |