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[] fields = StringUtils.split(string, __SEPARATOR); 073 String login = fields[0]; 074 String populationId = fields[1]; 075 return new UserIdentity(login, populationId); 076 } 077 else 078 { 079 return null; 080 } 081 } 082 083 084 /** 085 * Get the login of the user 086 * @return The login of the user 087 */ 088 public String getLogin() 089 { 090 return _login; 091 } 092 093 /** 094 * GetGet the user population the user belongs to 095 * @return The id of the user population the user belongs to 096 */ 097 public String getPopulationId() 098 { 099 return _populationId; 100 } 101 102 @Override 103 public int hashCode() 104 { 105 final int prime = 31; 106 int result = 1; 107 result = prime * result + ((_login == null) ? 0 : _login.hashCode()); 108 result = prime * result + ((_populationId == null) ? 0 : _populationId.hashCode()); 109 return result; 110 } 111 112 @Override 113 public boolean equals(Object obj) 114 { 115 if (this == obj) 116 { 117 return true; 118 } 119 if (obj == null) 120 { 121 return false; 122 } 123 if (!(obj instanceof UserIdentity)) 124 { 125 return false; 126 } 127 128 UserIdentity other = (UserIdentity) obj; 129 if (_login == null) 130 { 131 if (other._login != null) 132 { 133 return false; 134 } 135 } 136 else if (!_login.equals(other._login)) 137 { 138 return false; 139 } 140 if (_populationId == null) 141 { 142 if (other._populationId != null) 143 { 144 return false; 145 } 146 } 147 else if (!_populationId.equals(other._populationId)) 148 { 149 return false; 150 } 151 152 return true; 153 } 154 155 @Override 156 public String toString() 157 { 158 return "UserIdentity [login=" + _login + ", population=" + _populationId + "]"; 159 } 160}