package org.pmdreports.model;

import java.security.InvalidParameterException;

import org.pmdreports.util.StringUtil;

/**
 * A PMD Ruleset used to generate code quality statistics.
 * 
 * @author Ben Northrop Sep 24, 2007
 */
public class Ruleset {
	// DATA MEMBERS
	private final String name;

	private final String fileName;

	private final String description;

	private final Double minViolations;

	private final Double maxViolations;

	private final Boolean isShowAbsolute;

	// CONSTRUCTORS
	/**
	 * Default constructor for immutability.
	 */
	public Ruleset(String name, String description, String fileName,
			String minViolationsPerThousand, String maxViolationsPerThousand,
			String strIsShowAbsolute) {
		if (StringUtil.isEmpty(name)) {
			throw new InvalidParameterException("name is empty!");
		}
		if (StringUtil.isEmpty(description)) {
			throw new InvalidParameterException("description is empty!");
		}
		this.minViolations = minViolationsPerThousand == null ? null : Double
				.parseDouble(minViolationsPerThousand);
		this.maxViolations = maxViolationsPerThousand == null ? null : Double
				.parseDouble(maxViolationsPerThousand);
		this.name = name;
		this.fileName = fileName;
		this.description = description;

		this.isShowAbsolute = strIsShowAbsolute == null
				|| strIsShowAbsolute.equalsIgnoreCase("true");
	}

	// GETTERS
	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @return the longName
	 */
	public String getDescription() {
		return description;
	}

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

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

	/**
	 * Hash code based on 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 based on name
	 * 
	 * @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 Ruleset other = (Ruleset) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	/**
	 * @return the maxViolationsPerThousand
	 */
	public Double getMaxViolations() {
		return maxViolations;
	}

	/**
	 * @return the minViolationsPerThousand
	 */
	public Double getMinViolations() {
		return minViolations;
	}

	public Boolean getIsShowAbsolute() {
		return isShowAbsolute;
	}

}
