1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.util.HashSet ; 31 import java.util.Set ; 32 33 import net.sf.jasperreports.engine.JRVariable; 34 35 36 40 public class JRDistinctCountIncrementerFactory implements JRIncrementerFactory 41 { 42 43 44 47 private static JRDistinctCountIncrementerFactory mainInstance = new JRDistinctCountIncrementerFactory(); 48 49 50 53 public JRDistinctCountIncrementerFactory() 54 { 55 } 56 57 58 61 public static JRDistinctCountIncrementerFactory getInstance() 62 { 63 return mainInstance; 64 } 65 66 67 70 public JRIncrementer getIncrementer(byte calculation) 71 { 72 return new JRDistinctCountIncrementer(); 73 } 74 } 75 76 77 80 class JRDistinctCountIncrementer implements JRIncrementer 81 { 82 83 private DistinctCountHolder lastHolder = new DistinctCountHolder(); 84 85 86 89 public JRDistinctCountIncrementer() 90 { 91 } 92 93 94 97 public Object increment( 98 JRFillVariable variable, 99 Object 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 129 class DistinctCountHolder 130 { 131 private Set distinctValues = null; 132 private Object lastValue = null; 133 134 public DistinctCountHolder() 135 { 136 distinctValues = new HashSet (); 137 } 138 139 public DistinctCountHolder(Set distinctValues) 140 { 141 this.distinctValues = distinctValues; 142 } 143 144 public DistinctCountHolder(DistinctCountHolder holder, Object lastValue) 145 { 146 this.distinctValues = holder.getDistinctValues(); 147 this.lastValue = lastValue; 148 } 149 150 public void init() 151 { 152 distinctValues = new HashSet (); 153 } 154 155 public Set getDistinctValues() 156 { 157 return distinctValues; 158 } 159 160 public Object 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 |