package foundation.util; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MapList implements Iterable { protected List itemList; protected Map itemMap; protected Map indexMap; protected boolean ignoreCase; public MapList() { itemList = new ArrayList(); itemMap = new HashMap(); ignoreCase = true; } public MapList(int size) { itemList = new ArrayList(size); itemMap = new HashMap(); ignoreCase = true; } public MapList(boolean ignoreCase) { this(); this.ignoreCase = ignoreCase; } public void addAll(Map map) { Set keys = map.keySet(); for (K key: keys) { add(key, map.get(key)); } } @SuppressWarnings("unchecked") public void add(K key, T item) { if (key == null) { return; } if (ignoreCase && (key instanceof String)) { key = (K)((String)key).toLowerCase(); } T old = itemMap.get(key); if (old != null) { itemList.remove(old); } itemMap.put(key, item); itemList.add(item); } @SuppressWarnings("unchecked") public void set(K key, T item) { if (key == null) { return; } if (indexMap == null) { indexMap = new HashMap(); } if (ignoreCase) { key = (K)((String)key).toLowerCase(); } if (!itemMap.containsKey(key)) { itemMap.put(key, item); indexMap.put(key, itemList.size()); itemList.add(item); } else { Integer idx = indexMap.get(key); itemList.set(idx, item); } } @SuppressWarnings("unchecked") public T get(K key) { if (key == null) { return null; } if (ignoreCase && (key instanceof String)) { key = (K)((String)key).toLowerCase(); } return itemMap.get(key); } public T get(int idx) { return itemList.get(idx); } @SuppressWarnings("unchecked") public T remove(K key) { if (key == null) { return null; } if (ignoreCase && (key instanceof String)) { key = (K)((String)key).toLowerCase(); } T obj = itemMap.get(key); if (obj != null) { itemMap.remove(key); itemList.remove(obj); } return obj; } @SuppressWarnings("unchecked") public void addBeforeLast(K key, T item) { if (key == null) { return; } if (ignoreCase && (key instanceof String)) { key = (K)((String)key).toLowerCase(); } if (itemList.isEmpty()) { add(key, item); } itemMap.put(key, item); T last = itemList.remove(itemList.size() - 1); itemList.add(item); itemList.add(last); } public List getItemList() { return itemList; } public void setList(List list) { itemList = list; } @SuppressWarnings("unchecked") public void sortList(Comparator comparator) { int max = itemList.size(); T[] array = (T[]) new Object[max]; for (int i = 0; i < max; i++) { array[i] = itemList.get(i); } Arrays.sort(array, comparator); List result = Arrays.asList(array); itemList = result; } public Map getItemMap() { return itemMap; } public Set getKeySet() { return itemMap.keySet(); } public boolean isEmpty() { return itemList.isEmpty(); } public int size() { return itemList.size(); } public int mapSize() { return itemMap.size(); } public Iterator iterator() { return itemList.iterator(); } public void clear() { itemList.clear(); itemMap.clear(); } public String toString(String separator) { ContentBuilder builder = new ContentBuilder(separator); for (Object key: itemMap.keySet()) { builder.append(String.valueOf(key)); } return builder.toString(); } public String mapToString(String separator) { ContentBuilder builder = new ContentBuilder(separator); Set> entrySet = itemMap.entrySet(); for (Entry entry: entrySet) { String key = String.valueOf(entry.getKey()); String value = String.valueOf(entry.getValue()); if (value.startsWith("{") || value.startsWith("[")) { builder.append( Util.doubleQuotedStr(key) + ":" + value); } else { builder.append( Util.doubleQuotedStr(key) + ":" + Util.doubleQuotedStr(value)); } } return "{" + builder.toString() + "}"; } public Set keySet() { return itemMap.keySet(); } public boolean contains(Object key) { if (key == null) { return false; } if (ignoreCase && (key instanceof String)) { key = ((String)key).toLowerCase(); } return itemMap.containsKey(key); } public Object[] toArray() { return itemList.toArray(); } @SuppressWarnings("hiding") public T[] toArray(T[] a) { return itemList.toArray(a); } @SuppressWarnings("unchecked") public void sort(Comparator comparator) { Object[] array = new Object[itemList.size()]; itemList.toArray(array); Arrays.sort(array, comparator); itemList.clear(); for (Object obj: array) { itemList.add((T) obj); } } }