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