001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.util;
018:
019: import java.io.Serializable;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: public class MultiMap implements Map, Serializable {
026:
027: private Map m_map = null;
028: private Class m_collectionClass;
029:
030: public MultiMap(Map map, Class collectionClass) {
031: m_map = map;
032: m_collectionClass = collectionClass;
033: }
034:
035: public Object put(Object key, Object value) {
036: Collection c = null;
037:
038: if ((c = (Collection) m_map.get(key)) == null) {
039: try {
040: c = (Collection) m_collectionClass.newInstance();
041: } catch (InstantiationException e) {
042: throw new IllegalStateException(e.getClass().getName()
043: + ": " + e.getMessage());
044: } catch (IllegalAccessException e) {
045: throw new IllegalStateException(e.getClass().getName()
046: + ": " + e.getMessage());
047: }
048:
049: m_map.put(key, c);
050: }
051:
052: c.add(value);
053: return (c);
054: }
055:
056: public void putItems(Object key, Collection items) {
057: m_map.put(key, items);
058: }
059:
060: public int size() {
061: return (m_map.size());
062: }
063:
064: public void clear() {
065: m_map.clear();
066: }
067:
068: public boolean isEmpty() {
069: return (m_map.isEmpty());
070: }
071:
072: public boolean containsKey(Object key) {
073: return (m_map.containsKey(key));
074: }
075:
076: public boolean containsValue(Object value) {
077: for (Iterator itr = values().iterator(); itr.hasNext();) {
078: Collection c = (Collection) itr.next();
079: if (c.contains(value))
080: return (true);
081: }
082: return (false);
083: }
084:
085: public Collection values() {
086: return (m_map.values());
087: }
088:
089: public void putAll(Map t) {
090: for (Iterator itr = t.entrySet().iterator(); itr.hasNext();) {
091: Map.Entry entry = (Map.Entry) itr.next();
092: put(entry.getKey(), entry.getValue());
093: }
094: }
095:
096: public Set entrySet() {
097: return (m_map.entrySet());
098: }
099:
100: public Set keySet() {
101: return (m_map.keySet());
102: }
103:
104: public Object get(Object key) {
105: Object obj = null;
106: if ((obj = m_map.get(key)) == null) {
107: try {
108: obj = m_collectionClass.newInstance();
109: } catch (InstantiationException e) {
110: throw new IllegalStateException(e.getClass().getName()
111: + ": " + e.getMessage());
112: } catch (IllegalAccessException e) {
113: throw new IllegalStateException(e.getClass().getName()
114: + ": " + e.getMessage());
115: }
116: putItems(key, (Collection) obj);
117: }
118: return (obj);
119: }
120:
121: public Collection getItems(Object key) {
122: return ((Collection) get(key));
123: }
124:
125: public Object remove(Object key) {
126: return (m_map.remove(key));
127: }
128:
129: public Object remove(Object key, Object value) {
130: Collection c = (Collection) m_map.get(key);
131: c.remove(value);
132: return (value);
133: }
134:
135: }
|