001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth.login;
020
021import java.security.Principal;
022
023/**
024 * Wrapper class for container-managed or externally-provided principals.
025 * Instances of PrincipalWrapper are immutable.
026 * @since 2.3
027 */
028public final class PrincipalWrapper implements Principal
029{
030    private final Principal m_principal;
031    
032    /**
033     * Constructs a new instance of this class by wrapping (decorating)
034     * the supplied principal.
035     * @param principal The principal to wrap
036     */
037    public PrincipalWrapper(final Principal principal )
038    {
039        m_principal = principal;
040    }
041
042    /**
043     * Returns the wrapped Principal used to construct this instance.
044     * @return the wrapped Principal decorated by this instance.
045     */
046    public Principal getPrincipal()
047    {
048        return m_principal;
049    }
050    
051    /**
052     * Returns the name of the wrapped principal.
053     * 
054     * @return The name of the wrapped principal.
055     */
056    @Override
057    public String getName()
058    {
059        return m_principal.getName();
060    }
061
062    /**
063     *  Two PrincipalWrapper objects are equal if their internally-wrapped
064     *  principals are also equal.
065     *  
066     *  @param obj {@inheritDoc}
067     *  @return True, if the wrapped object is also equal to our wrapped object.
068     */
069    @Override
070    public boolean equals(final Object obj )
071    {
072        if ( ! ( obj instanceof PrincipalWrapper ) )
073        {
074            return false;
075        }
076        return m_principal.equals( ( (PrincipalWrapper)obj ).getPrincipal() );
077    }
078
079    /**
080     *  The hashcode is based on the hashcode of the wrapped principal.
081     *  
082     *  @return A hashcode.
083     */
084    @Override
085    public int hashCode()
086    {
087        return m_principal.hashCode() * 13;
088    }
089
090}