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