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

org.dstadler.commons.collections.ConcurrentMappedCounter Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package org.dstadler.commons.collections;

import java.util.Map;
import java.util.Set;

/**
 * Thread-Safe variant of {@link MappedCounter},
 * currently internally delegates to a {@link MappedCounterImpl}
 *
 * @param 
 * @author dominik.stadler
 */
public class ConcurrentMappedCounter implements MappedCounter {
	// simply delegate to a normal MappedCounter in synchronized blocks for now
	// a better implementation, e.g. by using ConcurrentHashMap can be added later if necessary
	private MappedCounter counter = new MappedCounterImpl<>();

	@Override
	public void addInt(T k, int v) {
		synchronized (counter) {
			counter.addInt(k, v);
		}
	}

	@Override
	public int get(T k) {
		synchronized (counter) {
			return counter.get(k);
		}
	}

	@Override
	public int remove(T key) {
		synchronized (counter) {
			return counter.remove(key);
		}
	}

	@Override
	public Set keys() {
		synchronized (counter) {
			return counter.keys();
		}
	}

	@Override
	public Set> entries() {
		synchronized (counter) {
			return counter.entries();
		}
	}

	@Override
	public Map sortedMap() {
		synchronized (counter) {
			return counter.sortedMap();
		}
	}

	@Override
	public int sum() {
		synchronized (counter) {
			return counter.sum();
		}
	}

	@Override
	public String toString() {
		return "Concurrent: " + counter.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy