Debian Codegraph @

src/uk/me/parabola/util/MultiIdentityHashMap.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2013.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 package uk.me.parabola.util;
15
16 import java.util.Collections;
17 import java.util.IdentityHashMap;
18 import java.util.LinkedList;
19 import java.util.List;
20
21
22 public class MultiIdentityHashMap<K,V> extends IdentityHashMap<K,List<V>> {
23
24 /**
25 * the empty list to be returned when there is key without values.
26 */
27
28 /**
29 * Returns the list of values associated with the given key.
30 *
31 * @param key the key to get the values for.
32 * @return a list of values for the given keys or the empty list of no such
33 * value exist.
34 */
35 public List<V> get(Object key) {
36 List<V> result = super.get(key);
37 return result == null ? Collections.emptyList() : result;
38 }
39
40
41 public V add(K key, V value ) {
42 List<V> values = super.get(key);
43 if (values == null ) {
44 values = new LinkedList<>();
45 super.put( key, values );
46 }
47
48 boolean results = values.add(value);
49
50 return ( results ? value : null );
51 }
52
53 public V removeMapping(K key, V value) {
54 List<V> values = super.get(key);
55 if (values == null )
56 return null;
57
58 values.remove(value);
59
60 if (values.isEmpty())
61 super.remove(key);
62
63 return value;
64 }
65 }
66