@@ -0,0 +1,1402 @@
+package com.trendrr.oss;%0A%0Aimport java.util.Collection;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0A/**%0A * A threadsafe map for maintaining counts of string keys.%0A * %0A * Uses AtomicInteger internally, with no synchronization so is a fast as could be.%0A * %0A * @author dan frank%0A *%0A */%0Apublic class IncrementMap %7B%09%0A%09ConcurrentHashMap%3CString, AtomicInteger%3E inc = new ConcurrentHashMap%3CString, AtomicInteger%3E();%0A%09%0A%09/**%0A%09 * Will increment key by amount - adds key to the map if not present%0A%09 * @param key%0A%09 * @param amount%0A%09 */%0A%09public void inc(String key, int amount) %7B%0A%09%09if (amount == 0) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09this.inc.putIfAbsent(key, new AtomicInteger(0));%0A%09%09this.inc.get(key).addAndGet(amount);%0A%09%7D%0A%09%0A%09public void clear()%7B%0A%09%09this.inc.clear();%0A%09%7D%0A%09%0A%09public void set(String key, int amount)%7B%0A%09%09this.inc.put(key, new AtomicInteger(amount));%0A%09%7D%0A%09%0A%09public int get(String key)%7B%0A%09%09AtomicInteger gotten = null;%0A%09%09try%7B%0A%09%09%09gotten = this.inc.get(key);%0A%09%09%7Dcatch (Exception e)%7B%7D%0A%09%09if (gotten == null)%7B%0A%09%09%09return 0;%0A%09%09%7Delse%7B%0A%09%09%09return gotten.get();%0A%09%09%7D%0A%09%7D%0A%09%0A%09public int remove(String key)%7B%0A%09%09AtomicInteger was = this.inc.remove(key);%0A%09%09return (was != null) ? was.get() : 0;%0A%09%7D%0A%09%0A%09public Set%3CString%3E keySet()%7B%0A%09%09return this.inc.keySet();%0A%09%7D%0A%0A%09%0A%09@Override%0A%09public String toString() %7B%0A%09%09return this.inc.toString();%0A%09%7D%0A%09%0A%09%0A%09%0A%7D%0A

Add threadsafe increment map