/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.showdependencies;

import java.util.Vector;
import java.util.Hashtable;
import com.togethersoft.openapi.sci.SciClass;

/**
 * This class is responsible for sorting the found Dependency objects
 * into two Hashtables used for building the tree.
 *
 * @author TogetherSoft
 */
class DependenciesHandler
{
    /**
     * Creates class instance with specified parameter.
     *
     * @param Vector object contained dependencies info
     */
    public DependenciesHandler(Vector dependencies)
    {
        myDependencies = dependencies;
        victimsHT = new Hashtable();
        causesHT = new Hashtable();
        sortDependencies();
    }

    /**
     * Returns Hashtable of Hashtables.
     * key: Victim -> another Hashtable (key:Cause -> Dependency)
     *
     * @return victim Hashtable object
     */
    public Hashtable getVictimsData()
    {
        return victimsHT;
    }

    /**
     * Returns Hashtable of Hashtables.
     * key: Cause -> another Hashtable (key:Victim -> Dependency)
     *
     * @return causes Hashtable object
     */
    public Hashtable getCausesData()
    {
        return causesHT;
    }

    /**
     * Sorts dependencies.
     */
    private void sortDependencies()
    {
        final int dependenciesSize = myDependencies.size();

        for (int i = 0; i < dependenciesSize; i++)
        {
            Dependency nextDependency = (Dependency)myDependencies.elementAt(i);

            SciClass victim = nextDependency.getVictim();
            SciClass cause = nextDependency.getCause();

            Hashtable causesHTForTheVictim = (Hashtable)victimsHT.get(victim);
            if (causesHTForTheVictim == null)
            {
                causesHTForTheVictim = new Hashtable();
            }

            Vector data = (Vector)causesHTForTheVictim.get(cause);
            if (data == null)
            {
                data = new Vector();
            }

            data.addElement(nextDependency);
            causesHTForTheVictim.put(cause, data);
            victimsHT.put(victim, causesHTForTheVictim);

            Hashtable victimsHTForTheCause = (Hashtable)causesHT.get(cause);
            if (victimsHTForTheCause == null)
            {
                victimsHTForTheCause = new Hashtable();
            }

            data = (Vector)victimsHTForTheCause.get(victim);
            if (data == null)
            {
                data = new Vector();
            }

            data.addElement(nextDependency);
            victimsHTForTheCause.put(victim, data);
            causesHT.put(cause, victimsHTForTheCause);
        }
    }

    private Vector myDependencies; // vector with found dependencies
    private Hashtable victimsHT;   // key: Victim -> another Hashtable (key:Cause -> Dependency)
    private Hashtable causesHT;    // key: Cause -> another Hashtable (key:Victim -> Dependency)
}


