1 package cz.cuni.amis.utils.maps;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 public class HashMapMap<PRIMARY_KEY, SECONDARY_KEY, ITEM>
21 extends HashMap<PRIMARY_KEY, Map<SECONDARY_KEY, ITEM>> {
22
23
24
25
26 private static final long serialVersionUID = -4541899270970246601L;
27 int secondaryCapacity;
28
29 public HashMapMap() {
30 secondaryCapacity = 16;
31 }
32
33 public HashMapMap(int primaryCapacity, int secondaryCapacity)
34 {
35 super(primaryCapacity);
36 this.secondaryCapacity = secondaryCapacity;
37 }
38
39
40
41
42
43
44 @Override
45 public Map<SECONDARY_KEY, ITEM> get(Object primaryKey) {
46 Map<SECONDARY_KEY, ITEM> map = super.get(primaryKey);
47 if (map != null) return map;
48 map = Collections.synchronizedMap(new HashMap<SECONDARY_KEY, ITEM>(secondaryCapacity));
49 super.put((PRIMARY_KEY)primaryKey, map);
50 return map;
51 }
52
53
54
55
56
57
58
59 public ITEM get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey) {
60 return get(primaryKey).get(secondaryKey);
61 }
62
63
64
65
66
67
68
69 public void put(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, ITEM item) {
70 get(primaryKey).put(secondaryKey, item);
71 }
72
73
74
75
76
77
78 @Override
79 public Map<SECONDARY_KEY, ITEM> remove(Object primaryKey) {
80 Map<SECONDARY_KEY, ITEM> map = super.remove(primaryKey);
81 if (map != null) return map;
82 return Collections.synchronizedMap(new HashMap<SECONDARY_KEY, ITEM>(secondaryCapacity));
83 }
84
85
86
87
88
89
90
91 public ITEM remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey) {
92 return get(primaryKey).remove(secondaryKey);
93 }
94
95 }