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
18
19
20 public class WeakHashTriMap<PRIMARY_KEY,SECONDARY_KEY,TERTIARY_KEY,ITEM>
21 extends WeakHashMap<PRIMARY_KEY,Map<SECONDARY_KEY,Map<TERTIARY_KEY,ITEM>>>
22 {
23
24
25
26
27 private static final long serialVersionUID = 1L;
28
29 private int secondaryCapacity;
30 private int tertiaryCapacity;
31
32
33
34
35 public WeakHashTriMap()
36 {
37 secondaryCapacity = 16;
38 tertiaryCapacity = 16;
39 }
40
41
42
43
44
45
46
47 public WeakHashTriMap( int primaryCapacity, int secondaryCapacity, int tertiaryCapacity)
48 {
49 super(primaryCapacity);
50 this.secondaryCapacity = secondaryCapacity;
51 this.tertiaryCapacity = tertiaryCapacity;
52 }
53
54
55
56
57
58
59
60 @SuppressWarnings("unchecked")
61 @Override
62 public Map<SECONDARY_KEY, Map<TERTIARY_KEY,ITEM>> get(Object primaryKey)
63 {
64 Map<SECONDARY_KEY, Map<TERTIARY_KEY,ITEM>> result = super.get(primaryKey);
65 if (result != null) { return result; };
66 result = Collections.synchronizedMap( new HashMapMap<SECONDARY_KEY,TERTIARY_KEY,ITEM>(secondaryCapacity, tertiaryCapacity) );
67 super.put( (PRIMARY_KEY)primaryKey,result);
68 return result;
69 }
70
71
72
73
74
75
76
77
78 public Map<TERTIARY_KEY,ITEM> get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey)
79 {
80 Map<TERTIARY_KEY, ITEM> result = get(primaryKey).get(secondaryKey);
81 if (result != null) { return result; };
82 result = Collections.synchronizedMap( new HashMap<TERTIARY_KEY,ITEM>( tertiaryCapacity ));
83 get(primaryKey).put(secondaryKey, result);
84 return result;
85 }
86
87
88
89
90
91
92
93
94 public ITEM get(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey)
95 {
96 return get(primaryKey,secondaryKey).get(tertiaryKey);
97 }
98
99
100
101
102
103
104
105
106 public void put(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey, ITEM item)
107 {
108 get(primaryKey,secondaryKey).put(tertiaryKey, item);
109 }
110
111
112
113
114 @Override
115 public Map<SECONDARY_KEY, Map<TERTIARY_KEY,ITEM>> remove(Object primaryKey)
116 {
117 Map<SECONDARY_KEY, Map<TERTIARY_KEY,ITEM>> result = super.remove(primaryKey);
118 if (result != null) { return result; };
119 return Collections.synchronizedMap( new HashMapMap<SECONDARY_KEY, TERTIARY_KEY,ITEM>(secondaryCapacity, tertiaryCapacity) );
120 }
121
122
123
124
125
126
127
128
129 public Map<TERTIARY_KEY,ITEM> remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey)
130 {
131 Map<TERTIARY_KEY,ITEM> result = get(primaryKey).remove(secondaryKey);
132 if ( result != null) { return result; };
133 return Collections.synchronizedMap( new HashMap<TERTIARY_KEY, ITEM> (tertiaryCapacity));
134 }
135
136
137
138
139
140
141
142
143
144 public ITEM remove(PRIMARY_KEY primaryKey, SECONDARY_KEY secondaryKey, TERTIARY_KEY tertiaryKey)
145 {
146 return get(primaryKey,secondaryKey).remove(tertiaryKey);
147 }
148
149 }