root/trunk/ProjectFortress/src/com/sun/fortress/useful/MultiMap.java @ 3259

Revision 3259, 2.7 KB (checked in by dr2chase, 11 months ago)

[repository, useful] Work on foreign-java interface generation etc.

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.useful;
19
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Map;
23import java.util.Set;
24
25/**
26 * A MultiMap is implemented as a Map from keys to sets of
27 * values, and in fact the regular Map methods act on sets,
28 * not items.
29 */
30public class MultiMap<K, V> extends HashMap<K, Set<V>> implements IMultiMap<K,V> {
31
32    /**
33     *
34     */
35    private static final long serialVersionUID = 3275403475085923977L;
36    public MultiMap() {
37        super();
38    }
39
40
41
42    public MultiMap(Map<? extends K, ? extends Set<V>> m) {
43        super(m);
44    }
45
46    // TODO can we get the Java generics right on this?
47    public void addInverse(Map<V, K> m) {
48        for (Map.Entry<V, K> e : m.entrySet()) {
49            putItem((K)e.getValue(), (V)e.getKey());
50        }
51
52    }
53
54    public Set<V> putItem(K k, V v) {
55        Set<V> s = get(k);
56        if (s == null) {
57            s = new HashSet<V>();
58            s.add(v);
59            put(k, s);
60        } else {
61            s.add(v);
62        }
63        return s;
64    }
65   
66    /**
67     * Ensures that k is in the map, perhaps mapping to an empty set.
68     * @param k
69     * @return
70     */
71    public Set<V> putKey(K k) {
72        Set<V> s = get(k);
73        if (s == null) {
74            s = new HashSet<V>();
75            put(k,s);
76        }
77        return s;
78    }
79    public Set<V> putItems(K k, Set<V> vs) {
80        Set<V> s = get(k);
81        if (s == null) {
82            s = new HashSet<V>(vs);
83            put(k, s);
84        } else {
85            s.addAll(vs);
86        }
87        return s;
88    }
89    public Set<V> removeItem(K k, V v) {
90        Set<V> s = get(k);
91        if (s != null) {
92            s.remove(v);
93            if (s.isEmpty()) {
94                remove(k);
95                s = null;
96            }
97        }
98        return s;
99    }
100    public Set<V> removeItemAllowEmpty(K k, V v) {
101        Set<V> s = get(k);
102        if (s != null) {
103            s.remove(v);
104        }
105        return s;
106    }
107
108}
Note: See TracBrowser for help on using the browser.