|
13 | 13 | See the License for the specific language governing permissions and
|
14 | 14 | limitations under the License.
|
15 | 15 | */
|
16 |
| - |
17 | 16 | package cue.lang;
|
18 | 17 |
|
19 | 18 | import java.util.ArrayList;
|
|
26 | 25 | import java.util.Set;
|
27 | 26 |
|
28 | 27 | /**
|
29 |
| - * |
| 28 | + * |
30 | 29 | * @author Jonathan Feinberg <[email protected]>
|
31 | 30 | * @param <T>
|
32 |
| - * |
| 31 | + * |
33 | 32 | */
|
34 | 33 | public class Counter<T> {
|
35 |
| - // delegate, don't extend, to prevent unauthorized monkeying with internals |
36 |
| - private final Map<T, Integer> items = new HashMap<T, Integer>(); |
37 |
| - private int totalItemCount = 0; |
38 |
| - |
39 |
| - public Counter() { |
40 |
| - this.BY_FREQ_DESC = new Comparator<Entry<T, Integer>>() { |
41 |
| - @Override |
42 |
| - public int compare(final Entry<T, Integer> o1, |
43 |
| - final Entry<T, Integer> o2) { |
44 |
| - return o2.getValue() - o1.getValue(); |
45 |
| - } |
46 |
| - }; |
47 |
| - } |
48 |
| - |
49 |
| - public Counter(final Iterable<T> items) { |
50 |
| - this.BY_FREQ_DESC = new Comparator<Entry<T, Integer>>() { |
51 |
| - @Override |
52 |
| - public int compare(final Entry<T, Integer> o1, |
53 |
| - final Entry<T, Integer> o2) { |
54 |
| - return o2.getValue() - o1.getValue(); |
55 |
| - } |
56 |
| - }; |
57 |
| - noteAll(items); |
58 |
| - } |
59 |
| - |
60 |
| - public final void noteAll(final Iterable<T> items) { |
61 |
| - for (final T t : items) { |
62 |
| - note(t, 1); |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - public void note(final T item) { |
67 |
| - note(item, 1); |
68 |
| - } |
69 |
| - |
70 |
| - public void note(final T item, final int count) { |
71 |
| - final Integer existingCount = items.get(item); |
72 |
| - if (existingCount != null) { |
73 |
| - items.put(item, existingCount + count); |
74 |
| - } else { |
75 |
| - items.put(item, count); |
76 |
| - } |
77 |
| - totalItemCount += count; |
78 |
| - } |
79 |
| - |
80 |
| - public void merge(final Counter<T> c) { |
81 |
| - for (final Entry<T, Integer> e : c.items.entrySet()) { |
82 |
| - note(e.getKey(), e.getValue()); |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - public int getTotalItemCount() { |
87 |
| - return totalItemCount; |
88 |
| - } |
89 |
| - |
90 |
| - private final Comparator<Entry<T, Integer>> BY_FREQ_DESC; |
91 |
| - |
92 |
| - /** |
93 |
| - * @param n |
94 |
| - * @return A list of the min(n, size()) most frequent items |
95 |
| - */ |
96 |
| - public List<T> getMostFrequent(final int n) { |
97 |
| - final List<Entry<T, Integer>> all = getAllByFrequency(); |
98 |
| - final int resultSize = Math.min(n, items.size()); |
99 |
| - final List<T> result = new ArrayList<T>(resultSize); |
100 |
| - for (final Entry<T, Integer> e : all.subList(0, resultSize)) { |
101 |
| - result.add(e.getKey()); |
102 |
| - } |
103 |
| - return Collections.unmodifiableList(result); |
104 |
| - } |
105 |
| - |
106 |
| - public List<Entry<T, Integer>> getAllByFrequency() { |
107 |
| - final List<Entry<T, Integer>> all = new ArrayList<Entry<T, Integer>>( |
108 |
| - items.entrySet()); |
109 |
| - Collections.sort(all, BY_FREQ_DESC); |
110 |
| - return Collections.unmodifiableList(all); |
111 |
| - } |
112 |
| - |
113 |
| - public Integer getCount(final T item) { |
114 |
| - final Integer freq = items.get(item); |
115 |
| - if (freq == null) { |
116 |
| - return 0; |
117 |
| - } |
118 |
| - return freq; |
119 |
| - } |
120 |
| - |
121 |
| - public void clear() { |
122 |
| - items.clear(); |
123 |
| - } |
124 |
| - |
125 |
| - public Set<Entry<T, Integer>> entrySet() { |
126 |
| - return Collections.unmodifiableSet(items.entrySet()); |
127 |
| - } |
128 |
| - |
129 |
| - public Set<T> keySet() { |
130 |
| - return Collections.unmodifiableSet(items.keySet()); |
131 |
| - } |
132 |
| - |
133 |
| - public List<T> keyList() { |
134 |
| - return getMostFrequent(items.size()); |
135 |
| - } |
136 |
| - |
137 |
| - @Override |
138 |
| - public String toString() { |
139 |
| - return items.toString(); |
140 |
| - } |
| 34 | + // delegate, don't extend, to prevent unauthorized monkeying with internals |
| 35 | + |
| 36 | + private final Map<T, Integer> items = new HashMap<>(); |
| 37 | + private int totalItemCount = 0; |
| 38 | + |
| 39 | + public Counter() { |
| 40 | + this.BY_FREQ_DESC = (final Entry<T, Integer> o1, final Entry<T, Integer> o2) -> o2.getValue() - o1.getValue(); |
| 41 | + } |
| 42 | + |
| 43 | + public Counter(final Iterable<T> items) { |
| 44 | + this.BY_FREQ_DESC = (final Entry<T, Integer> o1, final Entry<T, Integer> o2) -> o2.getValue() - o1.getValue(); |
| 45 | + noteAll(items); |
| 46 | + } |
| 47 | + |
| 48 | + public final void noteAll(final Iterable<T> items) { |
| 49 | + for (final T t : items) { |
| 50 | + note(t, 1); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void note(final T item) { |
| 55 | + note(item, 1); |
| 56 | + } |
| 57 | + |
| 58 | + public void note(final T item, final int count) { |
| 59 | + final Integer existingCount = items.get(item); |
| 60 | + if (existingCount != null) { |
| 61 | + items.put(item, existingCount + count); |
| 62 | + } else { |
| 63 | + items.put(item, count); |
| 64 | + } |
| 65 | + totalItemCount += count; |
| 66 | + } |
| 67 | + |
| 68 | + public void merge(final Counter<T> c) { |
| 69 | + c.items.entrySet().forEach((e) -> { |
| 70 | + note(e.getKey(), e.getValue()); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + public int getTotalItemCount() { |
| 75 | + return totalItemCount; |
| 76 | + } |
| 77 | + |
| 78 | + private final Comparator<Entry<T, Integer>> BY_FREQ_DESC; |
| 79 | + |
| 80 | + /** |
| 81 | + * @param n |
| 82 | + * @return A list of the min(n, size()) most frequent items |
| 83 | + */ |
| 84 | + public List<T> getMostFrequent(final int n) { |
| 85 | + final List<Entry<T, Integer>> all = getAllByFrequency(); |
| 86 | + final int resultSize = Math.min(n, items.size()); |
| 87 | + final List<T> result = new ArrayList<>(resultSize); |
| 88 | + all.subList(0, resultSize).forEach((e) -> { |
| 89 | + result.add(e.getKey()); |
| 90 | + }); |
| 91 | + return Collections.unmodifiableList(result); |
| 92 | + } |
| 93 | + |
| 94 | + public List<Entry<T, Integer>> getAllByFrequency() { |
| 95 | + final List<Entry<T, Integer>> all = new ArrayList<>( |
| 96 | + items.entrySet()); |
| 97 | + Collections.sort(all, BY_FREQ_DESC); |
| 98 | + return Collections.unmodifiableList(all); |
| 99 | + } |
| 100 | + |
| 101 | + public Integer getCount(final T item) { |
| 102 | + final Integer freq = items.get(item); |
| 103 | + if (freq == null) { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + return freq; |
| 107 | + } |
| 108 | + |
| 109 | + public void clear() { |
| 110 | + items.clear(); |
| 111 | + } |
| 112 | + |
| 113 | + public Set<Entry<T, Integer>> entrySet() { |
| 114 | + return Collections.unmodifiableSet(items.entrySet()); |
| 115 | + } |
| 116 | + |
| 117 | + public Set<T> keySet() { |
| 118 | + return Collections.unmodifiableSet(items.keySet()); |
| 119 | + } |
| 120 | + |
| 121 | + public List<T> keyList() { |
| 122 | + return getMostFrequent(items.size()); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return items.toString(); |
| 128 | + } |
141 | 129 | }
|
0 commit comments