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 java.util.Objects; 019 020import org.apache.commons.lang3.StringUtils; 021 022/** 023 * Class containing a group identity, i.e. the id of the group and the id of its group directory. 024 */ 025public class GroupIdentity 026{ 027 /** The separator between the id and the group directory id for the string representation of a group identity */ 028 private static final String __SEPARATOR = "#"; 029 030 /** The id of the group */ 031 private String _id; 032 033 /** The id of the group directory the group belongs to */ 034 private String _directoryId; 035 036 /** 037 * Constructs a group identity 038 * @param id The id of the group 039 * @param directoryId The id of the group directory the group belongs to 040 */ 041 public GroupIdentity(String id, String directoryId) 042 { 043 _id = id; 044 _directoryId = directoryId; 045 } 046 047 /** 048 * Gets a string representation of a {@link GroupIdentity} 049 * @param groupIdentity The group identity 050 * @return The string representation of the group identity. 051 */ 052 public static String groupIdentityToString(GroupIdentity groupIdentity) 053 { 054 return groupIdentity.getId() + __SEPARATOR + groupIdentity.getDirectoryId(); 055 } 056 057 /** 058 * Returns the {@link GroupIdentity} from its string representation 059 * @param string The string representation of the group identity 060 * @return The group identity from its string representation 061 */ 062 public static GroupIdentity stringToGroupIdentity(String string) 063 { 064 String id = StringUtils.substringBeforeLast(string, __SEPARATOR); 065 String groupDirectoryId = StringUtils.substringAfterLast(string, __SEPARATOR); 066 return new GroupIdentity(id, groupDirectoryId); 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 return Objects.hash(_id, _directoryId); 091 } 092 093 @Override 094 public boolean equals(Object obj) 095 { 096 if (this == obj) 097 { 098 return true; 099 } 100 101 return obj instanceof GroupIdentity groupIdentity && Objects.equals(_id, groupIdentity._id) 102 && Objects.equals(_directoryId, groupIdentity._directoryId); 103 } 104 105 @Override 106 public String toString() 107 { 108 return "Group [id=" + _id + ", directory=" + _directoryId + "]"; 109 } 110}