/* Copyright 2000 by Basil Vandegriend.  All rights reserved. */

package com.basilv.core;

import java.io.Serializable;

/**
 * Represents an object that is identified by its name. The name property is
 * immutable, and is used to determine equality. This class is intended to be
 * used as a base class.
 */
public class NamedObject implements Serializable, Comparable<NamedObject>
{
	private static final long serialVersionUID = 1L;

	private String name = null;

	/**
	 * Create the object with the specified name.
	 * 
	 * @param newName  Cannot be null.
	 */
	public NamedObject(String newName) {
		Assert.notNull("newName", newName);
		name = newName;
	}

	protected String getName() {
		return name;
	}

	/**
	 * Implements the Comparable interface.
	 * 
	 * @param obj  The object to compare this instance with. Must be a non-null
	 *        instance of NamedObject.
	 */
	public int compareTo(NamedObject namedObj) {
		return getName().compareTo(namedObj.getName());
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}

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

		NamedObject otherObj = (NamedObject) obj;

		return (getName().equals(otherObj.getName()));
	}

	@Override
	public int hashCode() {
		return getName().hashCode();
	}

	@Override
	public String toString() {
		return getName();
	}

} // class NamedObject
