package spiffy.core.util; import java.util.HashMap; /** * Shortcut way to build hashmaps. It's great for making concise tests. * <p> * Instead of * * <pre> * HashMap<String, Integer> map = new HashMap<String, Integer>(); * map.put("one", 1); * map.put("two", 2); * ... * </pre> * * you can now chain the inserts and use the more familiar add(). * * <pre> * HashMap<String, Integer> map = new HashMapBuilder<String, Integer>().add("one", 1).add("two", 2).build(); * </pre> * * @author Kasper B. Graversen, (c) 2007-2008 */ public class HashMapBuilder<K, V> { HashMap<K, V> map; public HashMapBuilder() { map = new HashMap<K, V>(); } /** * add a key-value pair */ public HashMapBuilder<K, V> add(final K key, final V value) { map.put(key, value); return this; } /** * build the hashmap. */ public HashMap<K, V> build() { return map; } }