diff src/main/java/cl/maps/triple/TripleMap.java @ 59:6e08ff123ae6

check in complete source of cl.maps.
author casties
date Wed, 14 Dec 2016 15:49:40 +0100
parents
children 58659e865279
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/cl/maps/triple/TripleMap.java	Wed Dec 14 15:49:40 2016 +0100
@@ -0,0 +1,169 @@
+package cl.maps.triple;
+
+
+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 TripleMap<V, A, B, C> implements ITripleMap<V, A, B, C>{
+	
+	private Map<TripleKey<A, B, C>, V> map;
+	private Map<A, List<TripleKey<A, B, C>>> mapAKey;
+	private Map<B, List<TripleKey<A, B, C>>> mapBKey;
+	private Map<C, TripleKey<A, B, C>> mapOwnKey;
+	
+	public TripleMap(){
+		this.map  = new HashMap<TripleKey<A, B, C>, V>();
+		this.mapAKey = new HashMap<A, List<TripleKey<A, B, C>>>();
+		this.mapBKey = new HashMap<B, List<TripleKey<A, B, C>>>();
+		this.mapOwnKey = new HashMap<C, TripleKey<A, B, C>>();
+	}
+	
+	public TripleMap(TripleMap<? extends V, A, B, C> m) {
+		this.map  = new HashMap<TripleKey<A, B, C>, V>();
+		this.mapAKey = new HashMap<A, List<TripleKey<A, B, C>>>();
+		this.mapBKey = new HashMap<B, List<TripleKey<A, B, C>>>();
+		this.mapOwnKey = new HashMap<C, TripleKey<A, B, C>>();
+		this.putAllForCreate(m);
+	}
+
+	private void putAllForCreate(TripleMap<? extends V, A, B, C> m) {
+		for(Map.Entry<TripleKey<A, B, C>, ? extends V> e : m.entrySet()){
+			TripleKey<A, B, C> 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<TripleKey<A, B, C>>());
+			 }
+			 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
+				 mapAKey.get(tKey.getAKey()).add(tKey);
+			 }
+			 
+			 if(!mapBKey.containsKey(tKey.getBKey())){
+				 mapBKey.put(tKey.getBKey(), new ArrayList<TripleKey<A, B, C>>());
+			 }
+			 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
+				 mapBKey.get(tKey.getBKey()).add(tKey);
+			 }
+		}
+	}
+	
+	public List<V> getValuesByAKey(A srcKey){
+		List<V> list = new ArrayList<V>();
+		if(mapAKey.containsKey(srcKey)){
+			for(TripleKey<A, B, C> 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(TripleKey<A, B, C> tKey : mapBKey.get(tarKey)){
+				list.add(map.get(tKey));
+			}
+		}
+		return list;
+	}
+	
+	public V getValuesByOwnKey(C ownKey){
+		TripleKey<A, B, C> tKey = mapOwnKey.get(ownKey);
+		if(tKey != null){
+			return this.map.get(tKey);
+		}
+		return null;
+	}
+	
+	public Set<TripleKey<A, B, C>> keySet(){
+		return this.map.keySet();
+	}
+	
+	public Set<Map.Entry<TripleKey<A, B, C>, 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(TripleKey<A, B, C> key) {
+		return this.map.containsKey(key);
+	}
+
+	@Override
+	public boolean containsValue(Object value) {
+		return this.map.containsValue(value);
+	}
+
+	@Override
+	public V get(TripleKey<A, B, C> key) {
+		return map.get(key);
+	}
+
+	@Override
+	public V put(TripleKey<A, B, C> tKey, V value) {
+		if(!mapAKey.containsKey(tKey.getAKey())){
+			 mapAKey.put(tKey.getAKey(), new ArrayList<TripleKey<A, B, C>>());
+		}
+		if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
+			mapAKey.get(tKey.getAKey()).add(tKey);
+		}
+
+		if(!mapBKey.containsKey(tKey.getBKey())){
+			mapBKey.put(tKey.getBKey(), new ArrayList<TripleKey<A, B, C>>());
+		}
+		if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
+			mapBKey.get(tKey.getBKey()).add(tKey);
+		}
+		
+		this.mapOwnKey.put(tKey.getOwnKey(), tKey);
+		return this.map.put(tKey, value);
+	}
+
+	@Override
+	public V remove(TripleKey<A, B, C> key) {
+		if(mapAKey.containsKey(key.getAKey())){
+			mapAKey.get(key.getAKey()).remove(key);
+		}
+		if(mapBKey.containsKey(key.getBKey())){
+			mapBKey.get(key.getBKey()).remove(key);
+		}
+		this.mapOwnKey.remove(key.getOwnKey());
+		
+		int hashCodeInput = key.hashCode();
+		System.out.println(hashCodeInput);
+		if(map.size() == 1){
+			Object uu = new ArrayList<TripleKey<A, B, C>>(map.keySet()).get(0);
+			int hashCode2 = uu.hashCode();
+			System.out.println(hashCode2);
+		}
+		
+		return this.map.remove(key);
+	}
+
+	@Override
+	public void clear() {
+		this.map.clear();
+		this.mapAKey.clear();
+		this.mapBKey.clear();
+		this.mapOwnKey.clear();
+	}
+
+	@Override
+	public Collection<V> values() {
+		return this.map.values();
+	}
+}