1/*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.security.auth;
27
28import java.security.Principal;
29
30/**
31 * A user principal identified by a username or account name.
32 *
33 * <p>
34 * After successful authentication, a user {@link java.security.Principal}
35 * can be associated with a particular {@link javax.security.auth.Subject}
36 * to augment that <code>Subject</code> with an additional identity.
37 * Authorization decisions can then be based upon the
38 * <code>Principal</code>s that are associated with a <code>Subject</code>.
39 *
40 * <p>
41 * This class is immutable.
42 *
43 * @since 1.6
44 */
45public final class UserPrincipal implements Principal, java.io.Serializable {
46
47    private static final long serialVersionUID = 892106070870210969L;
48
49    /**
50     * The principal's name
51     *
52     * @serial
53     */
54    private final String name;
55
56    /**
57     * Creates a principal.
58     *
59     * @param name The principal's string name.
60     * @exception NullPointerException If the <code>name</code> is
61     * <code>null</code>.
62     */
63    public UserPrincipal(String name) {
64        if (name == null) {
65            throw new NullPointerException("null name is illegal");
66        }
67        this.name = name;
68    }
69
70    /**
71     * Compares this principal to the specified object.
72     *
73     * @param object The object to compare this principal against.
74     * @return true if they are equal; false otherwise.
75     */
76    public boolean equals(Object object) {
77        if (this == object) {
78            return true;
79        }
80        if (object instanceof UserPrincipal) {
81            return name.equals(((UserPrincipal)object).getName());
82        }
83        return false;
84    }
85
86    /**
87     * Returns a hash code for this principal.
88     *
89     * @return The principal's hash code.
90     */
91    public int hashCode() {
92        return name.hashCode();
93    }
94
95    /**
96     * Returns the name of this principal.
97     *
98     * @return The principal's name.
99     */
100    public String getName() {
101        return name;
102    }
103
104    /**
105     * Returns a string representation of this principal.
106     *
107     * @return The principal's name.
108     */
109    public String toString() {
110        return name;
111    }
112}
113