Mercurial > hg > openmind
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cl/maps/quad/QuadMap.java Wed Dec 14 15:49:40 2016 +0100 @@ -0,0 +1,191 @@ +package cl.maps.quad; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class QuadMap<V, A, B, C, D> implements IQuadMap<V, A, B, C, D>{ + + private Map<QuadKey<A, B, C, D>, V> map; + private Map<A, List<QuadKey<A, B, C, D>>> mapAKey; + private Map<B, List<QuadKey<A, B, C, D>>> mapBKey; + private Map<C, List<QuadKey<A, B, C, D>>> mapCKey; + private Map<D, QuadKey<A, B, C, D>> mapOwnKey; + + public QuadMap(){ + this.map = new HashMap<QuadKey<A, B, C, D>, V>(); + this.mapAKey = new HashMap<A, List<QuadKey<A, B, C, D>>>(); + this.mapBKey = new HashMap<B, List<QuadKey<A, B, C, D>>>(); + this.mapCKey = new HashMap<C, List<QuadKey<A, B, C, D>>>(); + this.mapOwnKey = new HashMap<D, QuadKey<A, B, C, D>>(); + } + + public QuadMap(QuadMap<? extends V, A, B, C, D> m) { + this.map = new HashMap<QuadKey<A, B, C, D>, V>(); + this.mapAKey = new HashMap<A, List<QuadKey<A, B, C, D>>>(); + this.mapBKey = new HashMap<B, List<QuadKey<A, B, C, D>>>(); + this.mapCKey = new HashMap<C, List<QuadKey<A, B, C, D>>>(); + this.mapOwnKey = new HashMap<D, QuadKey<A, B, C, D>>(); + this.putAllForCreate(m); + } + + private void putAllForCreate(QuadMap<? extends V, A, B, C, D> m) { + for(Map.Entry<QuadKey<A, B, C, D>, ? extends V> e : m.entrySet()){ + QuadKey<A, B, C, D> tKey = e.getKey(); + this.map.put(tKey, e.getValue()); + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + + if(!mapAKey.containsKey(tKey.getAKey())){ + mapAKey.put(tKey.getAKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ + mapAKey.get(tKey.getAKey()).add(tKey); + } + + if(!mapBKey.containsKey(tKey.getBKey())){ + mapBKey.put(tKey.getBKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapBKey.get(tKey.getBKey()).contains(tKey)){ + mapBKey.get(tKey.getBKey()).add(tKey); + } + + if(!mapCKey.containsKey(tKey.getCKey())){ + mapCKey.put(tKey.getCKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapCKey.get(tKey.getCKey()).contains(tKey)){ + mapCKey.get(tKey.getCKey()).add(tKey); + } + } + } + + public List<V> getValuesByAKey(A srcKey){ + List<V> list = new ArrayList<V>(); + if(mapAKey.containsKey(srcKey)){ + for(QuadKey<A, B, C, D> tKey : mapAKey.get(srcKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public List<V> getValuesByBKey(B tarKey){ + List<V> list = new ArrayList<V>(); + if(mapBKey.containsKey(tarKey)){ + for(QuadKey<A, B, C, D> tKey : mapBKey.get(tarKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public List<V> getValuesByCKey(C cKey){ + List<V> list = new ArrayList<V>(); + if(mapCKey.containsKey(cKey)){ + for(QuadKey<A, B, C, D> tKey : mapCKey.get(cKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public V getValuesByOwnKey(D ownKey){ + QuadKey<A, B, C, D> tKey = mapOwnKey.get(ownKey); + if(tKey != null){ + return this.map.get(tKey); + } + return null; + } + + public Set<QuadKey<A, B, C, D>> keySet(){ + return this.map.keySet(); + } + + public Set<Map.Entry<QuadKey<A, B, C, D>, V>> entrySet() { + return this.map.entrySet(); + } + + @Override + public int size() { + return this.map.size(); + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public boolean containsKey(QuadKey<A, B, C, D> key) { + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return this.map.containsValue(value); + } + + @Override + public V get(QuadKey<A, B, C, D> key) { + return map.get(key); + } + + @Override + public V put(QuadKey<A, B, C, D> tKey, V value) { + + if(!mapAKey.containsKey(tKey.getAKey())){ + mapAKey.put(tKey.getAKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapAKey.get(tKey.getAKey()).contains(tKey)){ + mapAKey.get(tKey.getAKey()).add(tKey); + } + + if(!mapBKey.containsKey(tKey.getBKey())){ + mapBKey.put(tKey.getBKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapBKey.get(tKey.getBKey()).contains(tKey)){ + mapBKey.get(tKey.getBKey()).add(tKey); + } + + if(!mapCKey.containsKey(tKey.getCKey())){ + mapCKey.put(tKey.getCKey(), new ArrayList<QuadKey<A, B, C, D>>()); + } + if(!mapCKey.get(tKey.getCKey()).contains(tKey)){ + mapCKey.get(tKey.getCKey()).add(tKey); + } + + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + return this.map.put(tKey, value); + } + + @Override + public V remove(QuadKey<A, B, C, D> key) { + if(mapAKey.containsKey(key.getAKey())){ + mapAKey.get(key.getAKey()).remove(key); + } + if(mapBKey.containsKey(key.getBKey())){ + mapBKey.get(key.getBKey()).remove(key); + } + if(mapCKey.containsKey(key.getCKey())){ + mapCKey.get(key.getCKey()).remove(key); + } + this.mapOwnKey.remove(key.getOwnKey()); + return this.map.remove(key); + } + + @Override + public void clear() { + this.map.clear(); + this.mapAKey.clear(); + this.mapBKey.clear(); + this.mapCKey.clear(); + this.mapOwnKey.clear(); + } + + @Override + public Collection<V> values() { + return this.map.values(); + } +}