package frame.util; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class MapList implements Collection { protected List itemList; protected Map itemMap; public MapList() { itemList = new ArrayList(); itemMap = new HashMap(); } public void addAll(Map map) { Set keys = map.keySet(); for (Object key: keys) { add(key, map.get(key)); } } public void add(Object key, T item) { itemList.add(item); if (key == null) { key = "empty"; } if (key instanceof String) { key = ((String) key).toLowerCase(); } itemMap.put(key, item); } public T get(String key) { if (key == null) { key = "empty"; } key = key.toLowerCase(); return itemMap.get(key); } public T remove(String key) { if (key == null) { return null; } T obj = itemMap.get(key.toLowerCase()); if (obj != null) { itemMap.remove(key); itemList.remove(obj); } return obj; } public List getItemList() { return itemList; } public boolean isEmpty() { return itemList.isEmpty(); } public int size() { return itemList.size(); } public int mapSize() { return itemMap.size(); } @Override 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(key); } return builder.toString(); } public Set keySet() { return itemMap.keySet(); } public boolean equals(MapList another) { if (another == null) { return false; } if (this == another) { return true; } Set keys = itemMap.keySet(); for (Object key: keys) { if (!another.contains(key)) { return false; } } Set anotherKeys = another.itemMap.keySet(); for (Object anotherKey: anotherKeys) { if (!contains(anotherKey)) { return false; } } return true; } @Override public boolean contains(Object key) { if (key == null) { return false; } if (key instanceof String) { key = ((String)key).toLowerCase(); } return itemMap.containsKey(key); } @Override public Object[] toArray() { return itemList.toArray(); } @SuppressWarnings("hiding") @Override public T[] toArray(T[] a) { return itemList.toArray(a); } @Override public boolean add(T e) { // TODO Auto-generated method stub return false; } @Override public boolean remove(Object o) { // TODO Auto-generated method stub return false; } @Override public boolean containsAll(Collection c) { // TODO Auto-generated method stub return false; } @Override public boolean addAll(Collection c) { // TODO Auto-generated method stub return false; } @Override public boolean removeAll(Collection c) { // TODO Auto-generated method stub return false; } @Override public boolean retainAll(Collection c) { // TODO Auto-generated method stub return false; } }