1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
15 package org.simantics.utils.datastructures;
17 import gnu.trove.map.hash.THashMap;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
24 import java.util.Map.Entry;
28 * MapList is a data structure with map on left side and arraylist on right side.
31 * @author Toni Kalajainen
33 public class MapList<L, R> {
35 @SuppressWarnings("rawtypes")
36 public static final MapList EMPTY_MAPLIST = new MapList() {
37 private static final String IMMUTABLE_MSG = "Cannot modify immutable empty MapList";
40 public void add(Object key) {
41 throw new UnsupportedOperationException(IMMUTABLE_MSG);
44 public void add(Object key, int index, Object value) {
45 throw new UnsupportedOperationException(IMMUTABLE_MSG);
48 public void add(Object key, Object value) {
49 throw new UnsupportedOperationException(IMMUTABLE_MSG);
52 public void addAll(Object key, Collection values) {
53 throw new UnsupportedOperationException(IMMUTABLE_MSG);
57 throw new UnsupportedOperationException(IMMUTABLE_MSG);
60 public boolean remove(Object key) {
61 throw new UnsupportedOperationException(IMMUTABLE_MSG);
63 public boolean remove(Object key, Object value) {
64 throw new UnsupportedOperationException(IMMUTABLE_MSG);
68 @SuppressWarnings("unchecked")
69 public static <L, R> MapList<L, R> emptyMapList() {
73 protected Map<L, List<R>> lists;
76 lists = new THashMap<L, List<R>>();
79 @SuppressWarnings("unchecked")
80 public MapList( Class<?> mapClass ) {
82 lists = (Map<L, List<R>>) mapClass.newInstance();
83 } catch (InstantiationException e) {
84 throw new RuntimeException( e );
85 } catch (IllegalAccessException e) {
86 throw new RuntimeException( e );
90 public MapList(MapList<L, R> copyFrom) {
91 for (Entry<L, List<R>> e : copyFrom.lists.entrySet())
92 lists.put( e.getKey(), new ArrayList<R>(e.getValue()) );
95 public static <L, R> MapList<L, R> use( Map<L, List<R>> map ) {
96 MapList<L, R> result = new MapList<L, R>();
101 public void add(L key) {
102 getOrCreateList(key);
105 public void add(L key, R value)
107 List<R> list = getOrCreateList(key);
111 public void add(L key, int index, R value)
113 ArrayList<R> list = getOrCreateList(key);
114 list.add(index, value);
117 public void addAll(L key, Collection<R> values) {
118 ArrayList<R> list = getOrCreateList(key);
122 private ArrayList<R> getOrCreateList(L key)
124 ArrayList<R> list = (ArrayList<R>) lists.get(key);
126 list = new ArrayList<R>(1);
127 lists.put(key, list);
132 private List<R> getList(L key)
134 return lists.get(key);
137 public boolean remove(L key, R value)
139 List<R> list = getList(key);
140 if (list==null) return false;
141 boolean result = list.remove(value);
147 public boolean remove(L key)
149 List<R> list = getList(key);
150 if (list==null) return false;
160 public L[] getKeys(L[] list)
162 return lists.keySet().toArray(list);
165 public Set<L> getKeys()
167 return lists.keySet();
170 public int getKeySize() {
174 public boolean containsKey(L key)
176 return lists.containsKey(key);
179 public boolean contains(L key, R obj)
181 List<R> l = lists.get(key);
182 if (l==null) return false;
183 return l.contains(obj);
187 public R[] getValues(L key, R[] list)
189 List<R> l = lists.get(key);
190 if (l==null) return null;
191 return l.toArray(list);
196 * the key to get values for
198 * the list to fill with existing values for specified key. Fills
199 * this array with at maximum as many values as there is room for
200 * in the array even if there are more values available in the
201 * maplist for the specified key.
202 * @return the amount of values existing for the key. May be smaller or
203 * larger than the size of the provided list. If smaller, only the
204 * first array indexes will be filled with data and if larger, all
205 * array indexes will be filled with data.
207 public int getAtMostValues(L key, R[] list)
209 List<R> l = lists.get(key);
210 if (l==null) return 0;
211 int valueCount = l.size();
212 int size = Math.min(valueCount, list.length);
213 for (int i = 0; i < size; ++i)
219 * Returns a the internal list values for the specified key. The list is
220 * valid as long as it contains elements. The list should not be modified
221 * but the return value of this method does not enforce it like
222 * {@link #getValues(Object)} does. Use this method when you know you will
223 * not risk a 3rd party modifying the returned list and you want to avoid
224 * the cost of extra memory allocation through
225 * {@link Collections#unmodifiableList(List)}.
228 * the key to look values for
229 * @return empty unmodifiable list if there is no list with the specified
230 * key, otherwise an unmodifiable version of the stored list
232 public List<R> getValuesUnsafe(L key)
234 List<R> l = lists.get(key);
235 return l != null ? l : Collections.<R>emptyList();
239 * Returns a read-only reference to the values. The list is valid as long as
240 * it contains elements.
243 * @return empty unmodifiable list if there is no list with the specified key,
244 * otherwise an unmodifiable version of the stored list
246 public List<R> getValues(L key)
248 List<R> l = lists.get(key);
249 if (l==null) return Collections.emptyList();
250 return Collections.unmodifiableList(l);
254 * Returns a copy of the values
257 * @return empty unmodifiable list if there is no list with the specified key,
258 * otherwise a copy of the stored list
260 public List<R> getValuesSnapshot(L key)
262 List<R> l = lists.get(key);
263 if (l==null) return Collections.emptyList();
264 return new ArrayList<R>(l);
267 public List<R> getAllValuesSnapshot()
269 return getAllValuesSnapshot(null);
272 public List<R> getAllValuesSnapshot(List<R> result)
275 result = new ArrayList<R>();
276 for (List<R> right : lists.values()) {
277 result.addAll(right);
282 public boolean isEmpty() {
283 return lists.isEmpty();
287 * Makes _this_ maplist immutable.
289 public void makeImmutable() {
290 for (Entry<L, List<R>> e : lists.entrySet())
291 lists.put(e.getKey(), Collections.unmodifiableList(e.getValue()));
292 lists = Collections.unmodifiableMap(lists);