001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.login;
021
022import java.security.*;
023
024/**
025 * This class implements the principal interface.
026 *
027 * @author Satish Dharmaraj
028 */
029public class PrincipalImpl implements Principal {
030
031    private String user;
032
033    /**
034     * Construct a principal from a string user name.
035     *
036     * @param user The string form of the principal name.
037     */
038    public PrincipalImpl(String user) {
039        this.user = user;
040    }
041
042    /**
043     * Returns true if the object passed matches the principal represented in this implementation.
044     *
045     * @param another the Principal to compare with.
046     * @return true if the Principal passed is the same as that encapsulated in this object, false otherwise
047     */
048    @Override
049    public boolean equals(Object another) {
050        if (another instanceof PrincipalImpl) {
051            PrincipalImpl p = (PrincipalImpl) another;
052            return user.equals(p.toString());
053        } else {
054            return false;
055        }
056    }
057
058    /**
059     * Prints a stringified version of the principal.
060     */
061    @Override
062    public String toString() {
063        return user;
064    }
065
066    /**
067     * return a hashcode for the principal.
068     */
069    @Override
070    public int hashCode() {
071        return user.hashCode();
072    }
073
074    /**
075     * return the name of the principal.
076     */
077    public String getName() {
078        return user;
079    }
080
081}