/* * MultiMap.java * Copyright (C) 2004-2005, Bjorn Bringert (bringert@cs.chalmers.se) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package se.chalmers.cs.gf.util; import java.util.*; /** * Maps keys to sets of values. * @param The type of the keys * @param The type of the values. */ public class MultiMap { /** * Invariant: the sets have at least one element. */ private Map> map; public MultiMap() { this.map = new HashMap>(); } public Set get(K key) { Set s = map.get(key); return s == null ? Collections.emptySet() : s; } public boolean put(K key, V value) { Set s = map.get(key); if (s == null) { s = new HashSet(); map.put(key, s); } return s.add(value); } public Set keySet() { return map.keySet(); } /** * Checks whether there is at least one value for the * given key. */ public boolean containsKey(K key) { return map.containsKey(key); } public String toString() { return map.toString(); } /** * Returns the all the values in the map. * FIXME: The result is not backed by the map. */ public Collection values() { List l = new ArrayList(); for (Set s : map.values()) for (V v : s) l.add(v); return l; } }