view src/main/java/cl/maps/duplex/DuplexMap.java @ 93:88a485937f9a

New feature : export csv file with new two fields : book year and edition year
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Wed, 21 Jun 2017 06:25:34 +0200
parents 57d19e93f1c3
children
line wrap: on
line source

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();
	}

	
}