package org.apache.maven.multiproject.harmonizer;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;

/**
 * Helper Bean which helps to generate a dependency report for given  list of projects
 * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
 * @version $Id$
 */
public class DependencyHarmonizer
{
    private Map deps = new HashMap();
    private int numberOfProjects;
    private final static int NOT_SET = -1;
    private int numOfArtifacts = NOT_SET;
    
    
    /**
     * @return
     */
    public Iterator getDependencies()
    {
        // convert map values to list 
        List list = new ArrayList();
        list.addAll(deps.values());
        Collections.sort(list);
        return list.iterator();
    }
    public void build(List projects)
    {
        numberOfProjects = projects.size();
        for (Iterator projectIter = projects.iterator();
            projectIter.hasNext();
            )
        {
            Project project = (Project) projectIter.next();
            for (Iterator depIter = project.getDependencies().iterator();
                depIter.hasNext();
                )
            {
                Dependency dep = (Dependency) depIter.next();
                String id = dep.getId();
                MultiDependency multiDep = null;
                if (deps.containsKey(id))
                {
                    multiDep = (MultiDependency) deps.get(id);
                }
                else
                {
                    multiDep = new MultiDependency();
                    multiDep.setId(id);
                    deps.put(id, multiDep);
                }
                multiDep.add(project, dep.getVersion());
            }
        }
    }
    /**
     * 
     * @return
     */
    public int getNumberOfProjects()
    {
        return numberOfProjects;
    }
    /**
     * 
     * @return
     */
    public int getNumberOfDependencies()
    {
        return deps.size();
    }
    /**
     * 
     * @return
     */
    public int getNumberOfArtifacts()
    {
        if (numOfArtifacts != NOT_SET)
        {
           return numOfArtifacts;
        }
		numOfArtifacts = 0;
        for (Iterator iter = deps.keySet().iterator(); iter.hasNext();)
        {
            String key = (String)iter.next();
            MultiDependency dep = (MultiDependency) deps.get(key) ;
            numOfArtifacts += dep.getNumberOfVersions();
        }
        return numOfArtifacts;
    }
    public double getConvergence()
    {
        if (getNumberOfArtifacts() > 0)
        {
    	    return (double)getNumberOfDependencies()/getNumberOfArtifacts();
    	}
    	return 100d;
    }
}