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<K, T> implements Iterable<T> {
|
|
protected List<T> itemList;
|
protected Map<K, T> itemMap;
|
protected Map<K, Integer> indexMap;
|
protected boolean ignoreCase;
|
|
|
public MapList() {
|
itemList = new ArrayList<T>();
|
itemMap = new HashMap<K, T>();
|
ignoreCase = true;
|
}
|
|
public MapList(int size) {
|
itemList = new ArrayList<T>(size);
|
itemMap = new HashMap<K, T>();
|
ignoreCase = true;
|
}
|
|
public MapList(boolean ignoreCase) {
|
this();
|
this.ignoreCase = ignoreCase;
|
}
|
|
public void addAll(Map<K, T> map) {
|
Set<K> 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<K, Integer>();
|
}
|
|
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<T> getItemList() {
|
return itemList;
|
}
|
|
public void setList(List<T> list) {
|
itemList = list;
|
}
|
|
@SuppressWarnings("unchecked")
|
public void sortList(Comparator<T> 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<T> result = Arrays.asList(array);
|
itemList = result;
|
}
|
|
public Map<K, T> getItemMap() {
|
return itemMap;
|
}
|
|
public Set<K> getKeySet() {
|
return itemMap.keySet();
|
}
|
|
public boolean isEmpty() {
|
return itemList.isEmpty();
|
}
|
|
public int size() {
|
return itemList.size();
|
}
|
|
public int mapSize() {
|
return itemMap.size();
|
}
|
|
public Iterator<T> 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<Entry<K, T>> entrySet = itemMap.entrySet();
|
|
for (Entry<K, T> 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<K> 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> T[] toArray(T[] a) {
|
return itemList.toArray(a);
|
}
|
|
@SuppressWarnings("unchecked")
|
public void sort(Comparator<Object> comparator) {
|
Object[] array = new Object[itemList.size()];
|
itemList.toArray(array);
|
Arrays.sort(array, comparator);
|
|
itemList.clear();
|
|
for (Object obj: array) {
|
itemList.add((T) obj);
|
}
|
}
|
|
}
|