KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > PrincipalNameMapping


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share;
20
21 /** Mapping of principal name and optional class-name field.
22  *
23  * The one interesting characteristic of this class is that equals and hashCode
24  * only take into account the principal name field so two instances with the same
25  * principal name but different classnames would be considered equal. The reason
26  * for this is because only the principal name is used as a key when searching
27  * for this object in a collection. It does not make sense to have two instances
28  * that differ only by classname.
29  *
30  * @author Peter Williams
31  */

32 public final class PrincipalNameMapping {
33     
34     private String JavaDoc principalName;
35     private String JavaDoc className;
36
37     public PrincipalNameMapping(String JavaDoc pn) {
38         this(pn, null);
39     }
40
41     public PrincipalNameMapping(String JavaDoc pn, String JavaDoc cn) {
42         assert(pn != null) : "Principal name cannnot be null";
43         
44         principalName = pn;
45         className = cn;
46     }
47
48     public String JavaDoc toString() {
49         if(className == null || className.length() == 0) {
50             return principalName;
51         }
52         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(principalName.length() + className.length() + 10);
53         buffer.append(principalName);
54         buffer.append(" [cn=");
55         buffer.append(className);
56         buffer.append("]");
57         return buffer.toString();
58     }
59
60     public String JavaDoc getPrincipalName() {
61         return principalName;
62     }
63
64     public String JavaDoc getClassName() {
65         return className;
66     }
67
68     public boolean equals(Object JavaDoc obj) {
69         boolean result = false;
70
71         if(obj instanceof PrincipalNameMapping) {
72             result = principalName.equals(((PrincipalNameMapping) obj).getPrincipalName());
73         }
74
75         return result;
76     }
77
78     public int hashCode() {
79         return principalName.hashCode();
80     }
81 }
82
Popular Tags