Mercurial > hg > openmind
comparison src/main/java/cl/maps/quad/QuadMap.java @ 59:6e08ff123ae6
check in complete source of cl.maps.
| author | casties |
|---|---|
| date | Wed, 14 Dec 2016 15:49:40 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 58:153a0232270b | 59:6e08ff123ae6 |
|---|---|
| 1 package cl.maps.quad; | |
| 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 QuadMap<V, A, B, C, D> implements IQuadMap<V, A, B, C, D>{ | |
| 11 | |
| 12 private Map<QuadKey<A, B, C, D>, V> map; | |
| 13 private Map<A, List<QuadKey<A, B, C, D>>> mapAKey; | |
| 14 private Map<B, List<QuadKey<A, B, C, D>>> mapBKey; | |
| 15 private Map<C, List<QuadKey<A, B, C, D>>> mapCKey; | |
| 16 private Map<D, QuadKey<A, B, C, D>> mapOwnKey; | |
| 17 | |
| 18 public QuadMap(){ | |
| 19 this.map = new HashMap<QuadKey<A, B, C, D>, V>(); | |
| 20 this.mapAKey = new HashMap<A, List<QuadKey<A, B, C, D>>>(); | |
| 21 this.mapBKey = new HashMap<B, List<QuadKey<A, B, C, D>>>(); | |
| 22 this.mapCKey = new HashMap<C, List<QuadKey<A, B, C, D>>>(); | |
| 23 this.mapOwnKey = new HashMap<D, QuadKey<A, B, C, D>>(); | |
| 24 } | |
| 25 | |
| 26 public QuadMap(QuadMap<? extends V, A, B, C, D> m) { | |
| 27 this.map = new HashMap<QuadKey<A, B, C, D>, V>(); | |
| 28 this.mapAKey = new HashMap<A, List<QuadKey<A, B, C, D>>>(); | |
| 29 this.mapBKey = new HashMap<B, List<QuadKey<A, B, C, D>>>(); | |
| 30 this.mapCKey = new HashMap<C, List<QuadKey<A, B, C, D>>>(); | |
| 31 this.mapOwnKey = new HashMap<D, QuadKey<A, B, C, D>>(); | |
| 32 this.putAllForCreate(m); | |
| 33 } | |
| 34 | |
| 35 private void putAllForCreate(QuadMap<? extends V, A, B, C, D> m) { | |
| 36 for(Map.Entry<QuadKey<A, B, C, D>, ? extends V> e : m.entrySet()){ | |
| 37 QuadKey<A, B, C, D> tKey = e.getKey(); | |
| 38 this.map.put(tKey, e.getValue()); | |
| 39 this.mapOwnKey.put(tKey.getOwnKey(), tKey); | |
| 40 | |
| 41 if(!mapAKey.containsKey(tKey.getAKey())){ | |
| 42 mapAKey.put(tKey.getAKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 43 } | |
| 44 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ | |
| 45 mapAKey.get(tKey.getAKey()).add(tKey); | |
| 46 } | |
| 47 | |
| 48 if(!mapBKey.containsKey(tKey.getBKey())){ | |
| 49 mapBKey.put(tKey.getBKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 50 } | |
| 51 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){ | |
| 52 mapBKey.get(tKey.getBKey()).add(tKey); | |
| 53 } | |
| 54 | |
| 55 if(!mapCKey.containsKey(tKey.getCKey())){ | |
| 56 mapCKey.put(tKey.getCKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 57 } | |
| 58 if(!mapCKey.get(tKey.getCKey()).contains(tKey)){ | |
| 59 mapCKey.get(tKey.getCKey()).add(tKey); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 public List<V> getValuesByAKey(A srcKey){ | |
| 65 List<V> list = new ArrayList<V>(); | |
| 66 if(mapAKey.containsKey(srcKey)){ | |
| 67 for(QuadKey<A, B, C, D> tKey : mapAKey.get(srcKey)){ | |
| 68 list.add(map.get(tKey)); | |
| 69 } | |
| 70 } | |
| 71 return list; | |
| 72 } | |
| 73 | |
| 74 public List<V> getValuesByBKey(B tarKey){ | |
| 75 List<V> list = new ArrayList<V>(); | |
| 76 if(mapBKey.containsKey(tarKey)){ | |
| 77 for(QuadKey<A, B, C, D> tKey : mapBKey.get(tarKey)){ | |
| 78 list.add(map.get(tKey)); | |
| 79 } | |
| 80 } | |
| 81 return list; | |
| 82 } | |
| 83 | |
| 84 public List<V> getValuesByCKey(C cKey){ | |
| 85 List<V> list = new ArrayList<V>(); | |
| 86 if(mapCKey.containsKey(cKey)){ | |
| 87 for(QuadKey<A, B, C, D> tKey : mapCKey.get(cKey)){ | |
| 88 list.add(map.get(tKey)); | |
| 89 } | |
| 90 } | |
| 91 return list; | |
| 92 } | |
| 93 | |
| 94 public V getValuesByOwnKey(D ownKey){ | |
| 95 QuadKey<A, B, C, D> tKey = mapOwnKey.get(ownKey); | |
| 96 if(tKey != null){ | |
| 97 return this.map.get(tKey); | |
| 98 } | |
| 99 return null; | |
| 100 } | |
| 101 | |
| 102 public Set<QuadKey<A, B, C, D>> keySet(){ | |
| 103 return this.map.keySet(); | |
| 104 } | |
| 105 | |
| 106 public Set<Map.Entry<QuadKey<A, B, C, D>, V>> entrySet() { | |
| 107 return this.map.entrySet(); | |
| 108 } | |
| 109 | |
| 110 @Override | |
| 111 public int size() { | |
| 112 return this.map.size(); | |
| 113 } | |
| 114 | |
| 115 @Override | |
| 116 public boolean isEmpty() { | |
| 117 return this.map.isEmpty(); | |
| 118 } | |
| 119 | |
| 120 @Override | |
| 121 public boolean containsKey(QuadKey<A, B, C, D> key) { | |
| 122 return this.map.containsKey(key); | |
| 123 } | |
| 124 | |
| 125 @Override | |
| 126 public boolean containsValue(Object value) { | |
| 127 return this.map.containsValue(value); | |
| 128 } | |
| 129 | |
| 130 @Override | |
| 131 public V get(QuadKey<A, B, C, D> key) { | |
| 132 return map.get(key); | |
| 133 } | |
| 134 | |
| 135 @Override | |
| 136 public V put(QuadKey<A, B, C, D> tKey, V value) { | |
| 137 | |
| 138 if(!mapAKey.containsKey(tKey.getAKey())){ | |
| 139 mapAKey.put(tKey.getAKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 140 } | |
| 141 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ | |
| 142 mapAKey.get(tKey.getAKey()).add(tKey); | |
| 143 } | |
| 144 | |
| 145 if(!mapBKey.containsKey(tKey.getBKey())){ | |
| 146 mapBKey.put(tKey.getBKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 147 } | |
| 148 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){ | |
| 149 mapBKey.get(tKey.getBKey()).add(tKey); | |
| 150 } | |
| 151 | |
| 152 if(!mapCKey.containsKey(tKey.getCKey())){ | |
| 153 mapCKey.put(tKey.getCKey(), new ArrayList<QuadKey<A, B, C, D>>()); | |
| 154 } | |
| 155 if(!mapCKey.get(tKey.getCKey()).contains(tKey)){ | |
| 156 mapCKey.get(tKey.getCKey()).add(tKey); | |
| 157 } | |
| 158 | |
| 159 this.mapOwnKey.put(tKey.getOwnKey(), tKey); | |
| 160 return this.map.put(tKey, value); | |
| 161 } | |
| 162 | |
| 163 @Override | |
| 164 public V remove(QuadKey<A, B, C, D> key) { | |
| 165 if(mapAKey.containsKey(key.getAKey())){ | |
| 166 mapAKey.get(key.getAKey()).remove(key); | |
| 167 } | |
| 168 if(mapBKey.containsKey(key.getBKey())){ | |
| 169 mapBKey.get(key.getBKey()).remove(key); | |
| 170 } | |
| 171 if(mapCKey.containsKey(key.getCKey())){ | |
| 172 mapCKey.get(key.getCKey()).remove(key); | |
| 173 } | |
| 174 this.mapOwnKey.remove(key.getOwnKey()); | |
| 175 return this.map.remove(key); | |
| 176 } | |
| 177 | |
| 178 @Override | |
| 179 public void clear() { | |
| 180 this.map.clear(); | |
| 181 this.mapAKey.clear(); | |
| 182 this.mapBKey.clear(); | |
| 183 this.mapCKey.clear(); | |
| 184 this.mapOwnKey.clear(); | |
| 185 } | |
| 186 | |
| 187 @Override | |
| 188 public Collection<V> values() { | |
| 189 return this.map.values(); | |
| 190 } | |
| 191 } |
