KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > Histotable


1 /*
2  * Histotable.java
3  *
4  * Created on Aug 5, 2004
5  *
6  * $Id: Histotable.java,v 1.4.16.1 2007/01/13 01:31:39 stack-sf Exp $
7  *
8  *
9  * Copyright (C) 2003 Internet Archive.
10  *
11  * This file is part of the Heritrix web crawler (crawler.archive.org).
12  *
13  * Heritrix is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * any later version.
17  *
18  * Heritrix is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser Public License
24  * along with Heritrix; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */

27  
28
29 package org.archive.util;
30
31 import java.util.Comparator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.TreeSet JavaDoc;
35
36
37 /**
38  * Collect and report frequency information.
39  *
40  * Assumes external synchornization.
41  *
42  * @author gojomo
43  */

44 public class Histotable {
45     HashMap JavaDoc<Object JavaDoc,LongWrapper> totals = new HashMap JavaDoc<Object JavaDoc,LongWrapper>();
46
47     // sorted by count
48
TreeSet JavaDoc<Map.Entry JavaDoc<Object JavaDoc,LongWrapper>> sorted =
49       new TreeSet JavaDoc<Map.Entry JavaDoc<Object JavaDoc,LongWrapper>>(
50        new Comparator JavaDoc<Map.Entry JavaDoc<Object JavaDoc,LongWrapper>>() {
51         public int compare(Map.Entry JavaDoc<Object JavaDoc,LongWrapper> e1,
52                 Map.Entry JavaDoc<Object JavaDoc,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             // If the values are the same, sort by keys.
58
String JavaDoc firstKey = (String JavaDoc) ((Map.Entry JavaDoc) e1).getKey();
59             String JavaDoc secondKey = (String JavaDoc) ((Map.Entry JavaDoc) e2).getKey();
60             return firstKey.compareTo(secondKey);
61         }
62     });
63     
64     /**
65      * Record one more occurence of the given object key.
66      *
67      * @param key Object key.
68      */

69     public void tally(Object JavaDoc key) {
70         if (totals.containsKey(key)) {
71             totals.get(key).longValue += 1;
72         } else {
73             // if we didn't find this key add it
74
totals.put(key, new LongWrapper(1));
75         }
76     }
77     
78     /**
79      * @return Return an up-to-date sorted version of the totalled info.
80      */

81     public TreeSet JavaDoc getSorted() {
82         if(sorted.size()<totals.size()) {
83             sorted.clear();
84             sorted.addAll(totals.entrySet());
85         }
86         return sorted;
87     }
88     
89     /**
90      * Utility method to convert a key-&gt;LongWrapper(count) into
91      * the string "count key".
92      *
93      * @param e Map key.
94      * @return String 'count key'.
95      */

96     public static String JavaDoc entryString(Object JavaDoc e) {
97         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) e;
98         return ((LongWrapper)entry.getValue()).longValue + " " + entry.getKey();
99     }
100 }
101
Popular Tags