blob: e4776f7dbd224a6d669ae92e927e835109d18c1c [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.map;
2
3import java.util.*;
4
5/**
Stuart McCulloch2286f232012-06-15 13:27:53 +00006 * Easy way to build a map: Map<String,Integer> s = MAP.$("a",2).$("b",3);
Stuart McCullochbb014372012-06-07 21:57:32 +00007 */
8public class MAP {
9
Stuart McCulloch2286f232012-06-15 13:27:53 +000010 static public class MAPX<K, V> extends LinkedHashMap<K,V> {
Stuart McCullochbb014372012-06-07 21:57:32 +000011 private static final long serialVersionUID = 1L;
Stuart McCulloch2286f232012-06-15 13:27:53 +000012
13 public MAPX<K,V> $(K key, V value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000014 put(key, value);
15 return this;
16 }
17
Stuart McCulloch2286f232012-06-15 13:27:53 +000018 public MAPX<K,V> $(Map<K,V> all) {
Stuart McCullochbb014372012-06-07 21:57:32 +000019 putAll(all);
20 return this;
21 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000022
Stuart McCullochbb014372012-06-07 21:57:32 +000023 public Hashtable<K,V> asHashtable() {
24 return new Hashtable<K,V>(this);
25 }
26 }
27
Stuart McCulloch2286f232012-06-15 13:27:53 +000028 public static <Kx, Vx> MAPX<Kx,Vx> $(Kx key, Vx value) {
29 MAPX<Kx,Vx> map = new MAPX<Kx,Vx>();
Stuart McCullochbb014372012-06-07 21:57:32 +000030 map.put(key, value);
31 return map;
32 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000033
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 McCullochbb014372012-06-07 21:57:32 +000037 K k = e.nextElement();
38 V v = dict.get(k);
Stuart McCulloch2286f232012-06-15 13:27:53 +000039 map.put(k, v);
Stuart McCullochbb014372012-06-07 21:57:32 +000040 }
41 return map;
42 }
43}