Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.map; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 6 | * Easy way to build a map: Map<String,Integer> s = MAP.$("a",2).$("b",3); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 7 | */ |
| 8 | public class MAP { |
| 9 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | static public class MAPX<K, V> extends LinkedHashMap<K,V> { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | private static final long serialVersionUID = 1L; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | |
| 13 | public MAPX<K,V> $(K key, V value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | put(key, value); |
| 15 | return this; |
| 16 | } |
| 17 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | public MAPX<K,V> $(Map<K,V> all) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | putAll(all); |
| 20 | return this; |
| 21 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 22 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | public Hashtable<K,V> asHashtable() { |
| 24 | return new Hashtable<K,V>(this); |
| 25 | } |
| 26 | } |
| 27 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | public static <Kx, Vx> MAPX<Kx,Vx> $(Kx key, Vx value) { |
| 29 | MAPX<Kx,Vx> map = new MAPX<Kx,Vx>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | map.put(key, value); |
| 31 | return map; |
| 32 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | |
| 34 | public <K, V> Map<K,V> dictionary(Dictionary<K,V> dict) { |
| 35 | Map<K,V> map = new LinkedHashMap<K,V>(); |
| 36 | for (Enumeration<K> e = dict.keys(); e.hasMoreElements();) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | K k = e.nextElement(); |
| 38 | V v = dict.get(k); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | map.put(k, v); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | } |
| 41 | return map; |
| 42 | } |
| 43 | } |