001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.util;
034:
035: import java.util.*;
036:
037: import com.vividsolutions.jts.util.Assert;
038:
039: /**
040: * A Map whose values are Collections.
041: */
042: public class CollectionMap implements Map {
043: private Map map;
044: private Class collectionClass = ArrayList.class;
045:
046: /**
047: * Creates a CollectionMap backed by the given Map class.
048: * @param mapClass a Class that implements Map
049: */
050: public CollectionMap(Class mapClass) {
051: try {
052: map = (Map) mapClass.newInstance();
053: } catch (InstantiationException e) {
054: Assert.shouldNeverReachHere();
055: } catch (IllegalAccessException e) {
056: Assert.shouldNeverReachHere();
057: }
058: }
059:
060: public CollectionMap(Class mapClass, Class collectionClass) {
061: this .collectionClass = collectionClass;
062: try {
063: map = (Map) mapClass.newInstance();
064: } catch (InstantiationException e) {
065: Assert.shouldNeverReachHere();
066: } catch (IllegalAccessException e) {
067: Assert.shouldNeverReachHere();
068: }
069: }
070:
071: /**
072: * Creates a CollectionMap.
073: */
074: public CollectionMap() {
075: this (HashMap.class);
076: }
077:
078: private Collection getItemsInternal(Object key) {
079: Collection collection = (Collection) map.get(key);
080: if (collection == null) {
081: try {
082: collection = (Collection) collectionClass.newInstance();
083: } catch (InstantiationException e) {
084: Assert.shouldNeverReachHere();
085: } catch (IllegalAccessException e) {
086: Assert.shouldNeverReachHere();
087: }
088: map.put(key, collection);
089: }
090: return collection;
091: }
092:
093: /**
094: * Adds the item to the Collection at the given key, creating a new Collection if
095: * necessary.
096: * @param key the key to the Collection to which the item should be added
097: * @param item the item to add
098: */
099: public void addItem(Object key, Object item) {
100: getItemsInternal(key).add(item);
101: }
102:
103: public void removeItem(Object key, Object item) {
104: getItemsInternal(key).remove(item);
105: }
106:
107: public void clear() {
108: map.clear();
109: }
110:
111: /**
112: * Adds the items to the Collection at the given key, creating a new Collection if
113: * necessary.
114: * @param key the key to the Collection to which the items should be added
115: * @param items the items to add
116: */
117: public void addItems(Object key, Collection items) {
118: for (Iterator i = items.iterator(); i.hasNext();) {
119: addItem(key, i.next());
120: }
121: }
122:
123: public void addItems(CollectionMap other) {
124: for (Iterator i = other.keySet().iterator(); i.hasNext();) {
125: Object key = i.next();
126: addItems(key, other.getItems(key));
127: }
128: }
129:
130: /**
131: * Returns the values.
132: * @return a view of the values, backed by this CollectionMap
133: */
134: public Collection values() {
135: return map.values();
136: }
137:
138: /**
139: * Returns the keys.
140: * @return a view of the keys, backed by this CollectionMap
141: */
142: public Set keySet() {
143: return map.keySet();
144: }
145:
146: /**
147: * Returns the number of mappings.
148: * @return the number of key-value pairs
149: */
150: public int size() {
151: return map.size();
152: }
153:
154: public Object get(Object key) {
155: return getItems(key);
156: }
157:
158: public Collection getItems(Object key) {
159: return Collections
160: .unmodifiableCollection(getItemsInternal(key));
161: }
162:
163: public Object remove(Object key) {
164: return map.remove(key);
165: }
166:
167: public boolean containsKey(Object key) {
168: return map.containsKey(key);
169: }
170:
171: public boolean containsValue(Object value) {
172: return map.containsValue(value);
173: }
174:
175: public Set entrySet() {
176: return map.entrySet();
177: }
178:
179: public boolean isEmpty() {
180: return map.isEmpty();
181: }
182:
183: public Object put(Object key, Object value) {
184: Assert.isTrue(value instanceof Collection);
185:
186: return map.put(key, value);
187: }
188:
189: public void putAll(Map map) {
190: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
191: Object key = i.next();
192: //Delegate to #put so that the assertion is made. [Jon Aquino]
193: put(key, map.get(key));
194: }
195: }
196:
197: public void removeItems(Object key, Collection items) {
198: getItemsInternal(key).removeAll(items);
199: }
200:
201: public Map getMap() {
202: return map;
203: }
204:
205: }
|