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 */
019 package org.apache.wiki.auth.login;
020
021 import 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 */
028 public 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( 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 public String getName()
057 {
058 return m_principal.getName();
059 }
060
061 /**
062 * Two PrincipalWrapper objects are equal if their internally-wrapped
063 * principals are also equal.
064 *
065 * @param obj {@inheritDoc}
066 * @return True, if the wrapped object is also equal to our wrapped object.
067 */
068 @Override
069 public boolean equals( Object obj )
070 {
071 if ( ! ( obj instanceof PrincipalWrapper ) )
072 {
073 return false;
074 }
075 return m_principal.equals( ( (PrincipalWrapper)obj ).getPrincipal() );
076 }
077
078 /**
079 * The hashcode is based on the hashcode of the wrapped principal.
080 *
081 * @return A hashcode.
082 */
083 @Override
084 public int hashCode()
085 {
086 return m_principal.hashCode() * 13;
087 }
088
089 }