/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package graphvanrossum;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author martin
 */
public class GraphVanRossum {
    private Map<Object,Set<Object>> map;

    public GraphVanRossum() {
        map= new HashMap<>();
    }
    
    public void addLink(Object first, Object Second){
        Set<Object> tmp;
        if (map.containsKey(first))
            tmp = map.get(first);
        else{
            tmp = new HashSet<>();
            map.put(first,tmp);
        }
        tmp.add(Second);
        
        if (map.containsKey(Second))
            tmp = map.get(Second);
        else{
            tmp = new HashSet<>();
            map.put(Second,tmp);
        }
        tmp.add(first);
    }
    
    public Set<Object> neighbor(Object node){
        Set<Object> tmp;
        if (map.containsKey(node))
            tmp = map.get(node);
        else
            tmp = new HashSet<>();
        return Collections.unmodifiableSet(tmp);
    }
    
    public void remove(Object node){
        if (map.containsKey(node)){
            for(Object key : map.get(node))
                map.get(key).remove(node);
            map.remove(node);
        }
    }
    
    public void unlink(Object first, Object second){
        if (map.containsKey(first)&&map.containsKey(second)){
            map.get(first).remove(second);
            map.get(second).remove(first);
        }
    }
}
