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 class CountMap extends ConcurrentHashMap {
	private static final long serialVersionUID = 1L;

	protected Count newCount() {
		return new Count();
	}

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy