1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: Histogram.java 2090 2011-03-07 04:13:05Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * A simple implementation of a histogram. It would be nice to enhance it to
29   * order on frequency.
30   * 
31   * @see gnu.lgpl.License for license details.<br>
32   *      The copyright to this program is held by it's authors.
33   * @author DM Smith [ dmsmith555 at yahoo dot com]
34   */
35  public class Histogram {
36      /**
37       * Create an empty histogram
38       */
39      public Histogram() {
40          hist = new HashMap<String, Counter>();
41      }
42  
43      /**
44       * note that this key has been seen one time more than before.
45       * 
46       * @param key
47       */
48      public void increment(String key) {
49          Counter counter = hist.get(key);
50          if (counter == null) {
51              counter = new Counter();
52              hist.put(key, counter);
53          }
54          counter.increment();
55      }
56  
57      public void clear() {
58          hist.clear();
59      }
60  
61      /**
62       * The format of the histogram is an unordered list of string and the counts
63       * of the number of times it has been seen.
64       * 
65       * @return the resultant histogram
66       * @see java.lang.Object#toString()
67       */
68      @Override
69      public String toString() {
70          StringBuilder buf = new StringBuilder();
71          for (Map.Entry<String, Counter> entry : hist.entrySet()) {
72              buf.append(entry.getKey());
73              buf.append('\t');
74              buf.append(entry.getValue().toString());
75              buf.append('\n');
76          }
77          return buf.toString();
78      }
79  
80      /**
81       * Trivial mutable counting integer class.
82       */
83      private static class Counter {
84          public Counter() {
85          }
86  
87          public void increment() {
88              counter++;
89          }
90  
91          @Override
92          public String toString() {
93              return Integer.toString(counter);
94          }
95  
96          private int counter;
97      }
98  
99      private Map<String, Counter> hist;
100 
101 }
102