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

org.snpeff.collections.AutoHashMap Maven / Gradle / Ivy

The newest version!
package org.snpeff.collections;

import java.util.HashMap;

/**
 * A Hash that creates new elements if they don't exists
 * @author pcingola
 *
 * @param 
 * @param 
 */
public class AutoHashMap extends HashMap {

	private static final long serialVersionUID = 255818677257868365L;

	V instance;

	public AutoHashMap(V instance) {
		super();
		this.instance = instance;
	}

	@SuppressWarnings("unchecked")
	public V getOrCreate(K key) {
		V v = get(key);
		if (v == null) {
			try {
				v = (V) instance.getClass().newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			put(key, v);
		}
		return v;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy