一、前言
关于ini4j源码包定义org.ini4j.MultiMap、org.ini4j.BasicMultiMap定义接口及实现类,除了默认LinkedHashMap实现外,可以通过任意自定义Map来实例化,可以将不同Map合并在同一个key的集合List中,详情参见源码说明部分。
二、源码说明
1.MultiMap接口
package org.ini4j;@b@@b@import java.util.List;@b@import java.util.Map;@b@@b@public abstract interface MultiMap<K, V> extends Map<K, V>@b@{@b@ public abstract List<V> getAll(Object paramObject);@b@@b@ public abstract void add(K paramK, V paramV);@b@@b@ public abstract void add(K paramK, V paramV, int paramInt);@b@@b@ public abstract V get(Object paramObject, int paramInt);@b@@b@ public abstract int length(Object paramObject);@b@@b@ public abstract V put(K paramK, V paramV, int paramInt);@b@@b@ public abstract List<V> putAll(K paramK, List<V> paramList);@b@@b@ public abstract V remove(Object paramObject, int paramInt);@b@}
2.BasicMultiMap实现类
package org.ini4j;@b@@b@import java.io.Serializable;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashSet;@b@import java.util.Iterator;@b@import java.util.LinkedHashMap;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@@b@public class BasicMultiMap<K, V>@b@ implements MultiMap<K, V>, Serializable@b@{@b@ private static final long serialVersionUID = 4716749660560043989L;@b@ private final Map<K, List<V>> _impl;@b@@b@ public BasicMultiMap()@b@ {@b@ this(new LinkedHashMap());@b@ }@b@@b@ public BasicMultiMap(Map<K, List<V>> impl)@b@ {@b@ this._impl = impl;@b@ }@b@@b@ public List<V> getAll(Object key)@b@ {@b@ return ((List)this._impl.get(key));@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return this._impl.isEmpty();@b@ }@b@@b@ public void add(K key, V value)@b@ {@b@ getList(key, true).add(value);@b@ }@b@@b@ public void add(K key, V value, int index)@b@ {@b@ getList(key, true).add(index, value);@b@ }@b@@b@ public void clear()@b@ {@b@ this._impl.clear();@b@ }@b@@b@ public boolean containsKey(Object key)@b@ {@b@ return this._impl.containsKey(key);@b@ }@b@@b@ public boolean containsValue(Object value)@b@ {@b@ boolean ret = false;@b@@b@ for (List all : this._impl.values())@b@ {@b@ if (all.contains(value))@b@ {@b@ ret = true;@b@@b@ break;@b@ }@b@ }@b@@b@ return ret;@b@ }@b@@b@ public Set<Map.Entry<K, V>> entrySet()@b@ {@b@ Set ret = new HashSet();@b@@b@ for (Iterator i$ = keySet().iterator(); i$.hasNext(); ) { Object key = i$.next();@b@@b@ ret.add(new ShadowEntry(this, key));@b@ }@b@@b@ return ret;@b@ }@b@@b@ public V get(Object key)@b@ {@b@ List values = getList(key, false);@b@@b@ return ((values == null) ? null : values.get(values.size() - 1));@b@ }@b@@b@ public V get(Object key, int index)@b@ {@b@ List values = getList(key, false);@b@@b@ return ((values == null) ? null : values.get(index));@b@ }@b@@b@ public Set<K> keySet()@b@ {@b@ return this._impl.keySet();@b@ }@b@@b@ public int length(Object key)@b@ {@b@ List values = getList(key, false);@b@@b@ return ((values == null) ? 0 : values.size());@b@ }@b@@b@ public V put(K key, V value)@b@ {@b@ Object ret = null;@b@ List values = getList(key, true);@b@@b@ if (values.isEmpty())@b@ {@b@ values.add(value);@b@ }@b@ else@b@ {@b@ ret = values.set(values.size() - 1, value);@b@ }@b@@b@ return ret;@b@ }@b@@b@ public V put(K key, V value, int index)@b@ {@b@ return getList(key, false).set(index, value);@b@ }@b@@b@ public void putAll(Map<? extends K, ? extends V> map)@b@ {@b@ MultiMap mm;@b@ Iterator i$;@b@ Iterator i$;@b@ if (map instanceof MultiMap)@b@ {@b@ mm = (MultiMap)map;@b@@b@ for (i$ = mm.keySet().iterator(); i$.hasNext(); ) { Object key = i$.next();@b@@b@ putAll(key, mm.getAll(key));@b@ }@b@ }@b@ else@b@ {@b@ for (i$ = map.keySet().iterator(); i$.hasNext(); ) { Object key = i$.next();@b@@b@ put(key, map.get(key));@b@ }@b@ }@b@ }@b@@b@ public List<V> putAll(K key, List<V> values)@b@ {@b@ List ret = (List)this._impl.get(key);@b@@b@ this._impl.put(key, new ArrayList(values));@b@@b@ return ret;@b@ }@b@@b@ public V remove(Object key)@b@ {@b@ List prev = (List)this._impl.remove(key);@b@@b@ return ((prev == null) ? null : prev.get(0));@b@ }@b@@b@ public V remove(Object key, int index)@b@ {@b@ Object ret = null;@b@ List values = getList(key, false);@b@@b@ if (values != null)@b@ {@b@ ret = values.remove(index);@b@ if (values.isEmpty())@b@ {@b@ this._impl.remove(key);@b@ }@b@ }@b@@b@ return ret;@b@ }@b@@b@ public int size()@b@ {@b@ return this._impl.size();@b@ }@b@@b@ public String toString()@b@ {@b@ return this._impl.toString();@b@ }@b@@b@ public Collection<V> values()@b@ {@b@ List all = new ArrayList(this._impl.size());@b@@b@ for (List values : this._impl.values())@b@ {@b@ all.addAll(values);@b@ }@b@@b@ return all;@b@ }@b@@b@ private List<V> getList(Object key, boolean create)@b@ {@b@ List values = (List)this._impl.get(key);@b@@b@ if ((values == null) && (create))@b@ {@b@ values = new ArrayList();@b@ this._impl.put(key, values);@b@ }@b@@b@ return values;@b@ }@b@@b@ class ShadowEntry implements Map.Entry<K, V>@b@ {@b@ private final K _key;@b@@b@ ShadowEntry()@b@ {@b@ this._key = key;@b@ }@b@@b@ public K getKey()@b@ {@b@ return this._key;@b@ }@b@@b@ public V getValue()@b@ {@b@ return this.this$0.get(this._key);@b@ }@b@@b@ public V setValue()@b@ {@b@ return this.this$0.put(this._key, value);@b@ }@b@ }@b@}