import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class ServerContainer {
	private Map<String, Set<Server>> objServerContainer;
	private Map<String, Server> ipportServerContainer;

	ServerContainer() {
		objServerContainer = new HashMap<String, Set<Server>>();
		ipportServerContainer = new HashMap<String, Server>();
	}

	public boolean containsServer(ConObject obj1, ConObject obj2) {
	    sortByAlphabet(obj1,obj2);
		return objServerContainer.containsKey(obj1.name + " " + obj2.name);
	}
	
	public Server getOneServer(ConObject obj1, ConObject obj2){
	    sortByAlphabet(obj1,obj2);
	    if(containsServer(obj1,obj2)){
	        Set <Server> hs = objServerContainer.get(obj1.name + " " + obj2.name);
	        Iterator <Server> iter = hs.iterator();
	        if(iter.hasNext()){
	        	return iter.next();
	        }else{
	        	System.err.println("ERROR! No Server in it");
	        }
	    }
	    return null;
	}
	
	public Set<Server> getAllServer(ConObject obj1, ConObject obj2){
	    sortByAlphabet(obj1,obj2);
	    if(containsServer(obj1,obj2)){
	        return objServerContainer.get(obj1.name + " " + obj2.name);
	    }else{
	        return null;
	    }
	}
	
	public boolean addServer(Server server){
	    sortByAlphabet(server.obj1,server.obj2);
	    String ipport = server.ip + ":" + server.port;
	    if(ipportServerContainer.containsKey(ipport)){
	        return false;
	    }
	    
	 	ipportServerContainer.put(ipport, server);

	  	String obj = server.obj1.name + " " + server.obj2.name;
	    if(containsServer(server.obj1, server.obj2)){
	        Set<Server> hs = getAllServer(server.obj1,server.obj2);
	        hs.add(server);
	    }else{
	        Set <Server>hs = new HashSet<Server>();
	        hs.add(server);
	        objServerContainer.put(obj,hs);
	    }
	    server.obj1.addConnectedObj(server.obj2);
        server.obj2.addConnectedObj(server.obj1);
	    return true;
	}
	
	public boolean removeServer(String ip, int port){
	    Server server;
	    String ipport = ip + ":" + port;
	    if(ipportServerContainer.containsKey(ipport)){
	        server = ipportServerContainer.get(ipport);
	        ipportServerContainer.remove(ipport);
	        String obj = server.obj1.name + " " + server.obj2.name;
	        Set<Server> hs = objServerContainer.get(obj);
	        hs.remove(server);
	        if(hs.size() == 0){
	            objServerContainer.remove(obj);
	            server.obj1.removeConnectedObj(server.obj2);
	            server.obj2.removeConnectedObj(server.obj1);
	        }
	        return true;
	    }else{
	        return false;
	    }
	}
	
	private void sortByAlphabet(ConObject obj1, ConObject obj2){
	    if(obj1.name.compareTo(obj2.name) > 0){
	        ConObject temp = obj1;
	        obj1 = obj2;
	        obj2 = temp;
	    }
	}
}
@@ -494,34 +494,130 @@
     
-sortByAlphabet(obj1,obj2);
+if(obj1.name.compareTo(obj2.name) %3E 0)%7B%0A%09        ConObject temp = obj1;%0A%09        obj1 = obj2;%0A%09        obj2 = temp;%0A%09    %7D
 %0A%09%09r
@@ -758,70 +758,274 @@
     
-sortByAlphabet(obj1,obj2);%0A%09    if(containsServer(obj1,obj2))%7B
+if(obj1.name.compareTo(obj2.name) %3E 0)%7B%0A%09        ConObject temp = obj1;%0A%09        obj1 = obj2;%0A%09        obj2 = temp;%0A%09    %7D%0A%09    System.out.println(%22get %22+obj1.name + %22 %22 + obj2.name);%0A%09    if(containsServer(obj1,obj2))%7B%0A%09        System.out.println(%22Contains obj12%22);
 %0A%09  
@@ -1397,34 +1397,130 @@
     
-sortByAlphabet(obj1,obj2);
+if(obj1.name.compareTo(obj2.name) %3E 0)%7B%0A%09        ConObject temp = obj1;%0A%09        obj1 = obj2;%0A%09        obj2 = temp;%0A%09    %7D
 %0A%09  
@@ -1718,35 +1718,229 @@
     
-sortByAlphabet(server.obj1,
+if(server.obj1.name.compareTo(server.obj2.name) %3E 0)%7B%0A%09        ConObject temp = server.obj1;%0A%09        server.obj1 = server.obj2;%0A%09        server.obj2 = temp;%0A%09    %7D%0A%09    System.out.println(%22add %22+server.obj1.name + %22 %22 + 
 serv
@@ -1938,32 +1938,37 @@
  %22 + server.obj2
+.name
 );%0A%09    String i
@@ -3277,200 +3277,5 @@
 %0A%09%7D%0A
-%09%0A%09private void sortByAlphabet(ConObject obj1, ConObject obj2)%7B%0A%09    if(obj1.name.compareTo(obj2.name) %3E 0)%7B%0A%09        ConObject temp = obj1;%0A%09        obj1 = obj2;%0A%09        obj2 = temp;%0A%09    %7D%0A%09%7D%0A
 %7D

fix both direction