//$Id: ClassName.java,v 1.2 2003/08/25 17:16:19 maxcsaucdk Exp $
package net.sf.hibernate.tool.hbm2java;

import java.util.HashSet;
import java.util.Set;

import net.sf.hibernate.util.StringHelper;

public class ClassName {
	
	static Set primitives = new HashSet();
	static {
		primitives.add("byte");
		primitives.add("short");
		primitives.add("int");
		primitives.add("long");		
		primitives.add("float");
		primitives.add("double");
		primitives.add("char");
		primitives.add("boolean");
	}
	private String fullyQualifiedName = null;
	private String packageName = null;
	private String name = null;

	public void setFullyQualifiedName(String fullyQualifiedName) {
		setFullyQualifiedName(fullyQualifiedName, false);
	}

	public void setFullyQualifiedName(String fullyQualifiedName, boolean isPrimitive) {
		this.fullyQualifiedName = fullyQualifiedName;
		if (!isPrimitive()) {
			if (fullyQualifiedName != null) {

				int lastDot = fullyQualifiedName.lastIndexOf(".");
				if (lastDot < 0) {
					name = fullyQualifiedName;
					packageName = null;
				} else {
					name = fullyQualifiedName.substring(lastDot + 1);
					packageName = fullyQualifiedName.substring(0, lastDot);
				}
			} else {
				name = fullyQualifiedName;
				packageName = null;
			}
		} else {
			name = fullyQualifiedName;
			packageName = null;
		}

	}

	public String getFullyQualifiedName() {
		return this.fullyQualifiedName;
	}

	public String getPackageName() {
		return this.packageName;
	}

	public String getName() {
		return this.name;
	}

	public boolean inJavaLang() {
		return "java.lang".equals(packageName);
	}

	public boolean inSamePackage(ClassName other) {
		return other.packageName == this.packageName
			|| (other.packageName != null && other.packageName.equals(this.packageName));
	}

	public boolean equals(Object other) {
		ClassName otherClassName = (ClassName) other;
		return otherClassName.fullyQualifiedName.equals(fullyQualifiedName);
	}
		
	/**
	 * Method isPrimitive.
	 * @return boolean
	 */
	public boolean isPrimitive() {
		String name = StringHelper.replace(fullyQualifiedName, "[]", "");
		return primitives.contains(name);
	}
	
	public boolean isArray() {
		return fullyQualifiedName.endsWith("[]");
	}

	public String toString() {
		return getFullyQualifiedName();
	}

}
