/**
* Map which allows access either with A or with B.
* There are used TreeMaps inside, thus the Maps will be sorted.
* If there are several equal A or B they will be put in a List.
* All methods in this class are synchronized.
* There is no remove method, because it would destroy the equality of the inner maps.
*/
public class DoubleMap<A,B>
{
private Map<A,ArrayList[B]> mapAB = new TreeMap<A,ArrayList[B]>();
private Map<B,ArrayList<A>> mapBA = new TreeMap<B,ArrayList<A>>();
public synchronized void put (A a, B b)
{
if (mapAB.get(a) == null){
mapAB.put(a, new ArrayList[B]());
}
mapAB.get(a).add (b);
if (mapBA.get(b) == null){
System.out.println ("PUT NEW B");
mapBA.put(b, new ArrayList<A>());
}
mapBA.get(b).add(a);
}
public synchronized void clear()
{
mapAB.clear();
mapBA.clear();
}
public synchronized List<A> getAforB (B b)
{
return mapBA.get(b);
}
public synchronized List[B] getBforA (A a)
{
return mapAB.get(a);
}
public synchronized Set<A> getASet()
{
return mapAB.keySet();
}
public synchronized Set[B] getBSet()
{
return mapBA.keySet();
}
public synchronized boolean isEmpty()
{
return mapAB.isEmpty();
}
}