package org.pmdreports.model;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import org.pmdreports.util.StringUtil;

/**
 * The project for which one or more components have been analyzed.
 * 
 * @author Ben Northrop Aug 22, 2007
 */
public class Project {

	// DATA MEMBERS
	private final String name;

	private final String srcRoot;

	private final List<Component> components = new ArrayList<Component>();

	// CONSTRUCTOR
	public Project(String name, String srcRoot) {
		if (StringUtil.isEmpty(name)) {
			throw new InvalidParameterException(
					"project attribute 'name' is null");
		}
		if (StringUtil.isEmpty(srcRoot)) {
			throw new InvalidParameterException(
					"project attribute 'srcRoot' is null for project: " + name);
		}
		this.name = name;
		this.srcRoot = srcRoot;
	}

	public Project(String srcRoot) {
		if (StringUtil.isEmpty(srcRoot)) {
			throw new InvalidParameterException(
					"project attribute 'srcRoot' is null.");
		}
		this.srcRoot = srcRoot;
		this.name = srcRoot;
	}

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

	/**
	 * Add a component to the master list.
	 */
	public void addComponent(Component component) {
		if (component != null) {
			components.add(component);
		}
	}

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

	/**
	 * @return the srcRoot
	 */
	public String getSrcRoot() {
		return srcRoot;
	}

	/**
	 * @return the components
	 */
	public List<Component> getComponents() {
		return components;
	}

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

	/**
	 * Equals using name attribute.
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Project other = (Project) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

}
