diff src/main/java/cl/maps/duplex/DuplexMap.java @ 1:615d27dce9b3

(none)
author jurzua
date Wed, 29 Oct 2014 13:28:45 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/cl/maps/duplex/DuplexMap.java	Wed Oct 29 13:28:45 2014 +0000
@@ -0,0 +1,169 @@
+package cl.maps.duplex;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.mpi.openmind.repository.bo.Entity;
+
+import cl.maps.utils.DuplexMapKeepSortByNormalizedOV;
+import cl.maps.utils.DuplexMapSortByNormalizedOV;
+
+public class DuplexMap<V, A, B> implements IDuplexMap<V, A, B>{
+
+	private Map<DuplexKey<A, B>, V> map;
+	private Map<A, List<DuplexKey<A, B>>> mapAKey;
+	private Map<B, DuplexKey<A, B>> mapOwnKey;
+	
+	public DuplexMap(){
+		this.map  = new HashMap<DuplexKey<A, B>, V>();
+		this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>();
+		this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>();
+	}
+	
+	public DuplexMap(DuplexMap<? extends V, A, B> m) {
+		this.map  = new HashMap<DuplexKey<A, B>, V>();
+		this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>();
+		this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>();
+		this.putAllForCreate(m);
+	}
+
+	private void putAllForCreate(DuplexMap<? extends V, A, B> m) {
+		for(Map.Entry<DuplexKey<A, B>, ? extends V> e : m.entrySet()){
+			DuplexKey<A, B> 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 LinkedList<DuplexKey<A, B>>());
+			 }
+			 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
+				 mapAKey.get(tKey.getAKey()).add(tKey);	 
+			 }
+		}
+	}
+	
+	public List<V>getValuesByAKey(A srcKey){
+		List<V> list = new ArrayList<V>();
+		if(mapAKey.containsKey(srcKey)){
+			for(DuplexKey<A, B> tKey : mapAKey.get(srcKey)){
+				list.add(map.get(tKey));
+			}
+		}
+		return list;
+	}
+	
+	public V getValuesByOwnKey(B ownKey){
+		DuplexKey<A, B> tKey = mapOwnKey.get(ownKey);
+		if(tKey != null){
+			return this.map.get(tKey);
+		}
+		return null;
+	}
+	
+	public Set<DuplexKey<A, B>> keySet(){
+		return this.map.keySet();
+	}
+	
+	public Set<Map.Entry<DuplexKey<A, B>, 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(DuplexKey<A, B> key) {
+		return this.map.containsKey(key);
+	}
+
+	@Override
+	public boolean containsValue(Object value) {
+		return this.map.containsValue(value);
+	}
+
+	@Override
+	public V get(DuplexKey<A, B> key) {
+		return map.get(key);
+	}
+
+	@Override
+	public V put(DuplexKey<A, B> tKey, V value) {
+		
+		if(!mapAKey.containsKey(tKey.getAKey())){
+			List<DuplexKey<A, B>> list = new LinkedList<DuplexKey<A, B>>();
+			Collections.sort(list, new DuplexMapSortByNormalizedOV<V, A, B>(this));
+			mapAKey.put(tKey.getAKey(), list);
+		}
+		
+		List<DuplexKey<A, B>> list = mapAKey.get(tKey.getAKey());
+		if(value instanceof Entity){
+			//sorted insertion
+			if(!list.contains(tKey)){
+				int index = Collections.binarySearch(list, tKey, new DuplexMapKeepSortByNormalizedOV<V, A, B>(this, value));
+				if (index < 0) index = ~index;
+				list.add(index, tKey);	
+			}	
+		}else{
+			//unsorted insertion
+			if(!list.contains(tKey)){
+				list.add(tKey);
+			}	
+		}
+		
+		this.mapOwnKey.put(tKey.getOwnKey(), tKey);
+		return this.map.put(tKey, value);
+		
+		//****
+		/*
+		
+		
+		
+		if(!mapAKey.containsKey(tKey.getAKey())){
+			List<DuplexKey<A, B>> list = new ArrayList<DuplexKey<A, B>>();
+			mapAKey.put(tKey.getAKey(), list);
+		}
+		if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
+			mapAKey.get(tKey.getAKey()).add(tKey);
+		}
+		
+		this.mapOwnKey.put(tKey.getOwnKey(), tKey);
+		return this.map.put(tKey, value);
+		*/
+	}
+
+	@Override
+	public V remove(DuplexKey<A, B> key) {
+		if(mapAKey.containsKey(key.getAKey())){
+			mapAKey.get(key.getAKey()).remove(key);
+		}
+		this.mapOwnKey.remove(key.getOwnKey());
+		return this.map.remove(key);
+	}
+
+	@Override
+	public void clear() {
+		this.map.clear();
+		this.mapAKey.clear();
+		this.mapOwnKey.clear();
+	}
+
+	@Override
+	public Collection<V> values() {
+		return this.map.values();
+	}
+
+	
+}