net.minidev.util.Counter Maven / Gradle / Ivy
The newest version!
package net.minidev.util;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeMap;
/**
* occurence counter
*
* @author Uriel Chemouni
*
* @param
*/
public class Counter> {
private TreeMap> map = new TreeMap>();
int total = 0;
public int add(T value) {
return add(value, 1);
}
public int add(T value, int poid) {
if (value == null)
return 0;
if (total != 0)
total = 0;
Value v = map.get(value);
if (v == null) {
v = new Value(value, poid);
map.put(value, v);
} else {
v.inc(poid);
}
return v.getCount();
}
public Set getKeys() {
return map.keySet();
}
public int getCount(T key) {
Value i = map.get(key);
if (i == null)
return 0;
else
return i.getCount();
}
public int countDistinct() {
return map.size();
}
public int count() {
if (total != 0)
return total;
int c = 0;
for (Value i : map.values())
c += i.getCount();
total = c;
return c;
}
public double getPct(T value) {
int count = count();
if (count == 0)
return Double.NaN;
Value v = map.get(value);
if (v == null)
return 0;
return ((double) v.getCount()) / ((double) count);
}
public Set keys() {
return map.keySet();
}
@SuppressWarnings("unchecked")
public Value[] getValues() {
Value[] out = new Value[map.size()];
map.values().toArray(out);
Arrays.sort(out);
return out;
}
public Value getTopValue() {
int offCount = 0;
Value result = null;
for (Value v : map.values()) {
if (v.getCount() > offCount) {
offCount = v.getCount();
result = v;
}
}
return result;
}
public static class Value implements Comparable> {
private T key;
private int count;
public Value(T key, int count) {
this.key = key;
this.count = count;
}
public T getKey() {
return key;
}
public int getCount() {
return count;
}
private void inc(int poid) {
this.count += poid;
}
public String toString() {
return "Value:" + key + " occ:" + count;
}
@Override
public int compareTo(Value o) {
return o.getCount() - this.getCount();
}
}
}