1 16 package org.apache.commons.math.stat; 17 18 import java.io.Serializable ; 19 import java.text.NumberFormat ; 20 import java.util.Iterator ; 21 import java.util.Comparator ; 22 import java.util.TreeMap ; 23 24 40 public class Frequency implements Serializable { 41 42 43 static final long serialVersionUID = -3845586908418844111L; 44 45 46 private TreeMap freqTable = null; 47 48 51 public Frequency() { 52 freqTable = new TreeMap (); 53 } 54 55 60 public Frequency(Comparator comparator) { 61 freqTable = new TreeMap (comparator); 62 } 63 64 70 public String toString() { 71 NumberFormat nf = NumberFormat.getPercentInstance(); 72 StringBuffer outBuffer = new StringBuffer (); 73 outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n"); 74 Iterator iter = freqTable.keySet().iterator(); 75 while (iter.hasNext()) { 76 Object value = iter.next(); 77 outBuffer.append(value); 78 outBuffer.append('\t'); 79 outBuffer.append(getCount(value)); 80 outBuffer.append('\t'); 81 outBuffer.append(nf.format(getPct(value))); 82 outBuffer.append('\t'); 83 outBuffer.append(nf.format(getCumPct(value))); 84 outBuffer.append('\n'); 85 } 86 return outBuffer.toString(); 87 } 88 89 95 public void addValue(Object v) { 96 Object obj = v; 97 if (v instanceof Integer ) { 98 obj = new Long (((Integer ) v).longValue()); 99 } 100 try { 101 Long count = (Long ) freqTable.get(obj); 102 if (count == null) { 103 freqTable.put(obj, new Long (1)); 104 } else { 105 freqTable.put(obj, new Long (count.longValue() + 1)); 106 } 107 } catch (ClassCastException ex) { 108 throw new IllegalArgumentException ("Value not comparable to existing values."); 110 } 111 } 112 113 118 public void addValue(int v) { 119 addValue(new Long (v)); 120 } 121 122 127 public void addValue(Integer v) { 128 addValue(new Long (v.longValue())); 129 } 130 131 136 public void addValue(long v) { 137 addValue(new Long (v)); 138 } 139 140 145 public void addValue(char v) { 146 addValue(new Character (v)); 147 } 148 149 150 public void clear() { 151 freqTable.clear(); 152 } 153 154 163 public Iterator valuesIterator() { 164 return freqTable.keySet().iterator(); 165 } 166 167 169 174 public long getSumFreq() { 175 long result = 0; 176 Iterator iterator = freqTable.values().iterator(); 177 while (iterator.hasNext()) { 178 result += ((Long ) iterator.next()).longValue(); 179 } 180 return result; 181 } 182 183 189 public long getCount(Object v) { 190 if (v instanceof Integer ) { 191 return getCount(((Integer ) v).longValue()); 192 } 193 long result = 0; 194 try { 195 Long count = (Long ) freqTable.get(v); 196 if (count != null) { 197 result = count.longValue(); 198 } 199 } catch (ClassCastException ex) { 200 } 202 return result; 203 } 204 205 211 public long getCount(int v) { 212 return getCount(new Long (v)); 213 } 214 215 221 public long getCount(long v) { 222 return getCount(new Long (v)); 223 } 224 225 231 public long getCount(char v) { 232 return getCount(new Character (v)); 233 } 234 235 237 246 public double getPct(Object v) { 247 if (getSumFreq() == 0) { 248 return Double.NaN; 249 } 250 return (double) getCount(v) / (double) getSumFreq(); 251 } 252 253 260 public double getPct(int v) { 261 return getPct(new Long (v)); 262 } 263 264 271 public double getPct(long v) { 272 return getPct(new Long (v)); 273 } 274 275 282 public double getPct(char v) { 283 return getPct(new Character (v)); 284 } 285 286 288 296 public long getCumFreq(Object v) { 297 if (getSumFreq() == 0) { 298 return 0; 299 } 300 if (v instanceof Integer ) { 301 return getCumFreq(((Integer ) v).longValue()); 302 } 303 Comparator c = freqTable.comparator(); 304 if (c == null) { 305 c = new NaturalComparator(); 306 } 307 long result = 0; 308 309 try { 310 Long value = (Long ) freqTable.get(v); 311 if (value != null) { 312 result = value.longValue(); 313 } 314 } catch (ClassCastException ex) { 315 return result; } 317 318 if (c.compare(v, freqTable.firstKey()) < 0) { 319 return 0; } 321 322 if (c.compare(v, freqTable.lastKey()) >= 0) { 323 return getSumFreq(); } 325 326 Iterator values = valuesIterator(); 327 while (values.hasNext()) { 328 Object nextValue = values.next(); 329 if (c.compare(v, nextValue) > 0) { 330 result += getCount(nextValue); 331 } else { 332 return result; 333 } 334 } 335 return result; 336 } 337 338 346 public long getCumFreq(int v) { 347 return getCumFreq(new Long (v)); 348 } 349 350 358 public long getCumFreq(long v) { 359 return getCumFreq(new Long (v)); 360 } 361 362 370 public long getCumFreq(char v) { 371 return getCumFreq(new Character (v)); 372 } 373 374 376 387 public double getCumPct(Object v) { 388 if (getSumFreq() == 0) { 389 return Double.NaN; 390 } 391 return (double) getCumFreq(v) / (double) getSumFreq(); 392 } 393 394 403 public double getCumPct(int v) { 404 return getCumPct(new Long (v)); 405 } 406 407 416 public double getCumPct(long v) { 417 return getCumPct(new Long (v)); 418 } 419 420 429 public double getCumPct(char v) { 430 return getCumPct(new Character (v)); 431 } 432 433 437 private static class NaturalComparator implements Comparator { 438 451 public int compare(Object o1, Object o2) { 452 return ((Comparable )o1).compareTo(o2); 453 } 454 } 455 } 456 | Popular Tags |