Mercurial > hg > IndexMetaContextualizer
diff src/main/java/de/mpiwg/indexmeta/utils/QuadMap.java @ 0:dfce13a5f5f9
nit project!
author | Jorge Urzua <jurzua@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 11 Apr 2013 15:25:26 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/indexmeta/utils/QuadMap.java Thu Apr 11 15:25:26 2013 +0200 @@ -0,0 +1,191 @@ +package de.mpiwg.indexmeta.utils; + +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> implements IQuadMap<V>{ + + private Map<QuadKey, V> map; + private Map<Object, List<QuadKey>> mapFirstKey; + private Map<Object, List<QuadKey>> mapSecondKey; + private Map<Object, List<QuadKey>> mapThirdKey; + private Map<Object, QuadKey> mapOwnKey; + + public QuadMap(){ + this.map = new HashMap<QuadKey, V>(); + this.mapFirstKey = new HashMap<Object, List<QuadKey>>(); + this.mapSecondKey = new HashMap<Object, List<QuadKey>>(); + this.mapThirdKey = new HashMap<Object, List<QuadKey>>(); + this.mapOwnKey = new HashMap<Object, QuadKey>(); + } + + public QuadMap(QuadMap<? extends V> m) { + this.map = new HashMap<QuadKey, V>(); + this.mapFirstKey = new HashMap<Object, List<QuadKey>>(); + this.mapSecondKey = new HashMap<Object, List<QuadKey>>(); + this.mapThirdKey = new HashMap<Object, List<QuadKey>>(); + this.mapOwnKey = new HashMap<Object, QuadKey>(); + this.putAllForCreate(m); + } + + private void putAllForCreate(QuadMap<? extends V> m) { + for(Map.Entry<QuadKey, ? extends V> e : m.entrySet()){ + QuadKey tKey = e.getKey(); + this.map.put(tKey, e.getValue()); + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + + if(!mapFirstKey.containsKey(tKey.getFirstKey())){ + mapFirstKey.put(tKey.getFirstKey(), new ArrayList<QuadKey>()); + } + if(!mapFirstKey.get(tKey.getFirstKey()).contains(tKey)){ + mapFirstKey.get(tKey.getFirstKey()).add(tKey); + } + + if(!mapSecondKey.containsKey(tKey.getSecondKey())){ + mapSecondKey.put(tKey.getSecondKey(), new ArrayList<QuadKey>()); + } + if(!mapSecondKey.get(tKey.getSecondKey()).contains(tKey)){ + mapSecondKey.get(tKey.getSecondKey()).add(tKey); + } + + if(!mapThirdKey.containsKey(tKey.getThirdKey())){ + mapThirdKey.put(tKey.getThirdKey(), new ArrayList<QuadKey>()); + } + if(!mapThirdKey.get(tKey.getThirdKey()).contains(tKey)){ + mapThirdKey.get(tKey.getThirdKey()).add(tKey); + } + } + } + + public List<V> getValuesByFirstKey(Object srcKey){ + List<V> list = new ArrayList<V>(); + if(mapFirstKey.containsKey(srcKey)){ + for(QuadKey tKey : mapFirstKey.get(srcKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public List<V> getValuesBySecondKey(Object tarKey){ + List<V> list = new ArrayList<V>(); + if(mapSecondKey.containsKey(tarKey)){ + for(QuadKey tKey : mapSecondKey.get(tarKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public List<V> getValuesByThirdKey(Object thirdKey){ + List<V> list = new ArrayList<V>(); + if(mapThirdKey.containsKey(thirdKey)){ + for(QuadKey tKey : mapThirdKey.get(thirdKey)){ + list.add(map.get(tKey)); + } + } + return list; + } + + public V getValuesByOwnKey(Object ownKey){ + QuadKey tKey = mapOwnKey.get(ownKey); + if(tKey != null){ + return this.map.get(tKey); + } + return null; + } + + public Set<QuadKey> keySet(){ + return this.map.keySet(); + } + + public Set<Map.Entry<QuadKey, 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 key) { + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return this.map.containsValue(value); + } + + @Override + public V get(QuadKey key) { + return map.get(key); + } + + @Override + public V put(QuadKey tKey, V value) { + + if(!mapFirstKey.containsKey(tKey.getFirstKey())){ + mapFirstKey.put(tKey.getFirstKey(), new ArrayList<QuadKey>()); + } + if(!mapFirstKey.get(tKey.getFirstKey()).contains(tKey)){ + mapFirstKey.get(tKey.getFirstKey()).add(tKey); + } + + if(!mapSecondKey.containsKey(tKey.getSecondKey())){ + mapSecondKey.put(tKey.getSecondKey(), new ArrayList<QuadKey>()); + } + if(!mapSecondKey.get(tKey.getSecondKey()).contains(tKey)){ + mapSecondKey.get(tKey.getSecondKey()).add(tKey); + } + + if(!mapThirdKey.containsKey(tKey.getThirdKey())){ + mapThirdKey.put(tKey.getThirdKey(), new ArrayList<QuadKey>()); + } + if(!mapThirdKey.get(tKey.getThirdKey()).contains(tKey)){ + mapThirdKey.get(tKey.getThirdKey()).add(tKey); + } + + this.mapOwnKey.put(tKey.getOwnKey(), tKey); + return this.map.put(tKey, value); + } + + @Override + public V remove(QuadKey key) { + if(mapFirstKey.containsKey(key.getFirstKey())){ + mapFirstKey.get(key.getFirstKey()).remove(key); + } + if(mapSecondKey.containsKey(key.getSecondKey())){ + mapSecondKey.get(key.getSecondKey()).remove(key); + } + if(mapThirdKey.containsKey(key.getThirdKey())){ + mapThirdKey.get(key.getThirdKey()).remove(key); + } + this.mapOwnKey.remove(key.getOwnKey()); + return this.map.remove(key); + } + + @Override + public void clear() { + this.map.clear(); + this.mapFirstKey.clear(); + this.mapSecondKey.clear(); + this.mapThirdKey.clear(); + this.mapOwnKey.clear(); + } + + @Override + public Collection<V> values() { + return this.map.values(); + } +}