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

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

[repository, ffi] Generated APIs now use proper import syntax, seem to pass static checking

Line 
1/*******************************************************************************
2    Copyright 2009 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.Collection;
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.Map;
24import java.util.Set;
25
26/**
27 * A MultiMap is implemented as a Map from keys to sets of
28 * values, and in fact the regular Map methods act on sets,
29 * not items.
30 */
31public class MultiMap<K, V> extends HashMap<K, Set<V>> implements IMultiMap<K,V> {
32
33    /**
34     *
35     */
36    private static final long serialVersionUID = 3275403475085923977L;
37    public MultiMap() {
38        super();
39    }
40
41
42
43    public MultiMap(Map<? extends K, ? extends Set<V>> m) {
44        super(m);
45    }
46
47    // TODO can we get the Java generics right on this?
48    public void addInverse(Map<V, K> m) {
49        for (Map.Entry<V, K> e : m.entrySet()) {
50            putItem((K)e.getValue(), (V)e.getKey());
51        }
52
53    }
54
55    public Set<V> putItem(K k, V v) {
56        Set<V> s = get(k);
57        if (s == null) {
58            s = new HashSet<V>();
59            s.add(v);
60            put(k, s);
61        } else {
62            s.add(v);
63        }
64        return s;
65    }
66   
67    /**
68     * Ensures that k is in the map, perhaps mapping to an empty set.
69     * @param k
70     * @return
71     */
72    public Set<V> putKey(K k) {
73        Set<V> s = get(k);
74        if (s == null) {
75            s = new HashSet<V>();
76            put(k,s);
77        }
78        return s;
79    }
80    public Set<V> putItems(K k, Collection<V> vs) {
81        Set<V> s = get(k);
82        if (s == null) {
83            s = new HashSet<V>(vs);
84            put(k, s);
85        } else {
86            s.addAll(vs);
87        }
88        return s;
89    }
90    public Set<V> removeItem(K k, V v) {
91        Set<V> s = get(k);
92        if (s != null) {
93            s.remove(v);
94            if (s.isEmpty()) {
95                remove(k);
96                s = null;
97            }
98        }
99        return s;
100    }
101    public Set<V> removeItemAllowEmpty(K k, V v) {
102        Set<V> s = get(k);
103        if (s != null) {
104            s.remove(v);
105        }
106        return s;
107    }
108
109}
Note: See TracBrowser for help on using the browser.