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