59
|
1 package cl.maps.penta;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.Collection;
|
|
5 import java.util.HashMap;
|
|
6 import java.util.List;
|
|
7 import java.util.Map;
|
|
8 import java.util.Set;
|
|
9
|
|
10 public class PentaMap<V, A, B, C, D, E> implements IPentaMap<V, A, B, C, D, E>{
|
|
11
|
|
12 private Map<PentaKey<A, B, C, D, E>, V> map;
|
|
13 private Map<A, List<PentaKey<A, B, C, D, E>>> mapAKey;
|
|
14 private Map<B, List<PentaKey<A, B, C, D, E>>> mapBKey;
|
|
15 private Map<C, List<PentaKey<A, B, C, D, E>>> mapCKey;
|
|
16 private Map<D, List<PentaKey<A, B, C, D, E>>> mapDKey;
|
|
17 private Map<E, PentaKey<A, B, C, D, E>> mapOwnKey;
|
|
18
|
|
19 public PentaMap(){
|
|
20 this.map = new HashMap<PentaKey<A, B, C, D, E>, V>();
|
|
21 this.mapAKey = new HashMap<A, List<PentaKey<A, B, C, D, E>>>();
|
|
22 this.mapBKey = new HashMap<B, List<PentaKey<A, B, C, D, E>>>();
|
|
23 this.mapCKey = new HashMap<C, List<PentaKey<A, B, C, D, E>>>();
|
|
24 this.mapDKey = new HashMap<D, List<PentaKey<A, B, C, D, E>>>();
|
|
25 this.mapOwnKey = new HashMap<E, PentaKey<A, B, C, D, E>>();
|
|
26 }
|
|
27
|
|
28 public PentaMap(PentaMap<V, A, B, C, D, E> m) {
|
|
29 this.map = new HashMap<PentaKey<A, B, C, D, E>, V>();
|
|
30 this.mapAKey = new HashMap<A, List<PentaKey<A, B, C, D, E>>>();
|
|
31 this.mapBKey = new HashMap<B, List<PentaKey<A, B, C, D, E>>>();
|
|
32 this.mapCKey = new HashMap<C, List<PentaKey<A, B, C, D, E>>>();
|
|
33 this.mapDKey = new HashMap<D, List<PentaKey<A, B, C, D, E>>>();
|
|
34 this.mapOwnKey = new HashMap<E, PentaKey<A, B, C, D, E>>();
|
|
35 this.putAllForCreate(m);
|
|
36 }
|
|
37
|
|
38 private void putAllForCreate(PentaMap<? extends V, A, B, C, D, E> m) {
|
|
39 for(Map.Entry<PentaKey<A, B, C, D, E>, ? extends V> e : m.entrySet()){
|
|
40
|
|
41 PentaKey<A, B, C, D, E> tKey = e.getKey();
|
|
42
|
|
43 this.map.put(tKey, e.getValue());
|
|
44 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
45
|
|
46 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
47 mapAKey.put(tKey.getAKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
48 }
|
|
49 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
50 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
51 }
|
|
52
|
|
53 if(!mapBKey.containsKey(tKey.getBKey())){
|
|
54 mapBKey.put(tKey.getBKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
55 }
|
|
56 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
|
|
57 mapBKey.get(tKey.getBKey()).add(tKey);
|
|
58 }
|
|
59
|
|
60 if(!mapCKey.containsKey(tKey.getCKey())){
|
|
61 mapCKey.put(tKey.getCKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
62 }
|
|
63 if(!mapCKey.get(tKey.getCKey()).contains(tKey)){
|
|
64 mapCKey.get(tKey.getCKey()).add(tKey);
|
|
65 }
|
|
66 if(!mapDKey.containsKey(tKey.getDKey())){
|
|
67 mapDKey.put(tKey.getDKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
68 }
|
|
69 if(!mapDKey.get(tKey.getDKey()).contains(tKey)){
|
|
70 mapDKey.get(tKey.getDKey()).add(tKey);
|
|
71 }
|
|
72 }
|
|
73 }
|
|
74
|
|
75 public List<V> getValuesByAKey(A srcKey){
|
|
76 List<V> list = new ArrayList<V>();
|
|
77 if(mapAKey.containsKey(srcKey)){
|
|
78 for(PentaKey<A, B, C, D, E> tKey : mapAKey.get(srcKey)){
|
|
79 list.add(map.get(tKey));
|
|
80 }
|
|
81 }
|
|
82 return list;
|
|
83 }
|
|
84
|
|
85 public List<V> getValuesByBKey(B tarKey){
|
|
86 List<V> list = new ArrayList<V>();
|
|
87 if(mapBKey.containsKey(tarKey)){
|
|
88 for(PentaKey<A, B, C, D, E> tKey : mapBKey.get(tarKey)){
|
|
89 list.add(map.get(tKey));
|
|
90 }
|
|
91 }
|
|
92 return list;
|
|
93 }
|
|
94
|
|
95 public List<V> getValuesByCKey(C cKey){
|
|
96 List<V> list = new ArrayList<V>();
|
|
97 if(mapCKey.containsKey(cKey)){
|
|
98 for(PentaKey<A, B, C, D, E> tKey : mapCKey.get(cKey)){
|
|
99 list.add(map.get(tKey));
|
|
100 }
|
|
101 }
|
|
102 return list;
|
|
103 }
|
|
104
|
|
105 public List<V> getValuesByDKey(D dKey){
|
|
106 List<V> list = new ArrayList<V>();
|
|
107 if(mapDKey.containsKey(dKey)){
|
|
108 for(PentaKey<A, B, C, D, E> tKey : mapDKey.get(dKey)){
|
|
109 list.add(map.get(tKey));
|
|
110 }
|
|
111 }
|
|
112 return list;
|
|
113 }
|
|
114
|
|
115 public V getValuesByOwnKey(E ownKey){
|
|
116 PentaKey<A, B, C, D, E> tKey = mapOwnKey.get(ownKey);
|
|
117 if(tKey != null){
|
|
118 return this.map.get(tKey);
|
|
119 }
|
|
120 return null;
|
|
121 }
|
|
122
|
|
123 public Set<PentaKey<A, B, C, D, E>> keySet(){
|
|
124 return this.map.keySet();
|
|
125 }
|
|
126
|
|
127 public Set<Map.Entry<PentaKey<A, B, C, D, E>, V>> entrySet() {
|
|
128 return this.map.entrySet();
|
|
129 }
|
|
130
|
|
131 @Override
|
|
132 public int size() {
|
|
133 return this.map.size();
|
|
134 }
|
|
135
|
|
136 @Override
|
|
137 public boolean isEmpty() {
|
|
138 return this.map.isEmpty();
|
|
139 }
|
|
140
|
|
141 @Override
|
|
142 public boolean containsKey(PentaKey<A, B, C, D, E> key) {
|
|
143 return this.map.containsKey(key);
|
|
144 }
|
|
145
|
|
146 @Override
|
|
147 public boolean containsValue(Object value) {
|
|
148 return this.map.containsValue(value);
|
|
149 }
|
|
150
|
|
151 @Override
|
|
152 public V get(PentaKey<A, B, C, D, E> key) {
|
|
153 return map.get(key);
|
|
154 }
|
|
155
|
|
156 @Override
|
|
157 public V put(PentaKey<A, B, C, D, E> tKey, V value) {
|
|
158
|
|
159 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
160 mapAKey.put(tKey.getAKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
161 }
|
|
162 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
163 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
164 }
|
|
165
|
|
166 if(!mapBKey.containsKey(tKey.getBKey())){
|
|
167 mapBKey.put(tKey.getBKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
168 }
|
|
169 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
|
|
170 mapBKey.get(tKey.getBKey()).add(tKey);
|
|
171 }
|
|
172
|
|
173 if(!mapCKey.containsKey(tKey.getCKey())){
|
|
174 mapCKey.put(tKey.getCKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
175 }
|
|
176 if(!mapCKey.get(tKey.getCKey()).contains(tKey)){
|
|
177 mapCKey.get(tKey.getCKey()).add(tKey);
|
|
178 }
|
|
179
|
|
180 if(!mapDKey.containsKey(tKey.getDKey())){
|
|
181 mapDKey.put(tKey.getDKey(), new ArrayList<PentaKey<A, B, C, D, E>>());
|
|
182 }
|
|
183 if(!mapDKey.get(tKey.getDKey()).contains(tKey)){
|
|
184 mapDKey.get(tKey.getDKey()).add(tKey);
|
|
185 }
|
|
186
|
|
187 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
188 return this.map.put(tKey, value);
|
|
189 }
|
|
190
|
|
191 @Override
|
|
192 public V remove(PentaKey<A, B, C, D, E> key) {
|
|
193 if(mapAKey.containsKey(key.getAKey())){
|
|
194 mapAKey.get(key.getAKey()).remove(key);
|
|
195 }
|
|
196 if(mapBKey.containsKey(key.getBKey())){
|
|
197 mapBKey.get(key.getBKey()).remove(key);
|
|
198 }
|
|
199 if(mapCKey.containsKey(key.getCKey())){
|
|
200 mapCKey.get(key.getCKey()).remove(key);
|
|
201 }
|
|
202 if(mapDKey.containsKey(key.getDKey())){
|
|
203 mapDKey.get(key.getDKey()).remove(key);
|
|
204 }
|
|
205
|
|
206 this.mapOwnKey.remove(key.getOwnKey());
|
|
207 return this.map.remove(key);
|
|
208 }
|
|
209
|
|
210 @Override
|
|
211 public void clear() {
|
|
212 this.map.clear();
|
|
213 this.mapAKey.clear();
|
|
214 this.mapBKey.clear();
|
|
215 this.mapCKey.clear();
|
|
216 this.mapDKey.clear();
|
|
217 this.mapOwnKey.clear();
|
|
218 }
|
|
219
|
|
220 @Override
|
|
221 public Collection<V> values() {
|
|
222 return this.map.values();
|
|
223 }
|
|
224 }
|