001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.core.user;
017
018import org.apache.commons.lang3.StringUtils;
019
020/**
021 * Class containing a user identity, i.e. the login of the user and 
022 * the id of its user population.
023 */
024public class UserIdentity
025{
026    /** The separator between the login and the population id for the string representation of a user identity */
027    private static final String __SEPARATOR = "#"; 
028    
029    /** The login of the user */
030    private String _login;
031    
032    /** The id of the user population the user belongs to */
033    private String _populationId;
034    
035    /**
036     * Constructs a user identity
037     * @param login The login of the user
038     * @param populationId The id of the user population the user belongs to
039     */
040    public UserIdentity(String login, String populationId)
041    {
042        _login = login;
043        _populationId = populationId;
044    }
045    
046    /**
047     * Gets a string representation of a {@link UserIdentity}
048     * @param userIdentity The user identity
049     * @return The string representation of the user identity.
050     */
051    public static String userIdentityToString(UserIdentity userIdentity)
052    {
053        if (userIdentity != null)
054        {
055            return userIdentity.getLogin() + __SEPARATOR + userIdentity.getPopulationId();
056        }
057        else
058        {
059            return null;
060        }
061    }
062    
063    /**
064     * Returns the {@link UserIdentity} from its string representation
065     * @param string The string representation of the user identity
066     * @return The user identity from its string representation
067     */
068    public static UserIdentity stringToUserIdentity(String string)
069    {
070        if (string != null && string.contains(__SEPARATOR))
071        {
072            String login = StringUtils.substringBeforeLast(string, __SEPARATOR);
073            String populationId = StringUtils.substringAfterLast(string, __SEPARATOR);
074            return new UserIdentity(login, populationId);
075        }
076        else
077        {
078            return null;
079        }
080    }
081    
082
083    /**
084     * Get the login of the user
085     * @return The login of the user
086     */
087    public String getLogin()
088    {
089        return _login;
090    }
091
092    /**
093     * GetGet the user population the user belongs to
094     * @return The id of the user population the user belongs to
095     */
096    public String getPopulationId()
097    {
098        return _populationId;
099    }
100
101    @Override
102    public int hashCode()
103    {
104        final int prime = 31;
105        int result = 1;
106        result = prime * result + ((_login == null) ? 0 : _login.hashCode());
107        result = prime * result + ((_populationId == null) ? 0 : _populationId.hashCode());
108        return result;
109    }
110    
111    @Override
112    public boolean equals(Object obj)
113    {
114        if (this == obj)
115        {
116            return true;
117        }
118        if (obj == null)
119        {
120            return false;
121        }
122        if (!(obj instanceof UserIdentity))
123        {
124            return false;
125        }
126        
127        UserIdentity other = (UserIdentity) obj;
128        if (_login == null)
129        {
130            if (other._login != null)
131            {
132                return false;
133            }
134        }
135        else if (!_login.equals(other._login))
136        {
137            return false;
138        }
139        if (_populationId == null)
140        {
141            if (other._populationId != null)
142            {
143                return false;
144            }
145        }
146        else if (!_populationId.equals(other._populationId))
147        {
148            return false;
149        }
150        
151        return true;
152    }
153
154    @Override
155    public String toString()
156    {
157        return "UserIdentity [login=" + _login + ", population=" + _populationId + "]";
158    }
159}