1 package cz.cuni.amis.utils.maps;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Set;
7
8
9
10
11
12
13
14
15
16
17
18
19
20 public class HashMapSet<KEY, ITEM> extends HashMap<KEY, Set<ITEM>> {
21
22
23
24
25
26
27 @Override
28 public Set<ITEM> get(Object primaryKey) {
29 Set<ITEM> set = super.get(primaryKey);
30 if (set != null) return set;
31 set = Collections.synchronizedSet(new HashSet<ITEM>());
32 super.put((KEY)primaryKey, set);
33 return set;
34 }
35
36
37
38
39
40
41 public void add(KEY key, ITEM item) {
42 get(key).add(item);
43 }
44
45
46
47
48
49
50 @Override
51 public Set<ITEM> remove(Object key) {
52 Set<ITEM> set = super.remove(key);
53 if (set != null) return set;
54 return Collections.synchronizedSet(new HashSet<ITEM>());
55 }
56
57
58
59
60
61
62 public boolean remove(KEY key, ITEM item) {
63 return get(key).remove(item);
64 }
65
66
67
68
69
70
71
72 public boolean contains(KEY key, ITEM item) {
73 return get(key).contains(item);
74 }
75
76 }