1 | package spiffy.core.util; |
2 | |
3 | import java.util.HashMap; |
4 | |
5 | /** |
6 | * Shortcut way to build hashmaps. It's great for making concise tests. |
7 | * <p> |
8 | * Instead of |
9 | * |
10 | * <pre> |
11 | * HashMap<String, Integer> map = new HashMap<String, Integer>(); |
12 | * map.put("one", 1); |
13 | * map.put("two", 2); |
14 | * ... |
15 | * </pre> |
16 | * |
17 | * you can now chain the inserts and use the more familiar add(). |
18 | * |
19 | * <pre> |
20 | * HashMap<String, Integer> map = new HashMapBuilder<String, Integer>().add("one", 1).add("two", 2).build(); |
21 | * </pre> |
22 | * |
23 | * @author Kasper B. Graversen, (c) 2007-2008 |
24 | */ |
25 | public class HashMapBuilder<K, V> { |
26 | HashMap<K, V> map; |
27 | |
28 | public HashMapBuilder() { |
29 | map = new HashMap<K, V>(); |
30 | } |
31 | |
32 | /** |
33 | * add a key-value pair |
34 | */ |
35 | public HashMapBuilder<K, V> add(final K key, final V value) { |
36 | map.put(key, value); |
37 | return this; |
38 | } |
39 | |
40 | /** |
41 | * build the hashmap. |
42 | */ |
43 | public HashMap<K, V> build() { |
44 | return map; |
45 | } |
46 | } |