1 /*
2 * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package edu.internet2.middleware.shibboleth.idp.authn;
18
19 import java.io.Serializable;
20 import java.security.Principal;
21
22 import org.opensaml.xml.util.DatatypeHelper;
23
24 /** A basic implementation of {@link Principal}. */
25 public class UsernamePrincipal implements Principal, Serializable {
26
27 /** Serial version UID. */
28 private static final long serialVersionUID = 8708917521896240626L;
29
30 /** Name of the principal. */
31 private String name;
32
33 /**
34 * Constructor.
35 *
36 * @param principalName name of the principal
37 */
38 public UsernamePrincipal(String principalName) {
39 name = DatatypeHelper.safeTrimOrNullString(principalName);
40 }
41
42 /** {@inheritDoc} */
43 public String getName() {
44 return name;
45 }
46
47 /** {@inheritDoc} */
48 public String toString() {
49 return "{BasicPrincipal}" + getName();
50 }
51
52 /** {@inheritDoc} */
53 public int hashCode() {
54 return name.hashCode();
55 }
56
57 /** {@inheritDoc} */
58 public boolean equals(Object obj) {
59 if (obj == this) {
60 return true;
61 }
62
63 if (obj instanceof UsernamePrincipal) {
64 return DatatypeHelper.safeEquals(getName(), ((UsernamePrincipal) obj).getName());
65 }
66
67 return false;
68 }
69 }