Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Histogram |
|
| 1.2857142857142858;1.286 | ||||
Histogram$Counter |
|
| 1.2857142857142858;1.286 |
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 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the 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 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.common.util; | |
21 | ||
22 | import java.util.HashMap; | |
23 | import java.util.Map; | |
24 | ||
25 | /** | |
26 | * A simple implementation of a histogram. It would be nice to enhance it to | |
27 | * order on frequency. | |
28 | * | |
29 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
30 | * @author DM Smith | |
31 | */ | |
32 | public class Histogram { | |
33 | /** | |
34 | * Create an empty histogram | |
35 | */ | |
36 | 0 | public Histogram() { |
37 | 0 | hist = new HashMap<String, Counter>(); |
38 | 0 | } |
39 | ||
40 | /** | |
41 | * Note that this key has been seen one time more than before. | |
42 | * | |
43 | * @param key the key to increment | |
44 | */ | |
45 | public void increment(String key) { | |
46 | 0 | Counter counter = hist.get(key); |
47 | 0 | if (counter == null) { |
48 | 0 | counter = new Counter(); |
49 | 0 | hist.put(key, counter); |
50 | } | |
51 | 0 | counter.increment(); |
52 | 0 | } |
53 | ||
54 | public void clear() { | |
55 | 0 | hist.clear(); |
56 | 0 | } |
57 | ||
58 | /** | |
59 | * The format of the histogram is an unordered list of string and the counts | |
60 | * of the number of times it has been seen. | |
61 | * | |
62 | * @return the resultant histogram | |
63 | * @see java.lang.Object#toString() | |
64 | */ | |
65 | @Override | |
66 | public String toString() { | |
67 | 0 | StringBuilder buf = new StringBuilder(); |
68 | 0 | for (Map.Entry<String, Counter> entry : hist.entrySet()) { |
69 | 0 | buf.append(entry.getKey()); |
70 | 0 | buf.append('\t'); |
71 | 0 | buf.append(entry.getValue().toString()); |
72 | 0 | buf.append('\n'); |
73 | } | |
74 | 0 | return buf.toString(); |
75 | } | |
76 | ||
77 | /** | |
78 | * Trivial mutable counting integer class. | |
79 | */ | |
80 | private static class Counter { | |
81 | 0 | Counter() { |
82 | 0 | } |
83 | ||
84 | public void increment() { | |
85 | 0 | counter++; |
86 | 0 | } |
87 | ||
88 | @Override | |
89 | public String toString() { | |
90 | 0 | return Integer.toString(counter); |
91 | } | |
92 | ||
93 | private int counter; | |
94 | } | |
95 | ||
96 | private Map<String, Counter> hist; | |
97 | ||
98 | } |