001    package org.apache.turbine.util.security;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.util.Collection;
025    import java.util.Iterator;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.apache.turbine.om.security.Group;
029    
030    /**
031     * This class represents a set of Groups. It's useful for building
032     * administration UI.  It enforces that only
033     * Group objects are allowed in the set and only relevant methods
034     * are available.
035     *
036     * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
037     * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
038     * @author <a href="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
039     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040     * @version $Id: GroupSet.java 1073174 2011-02-21 22:18:45Z tv $
041     */
042    public class GroupSet
043            extends SecuritySet<Group>
044    {
045        /** Serial version */
046            private static final long serialVersionUID = -80412911083063489L;
047    
048            /**
049         * Constructs an empty GroupSet
050         */
051        public GroupSet()
052        {
053            super();
054        }
055    
056        /**
057         * Constructs a new GroupSet with specified contents.
058         *
059         * If the given collection contains multiple objects that are
060         * identical WRT equals() method, some objects will be overwritten.
061         *
062         * @param groups A collection of groups to be contained in the set.
063         */
064        public GroupSet(Collection<Group> groups)
065        {
066            super();
067            add(groups);
068        }
069    
070        /**
071         * Adds a Group to this GroupSet.
072         *
073         * @param group A Group.
074         * @return True if Group was added; false if GroupSet
075         * already contained the Group.
076         */
077        public boolean add(Group group)
078        {
079            boolean res = contains(group);
080            nameMap.put(group.getName(), group);
081            idMap.put(group.getIdAsObj(), group);
082            return res;
083        }
084    
085        /**
086         * Adds the Groups in a Collection to this GroupSet.
087         *
088         * @param groups A Collection of Groups.
089         * @return True if this GroupSet changed as a result; false
090         * if no change to this GroupSet occurred (this GroupSet
091         * already contained all members of the added GroupSet).
092         */
093        public boolean add(Collection<Group> groups)
094        {
095            boolean res = false;
096            for (Group g : groups)
097            {
098                res |= add(g);
099            }
100            return res;
101        }
102    
103        /**
104         * Adds the Groups in another GroupSet to this GroupSet.
105         *
106         * @param groupSet A GroupSet.
107         * @return True if this GroupSet changed as a result; false
108         * if no change to this GroupSet occurred (this GroupSet
109         * already contained all members of the added GroupSet).
110         */
111        public boolean add(GroupSet groupSet)
112        {
113            boolean res = false;
114            for(Group g : groupSet)
115            {
116                res |= add(g);
117            }
118            return res;
119        }
120    
121        /**
122         * Removes a Group from this GroupSet.
123         *
124         * @param group A Group.
125         * @return True if this GroupSet contained the Group
126         * before it was removed.
127         */
128        public boolean remove(Group group)
129        {
130            boolean res = contains(group);
131            nameMap.remove(group.getName());
132            idMap.remove(group.getIdAsObj());
133            return res;
134        }
135    
136        /**
137         * Checks whether this GroupSet contains a Group.
138         *
139         * @param group A Group.
140         * @return True if this GroupSet contains the Group,
141         * false otherwise.
142         */
143        public boolean contains(Group group)
144        {
145            return nameMap.containsValue(group);
146        }
147    
148        /**
149         * Returns a Group with the given name, if it is contained in
150         * this GroupSet.
151         *
152         * @param groupName Name of Group.
153         * @return Group if argument matched a Group in this
154         * GroupSet; null if no match.
155         */
156        public Group getGroupByName(String groupName)
157        {
158            return (StringUtils.isNotEmpty(groupName))
159                    ? (Group) nameMap.get(groupName) : null;
160        }
161    
162        /**
163         * Returns a Group with the given id, if it is contained in
164         * this GroupSet.
165         *
166         * @param groupId Id of the group
167         * @return Group if argument matched a Group in this
168         * GroupSet; null if no match.
169         */
170        public Group getGroupById(int groupId)
171        {
172            return (groupId != 0)
173                    ? (Group) idMap.get(new Integer(groupId)) : null;
174        }
175    
176        /**
177         * Returns an Array of Groups in this GroupSet.
178         *
179         * @return An Array of Group objects.
180         */
181        public Group[] getGroupsArray()
182        {
183            return getSet().toArray(new Group[0]);
184        }
185    
186        /**
187         * Print out a GroupSet as a String
188         *
189         * @returns The Group Set as String
190         *
191         */
192        @Override
193        public String toString()
194        {
195            StringBuffer sb = new StringBuffer();
196            sb.append("GroupSet: ");
197    
198            for(Iterator<Group> it = iterator(); it.hasNext();)
199            {
200                Group g = it.next();
201                sb.append('[');
202                sb.append(g.getName());
203                sb.append(" -> ");
204                sb.append(g.getIdAsObj());
205                sb.append(']');
206                if (it.hasNext())
207                {
208                    sb.append(", ");
209                }
210            }
211    
212            return sb.toString();
213        }
214    }