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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.maven.project.Project;

/**
 * @author mmaczka
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class MultiDependency implements Comparable
{
    private Map versions = new HashMap();
    private String id = null;
   
    /**
     * 
     */
    public int compareTo(Object obj)
    {
        MultiDependency other = (MultiDependency) obj;
        if (this.isClean() && !other.isClean())
        {
            return 1;
        }
        if (!this.isClean() && other.isClean())
        {
            return -1;
        }
        return this.id.compareTo(other.id);
    }
    /**
     * @return
     */
    public String getId()
    {
        return id;
    }
    /**
     * @param id
     */
    public void setId(String id)
    {
        this.id = id;
    }
    public boolean isClean()
    {
        return versions.size() == 1;
    }
    /**
     * 
     * @param project the name of the project which has this dependecy
     * @param version the version of the dependecy used in the project
     */
    public void add(Project project, String version)
    {
        List projectList = null;
        if (versions.containsKey(version))
        {
            projectList = (List) versions.get(version);
        }
        else
        {
            projectList = new ArrayList();
            versions.put(version, projectList);
        }
        projectList.add(project);
    }
    public Iterator getProjects(String version)
    {
        List projects = (List) versions.get(version);
        return projects.iterator();
    }
    public Iterator getVersions()
    {
        return versions.keySet().iterator();
    }
    public String toString()
    {
        return id;
    }
    /**
     * @return
     */
    public int getNumberOfVersions()
    {
        
        return versions.size();
    }
}
