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