Skip to content

Instantly share code, notes, and snippets.

@dpash
Created August 29, 2018 19:33
Show Gist options
  • Save dpash/0e28f29eff0f8576c3f6fb3285acddd9 to your computer and use it in GitHub Desktop.
Save dpash/0e28f29eff0f8576c3f6fb3285acddd9 to your computer and use it in GitHub Desktop.
public class MutableInteger {
private int val;
public MutableInteger(int val) {
this.val = val;
}
public int get() {
return val;
}
public void set(int val) {
this.val = val;
}
@Override
public String toString() {
return Integer.toString(val);
}
}
public class CounterConcurrentHashMap<K> extends ConcurrentHashMap<K, MutableInteger> {
public MutableInteger up(K key) {
MutableInteger initValue = new MutableInteger(1);
MutableInteger oldValue = put(key, initValue);
if (oldValue != null) {
initValue.set(oldValue.get() + 1);
}
return initValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment