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[] fields = StringUtils.split(string, __SEPARATOR);
064        String id = fields[0];
065        String groupDirectoryId = fields[1];
066        return new GroupIdentity(id, groupDirectoryId);
067    }
068    
069
070    /**
071     * Get the if of the group
072     * @return The id of the group
073     */
074    public String getId()
075    {
076        return _id;
077    }
078
079    /**
080     * GetGet the group directory the group belongs to
081     * @return The id of the group directory the group belongs to
082     */
083    public String getDirectoryId()
084    {
085        return _directoryId;
086    }
087
088    @Override
089    public int hashCode()
090    {
091        final int prime = 31;
092        int result = 1;
093        result = prime * result + ((_directoryId == null) ? 0 : _directoryId.hashCode());
094        result = prime * result + ((_id == null) ? 0 : _id.hashCode());
095        return result;
096    }
097
098    @Override
099    public boolean equals(Object obj)
100    {
101        if (this == obj)
102        {
103            return true;
104        }
105        if (obj == null)
106        {
107            return false;
108        }
109        if (getClass() != obj.getClass())
110        {
111            return false;
112        }
113        
114        GroupIdentity other = (GroupIdentity) obj;
115        if (_directoryId == null)
116        {
117            if (other._directoryId != null)
118            {
119                return false;
120            }
121        }
122        else if (!_directoryId.equals(other._directoryId))
123        {
124            return false;
125        }
126        if (_id == null)
127        {
128            if (other._id != null)
129            {
130                return false;
131            }
132        }
133        else if (!_id.equals(other._id))
134        {
135            return false;
136        }
137        
138        return true;
139    }
140
141    @Override
142    public String toString()
143    {
144        return "Group [id=" + _id + ", directory=" + _directoryId + "]";
145    }
146
147}