1 27 28 29 package org.archive.util; 30 31 import java.util.Comparator ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import java.util.TreeSet ; 35 36 37 44 public class Histotable { 45 HashMap <Object ,LongWrapper> totals = new HashMap <Object ,LongWrapper>(); 46 47 TreeSet <Map.Entry <Object ,LongWrapper>> sorted = 49 new TreeSet <Map.Entry <Object ,LongWrapper>>( 50 new Comparator <Map.Entry <Object ,LongWrapper>>() { 51 public int compare(Map.Entry <Object ,LongWrapper> e1, 52 Map.Entry <Object ,LongWrapper> e2) { 53 long firstVal = e1.getValue().longValue; 54 long secondVal = e2.getValue().longValue; 55 if (firstVal < secondVal) { return 1; } 56 if (secondVal < firstVal) { return -1; } 57 String firstKey = (String ) ((Map.Entry ) e1).getKey(); 59 String secondKey = (String ) ((Map.Entry ) e2).getKey(); 60 return firstKey.compareTo(secondKey); 61 } 62 }); 63 64 69 public void tally(Object key) { 70 if (totals.containsKey(key)) { 71 totals.get(key).longValue += 1; 72 } else { 73 totals.put(key, new LongWrapper(1)); 75 } 76 } 77 78 81 public TreeSet getSorted() { 82 if(sorted.size()<totals.size()) { 83 sorted.clear(); 84 sorted.addAll(totals.entrySet()); 85 } 86 return sorted; 87 } 88 89 96 public static String entryString(Object e) { 97 Map.Entry entry = (Map.Entry ) e; 98 return ((LongWrapper)entry.getValue()).longValue + " " + entry.getKey(); 99 } 100 } 101 | Popular Tags |