/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)JCKPrincipal.java	1.3 02/01/12
 *
 * ------------------------------------------------------------------
 *  (C) COPYRIGHT INTERNATIONAL BUSINESS MACHINES CORPORATION 1999.
 *                       ALL RIGHTS RESERVED
 *                         IBM Confidential
 * ------------------------------------------------------------------
 */

package com.sun.cts.util.tests.jaas; 

import java.security.Principal;

/**
 * This file contains a simple Principal implementation for
 * purposes of JAAS JCK testing.
 *
 * <p> This class implements the <code>Principal</code> interface
 * and represents a generic user.
 *
 * <p> Principals such as this <code>JCKPrincipal</code>
 * may be associated with a particular <code>Subject</code>
 * to augment that <code>Subject</code> with an additional
 * identity.  Refer to the <code>Subject</code> class for more information
 * on how to achieve this.  Authorization decisions can then be based upon
 * the Principals associated with a <code>Subject</code>.
 *
 * @see java.security.Principal
 * @see javax.security.auth.Subject
 *
 * @(#)author:  D. Kent Soper
 * @(#)version: 1.0
 * @(#)date:    99/07/27
 */

public class JCKPrincipal implements Principal, java.io.Serializable {

    /**
     * @serial
     */
    private String name;

    /**
     * Create a JCKPrincipal with a generic username.
     *
     * <p>
     *
     * @param name the username for this user.
     *
     * @exception NullPointerException if the <code>name</code>
     *			is <code>null</code>.
     */
    public JCKPrincipal(String name) {
	if (name == null)
	    throw new NullPointerException("illegal null input");

	this.name = name;
    }

    /**
     * Return the Unix username for this <code>JCKPrincipal</code>.
     *
     * <p>
     *
     * @return the Unix username for this <code>JCKPrincipal</code>
     */
    public String getName() {
	return name;
    }

    /**
     * Return a string representation of this <code>JCKPrincipal</code>.
     *
     * <p>
     *
     * @return a string representation of this <code>JCKPrincipal</code>.
     */
    public String toString() {
	return("JCKPrincipal:  " + name);
    }

    /**
     * Compares the specified Object with this <code>JCKPrincipal</code>
     * for equality.  Returns true if the given object is also a
     * <code>JCKPrincipal</code> and the two JCKPrincipals
     * have the same username.
     *
     * <p>
     *
     * @param o Object to be compared for equality with this
     *		<code>JCKPrincipal</code>.
     *
     * @return true if the specified Object is equal equal to this
     *		<code>JCKPrincipal</code>.
     */
    public boolean equals(Object o) {
	if (o == null)
	    return false;

        if (this == o)
            return true;

        if (!(o instanceof JCKPrincipal))
            return false;
        JCKPrincipal that = (JCKPrincipal)o;

	if (this.getName().equals(that.getName()))
	    return true;
	return false;
    }

    /**
     * Return a hash code for this <code>JCKPrincipal</code>.
     *
     * <p>
     *
     * @return a hash code for this <code>JCKPrincipal</code>.
     */
    public int hashCode() {
	return name.hashCode();
    }
}
