net.sf.gluebooster.java.booster.basic.container.CountingMap Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.container;
import java.util.HashMap;
/**
* May be used as counter of the keys.
*
* @author CBauer
*
* @param
*/
public class CountingMap extends HashMap {
/**
* Increments the value of the key by 1.
*
* @param key
* the key of the map
*/
public void increment(Key key) {
Long value = get(key);
if (value == null) {
value = new Long(1);
} else {
value = new Long(value.longValue() + 1);
}
put(key, value);
}
/**
* Decrements the value of the key by 1.
*
* @param key
* the key of the map
*/
public void decrement(Key key) {
Long value = get(key);
if (value == null) {
value = new Long(-1);
} else {
value = new Long(value.longValue() - 1);
}
put(key, value);
}
/**
* Returns the value of the key
*
* @param key
* the key of the map
* @return 0 if no value
*/
public long count(Object key) {
Long result = super.get(key);
if (result == null) {
return 0;
} else {
return result.longValue();
}
}
@Override
public Long get(Object key) {
Long result = super.get(key);
if (result == null) {
result = new Long(0);
}
return result;
}
}