001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.security;
017    
018    import java.security.Principal;
019    
020    /**
021     * A basic implementation of the <code>Principal</code> interface.
022     * 
023     * @author Won Chul Doh
024     * @since Jul 18, 2007
025     * 
026     */
027    public class BasicPrincipal implements Principal {
028            
029            private String name;
030            
031            /**
032             * Initializes the principal with the given name
033             * 
034             * @param name
035             *            The given name
036             */
037            public BasicPrincipal(String name) {
038                    if (name == null) {
039                            throw new NullPointerException("Null name");
040                    }
041                    this.name = name.trim();
042                    if (name.length() == 0) {
043                            throw new IllegalArgumentException("Invalid name");
044                    }
045            }
046    
047            /**
048             * Retrieves the name of the principal
049             */
050            public String getName() {
051                    return name;
052            }
053    
054            @Override
055            public boolean equals(Object obj) {
056                    if (obj == this)
057                            return true;
058                    else
059                            return getClass().isInstance(obj)
060                                            && name.equals(((Principal) obj).getName());
061            }
062    
063            @Override
064            public int hashCode() {
065                    return name.hashCode();
066            }
067    
068            @Override
069            public String toString() {
070                    return getClass().getName() + "\"" + name + "\"";
071            }
072            
073    }