forked from I2P_Developers/i2p.i2p
* NetDb:
- Move stat initialization, reduce number of rates - Add basic DOS prevention by not flooding if stores are too-frequent
This commit is contained in:
@@ -10,19 +10,28 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
public class ObjectCounter<K> {
|
||||
private ConcurrentHashMap<K, Integer> _map;
|
||||
private static final Integer ONE = Integer.valueOf(1);
|
||||
|
||||
public ObjectCounter() {
|
||||
_map = new ConcurrentHashMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one.
|
||||
* Not perfectly concurrent, new AtomicInteger(1) would be better,
|
||||
* at the cost of some object churn.
|
||||
* @return count after increment
|
||||
*/
|
||||
public void increment(K h) {
|
||||
Integer i = _map.putIfAbsent(h, Integer.valueOf(1));
|
||||
if (i != null)
|
||||
_map.put(h, Integer.valueOf(i.intValue() + 1));
|
||||
public int increment(K h) {
|
||||
Integer i = _map.putIfAbsent(h, ONE);
|
||||
if (i != null) {
|
||||
int rv = i.intValue() + 1;
|
||||
_map.put(h, Integer.valueOf(rv));
|
||||
return rv;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return current count
|
||||
*/
|
||||
@@ -32,11 +41,20 @@ public class ObjectCounter<K> {
|
||||
return i.intValue();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return set of objects with counts > 0
|
||||
*/
|
||||
public Set<K> objects() {
|
||||
return _map.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* start over
|
||||
* @since 0.7.11
|
||||
*/
|
||||
public void clear() {
|
||||
_map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user