package org.pmdreports.model;

import java.util.Collection;

/**
 * Represents a component (or actually high-level directory under which Java
 * source is found) to be analyzed for code quality.
 * 
 * @author Ben Northrop Aug 22, 2007
 */
public class Component {

    // DATA MEMBERS
    private final String name;

    private final String path;

    private Project project;

    private Collection<Statistics> statisticsResults;

    // CONSTRUCTOR
    public Component(String name, String path) {
        this.name = name;
        this.path = path;
    }

    public Component(String path) {
        this.path = path;
        this.name = path;
    }

    public Component(String name, String path, Project project) {
        this.name = name;
        this.path = path;
        this.project = project;
    }

    // ACCESSORS
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @return the statisticsResults
     */
    public Collection<Statistics> getStatisticsResults() {
        return statisticsResults;
    }

    /**
     * @param statisticsResults
     *            the statisticsResults to set
     */
    public void setStatisticsResults(Collection<Statistics> statisticsResults) {
        this.statisticsResults = statisticsResults;
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((name == null) ? 0 : name.hashCode());
        result = PRIME * result + ((path == null) ? 0 : path.hashCode());
        return result;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof Component)) {
            return false;
        }

        final Component other = (Component) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        }
        else if (!name.equals(other.name)) {
            return false;
        }
        if (path == null) {
            if (other.path != null) {
                return false;
            }
        }
        else if (!path.equals(other.path)) {
            return false;
        }
        return true;
    }

    /**
     * @return the project
     */
    public Project getProject() {
        return project;
    }

    /**
     * Generate a string representation.
     */
    public String toString() {
        StringBuffer sb = new StringBuffer(100);
        sb.append("Component(");
        sb.append(name);
        sb.append(", ");
        sb.append(path);
        sb.append(')');
        return sb.toString();
    }

}
