1 /*
2 * Copyright (c) 1996, 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
26 package java.security;
27
28 import javax.security.auth.Subject;
29
30 /**
31 * This interface represents the abstract notion of a principal, which
32 * can be used to represent any entity, such as an individual, a
33 * corporation, and a login id.
34 *
35 * @see java.security.cert.X509Certificate
36 *
37 * @author Li Gong
38 * @since 1.1
39 */
40 public interface Principal {
41
42 /**
43 * Compares this principal to the specified object. Returns true
44 * if the object passed in matches the principal represented by
45 * the implementation of this interface.
46 *
47 * @param another principal to compare with.
48 *
49 * @return true if the principal passed in is the same as that
50 * encapsulated by this principal, and false otherwise.
51 */
52 public boolean equals(Object another);
53
54 /**
55 * Returns a string representation of this principal.
56 *
57 * @return a string representation of this principal.
58 */
59 public String toString();
60
61 /**
62 * Returns a hashcode for this principal.
63 *
64 * @return a hashcode for this principal.
65 */
66 public int hashCode();
67
68 /**
69 * Returns the name of this principal.
70 *
71 * @return the name of this principal.
72 */
73 public String getName();
74
75 /**
76 * Returns true if the specified subject is implied by this principal.
77 *
78 * @implSpec
79 * The default implementation of this method returns true if
80 * {@code subject} is non-null and contains at least one principal that
81 * is equal to this principal.
82 *
83 * <p>Subclasses may override this with a different implementation, if
84 * necessary.
85 *
86 * @param subject the {@code Subject}
87 * @return true if {@code subject} is non-null and is
88 * implied by this principal, or false otherwise.
89 * @since 1.8
90 */
91 public default boolean implies(Subject subject) {
92 if (subject == null)
93 return false;
94 return subject.getPrincipals().contains(this);
95 }
96 }
97