All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xqbase.util.concurrent.CountMap Maven / Gradle / Ivy

There is a newer version: 0.2.18
Show newest version
package com.xqbase.util.concurrent;

import java.util.concurrent.ConcurrentHashMap;

public abstract class CountMap
		extends ConcurrentHashMap {
	private static final long serialVersionUID = 1L;

	protected abstract V newCount();

	public V acquire(K key) {
		V count = get(key);
		if (count == null) {
			V newCount = newCount();
			count = putIfAbsent(key, newCount);
			if (count == null) {
				return newCount;
			}
		}
		count.incrementAndGet();
		return count;
	}

	public void release(K key, V count) {
		if (count.decrementAndGet() == 0) {
			remove(key, Count.ZERO);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy