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

org.snpeff.stats.CountByKey Maven / Gradle / Ivy

The newest version!
package org.snpeff.stats;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;

/**
 * Counters indexed by key.
 * It's a generic version of CountByType
 * 
 * @author pcingola
 */
@SuppressWarnings("serial")
public class CountByKey implements Serializable {

	HashMap countByKey;

	public CountByKey() {
		countByKey = new HashMap();
	}

	/**
	 * How many counts of this type?
	 * @param key
	 * @return
	 */
	public long get(T key) {
		return getCount(countByKey, key);
	}

	long getCount(HashMap hash, T key) {
		Long count = hash.get(key);
		return count != null ? count : 0;
	}

	/**
	 * Increment counter in a hash
	 * @param hash
	 * @param key
	 */
	void inc(HashMap hash, T key, int toAdd) {
		Long count = hash.get(key);
		if (count == null) count = 0L;
		count += toAdd;
		hash.put(key, count);
	}

	/**
	 * Increment (by 1)
	 * @param key
	 */
	public void inc(T key) {
		inc(countByKey, key, 1);
	}

	/**
	 * Increment counter for a given type
	 * @param key
	 */
	public void inc(T key, int increment) {
		inc(countByKey, key, increment);
	}

	public boolean isEmpty() {
		return countByKey.isEmpty();
	}

	public Set keySet() {
		return countByKey.keySet();
	}

	/**
	 * Maximum count
	 */
	public long max() {
		long max = Long.MIN_VALUE;
		for (Long count : countByKey.values())
			max = Math.max(max, count);
		return max;
	}

	/**
	 * Minimum count
	 */
	public long min() {
		long min = Long.MAX_VALUE;
		for (Long count : countByKey.values())
			min = Math.min(min, count);
		return min;
	}
	
	public 	int size() { return countByKey.size();}

	@Override
	public String toString() {
		StringBuffer out = new StringBuffer();
		for (T type : keySet())
			out.append(type + "\t" + get(type) + "\n");

		return out.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy