changeset 2:57d19e93f1c3

adding mapping classes
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Fri, 08 May 2015 16:36:25 +0200
parents 1af9d7db348e
children 7ffaefcee5b5
files pom.xml src/main/java/cl/maps/duplex/DuplexKey.java src/main/java/cl/maps/duplex/DuplexMap.java src/main/java/cl/maps/duplex/IDuplexMap.java
diffstat 4 files changed, 267 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pom.xml	Tue May 05 15:35:46 2015 +0200
+++ b/pom.xml	Fri May 08 16:36:25 2015 +0200
@@ -11,15 +11,15 @@
 
 	
 	<dependencies>
-	
+		<!-- 
 	    <dependency>
             <groupId>cl.talca</groupId>
             <artifactId>hashMapping</artifactId>
             <version>1.0</version>
-            <scope>system</scope>
+            <scope>compile</scope>
             <systemPath>${basedir}/lib/hash-mapping-1.0.jar</systemPath>
         </dependency>		
-		<!-- 
+		
 		<dependency>
 			<groupId>javax.servlet</groupId>
 			<artifactId>javax.servlet-api</artifactId>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/cl/maps/duplex/DuplexKey.java	Fri May 08 16:36:25 2015 +0200
@@ -0,0 +1,81 @@
+package cl.maps.duplex;
+
+
+public class DuplexKey<A, B> {
+	
+	private A aKey;
+	private B ownKey;
+	
+	public DuplexKey(A aKey, B ownKey){
+		this.aKey = aKey;
+		this.ownKey = ownKey;
+	}
+	
+	public boolean equalsAKey(A key){
+		if(aKey != null && key != null){
+			return aKey.equals(key);
+		}else if(aKey == null && key == null){
+			return true;
+		}
+		return false;
+	}
+	
+	public boolean equalsOwnKey(B key){
+		if(ownKey != null && key != null){
+			return ownKey.equals(key);
+		}else if(ownKey == null && key == null){
+			return true;
+		}
+		return false;
+	}
+	
+	public A getAKey() {
+		return aKey;
+	}
+
+	public void setAKey(A aKey) {
+		this.aKey = aKey;
+	}
+
+	public B getOwnKey() {
+		return ownKey;
+	}
+
+	public void setOwnKey(B ownKey) {
+		this.ownKey = ownKey;
+	}
+	
+	@Override
+	public boolean equals(Object o){
+		if(o instanceof DuplexKey){
+			try {
+				DuplexKey<A, B> other = (DuplexKey<A, B>)o;
+				
+				if(this.equalsOwnKey(other.getOwnKey()) &&
+						this.equalsAKey(other.getAKey())){
+					return true;
+				}	
+			} catch (Exception e) {}
+			
+		}
+		return false;
+	}
+	
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((aKey == null) ? 0 : aKey.hashCode());
+		result = prime * result + ((ownKey == null) ? 0 : ownKey.hashCode());
+		
+		return result;
+	}
+
+	@Override
+	public String toString() {
+		if (aKey == null || ownKey == null) {
+			return super.toString();
+		}
+		return "DuplexKey [" + aKey.toString() + ", " + ownKey.toString() + "]";
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/cl/maps/duplex/DuplexMap.java	Fri May 08 16:36:25 2015 +0200
@@ -0,0 +1,132 @@
+package cl.maps.duplex;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+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 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();
+	}
+
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/cl/maps/duplex/IDuplexMap.java	Fri May 08 16:36:25 2015 +0200
@@ -0,0 +1,51 @@
+package cl.maps.duplex;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+
+public interface IDuplexMap<V, A, B> {
+
+	int size();
+	
+	boolean isEmpty();
+
+	boolean containsKey(DuplexKey<A, B> key);
+
+	boolean containsValue(Object value);
+	
+	V get(DuplexKey<A, B> key);
+	
+	V put(DuplexKey<A, B> key, V value);
+
+	V remove(DuplexKey<A, B> key);
+
+	//void putAll(Map<? extends K, ? extends V> m);
+
+	void clear();
+
+	Set<DuplexKey<A, B>> keySet();
+
+	Collection<V> values();
+
+	Set<Map.Entry<DuplexKey<A, B>, V>> entrySet();
+
+	boolean equals(Object o);
+
+	/**
+	  * Returns the hash code value for this map.  The hash code of a map is
+	  * defined to be the sum of the hash codes of each entry in the map's
+	  * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
+	  * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
+	  * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
+	  * {@link Object#hashCode}.
+	  *
+	  * @return the hash code value for this map
+	  * @see Map.Entry#hashCode()
+	  * @see Object#equals(Object)
+	  * @see #equals(Object)
+	  */
+	int hashCode();
+	
+}