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.5
Show newest version
package org.dstadler.commons.collections;

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

/**
 * Thread-Safe variant of {@link MappedCounter},
 * currently internally delegates to a {@link MappedCounterImpl}
 *
 * @param  The type of the key for the mapped counter, often this
 *           will be String, but any type that can be used as key for
 *           a HashMap will work here.
 */
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 final MappedCounter counter = new MappedCounterImpl<>();

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

	@Override
	public void inc(T k) {
		synchronized (counter) {
			counter.inc(k);
		}
	}

	@Override
    public void count(Collection items) {
        synchronized (counter) {
            counter.count(items);
        }
    }

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

    @Override
    public long 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 void clear() {
        synchronized (counter) {
            counter.clear();
        }
    }

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy