KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > security > GroupSet


1 package org.apache.turbine.util.security;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import org.apache.turbine.om.security.Group;
25
26 /**
27  * This class represents a set of Groups. It's useful for building
28  * administration UI. It enforces that only
29  * Group objects are allowed in the set and only relevant methods
30  * are available.
31  *
32  * @author <a HREF="mailto:john.mcnally@clearink.com">John D. McNally</a>
33  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
34  * @author <a HREF="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
35  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
36  * @version $Id: GroupSet.java,v 1.10.2.2 2004/05/20 03:27:24 seade Exp $
37  */

38 public class GroupSet
39         extends SecuritySet
40 {
41     /**
42      * Constructs an empty GroupSet
43      */

44     public GroupSet()
45     {
46         super();
47     }
48
49     /**
50      * Constructs a new GroupSet with specified contents.
51      *
52      * If the given collection contains multiple objects that are
53      * identical WRT equals() method, some objects will be overwritten.
54      *
55      * @param groups A collection of groups to be contained in the set.
56      */

57     public GroupSet(Collection JavaDoc groups)
58     {
59         super();
60         add(groups);
61     }
62
63     /**
64      * Adds a Group to this GroupSet.
65      *
66      * @param group A Group.
67      * @return True if Group was added; false if GroupSet
68      * already contained the Group.
69      */

70     public boolean add(Group group)
71     {
72         boolean res = contains(group);
73         nameMap.put(group.getName(), group);
74         idMap.put(group.getIdAsObj(), group);
75         return res;
76     }
77
78     /**
79      * Adds the Groups in a Collection to this GroupSet.
80      *
81      * @param groups A Collection of Groups.
82      * @return True if this GroupSet changed as a result; false
83      * if no change to this GroupSet occurred (this GroupSet
84      * already contained all members of the added GroupSet).
85      */

86     public boolean add(Collection JavaDoc groups)
87     {
88         boolean res = false;
89         for (Iterator JavaDoc it = groups.iterator(); it.hasNext();)
90         {
91             Group g = (Group) it.next();
92             res |= add(g);
93         }
94         return res;
95     }
96
97     /**
98      * Adds the Groups in another GroupSet to this GroupSet.
99      *
100      * @param groupSet A GroupSet.
101      * @return True if this GroupSet changed as a result; false
102      * if no change to this GroupSet occurred (this GroupSet
103      * already contained all members of the added GroupSet).
104      */

105     public boolean add(GroupSet groupSet)
106     {
107         boolean res = false;
108         for( Iterator JavaDoc it = groupSet.iterator(); it.hasNext();)
109         {
110             Group g = (Group) it.next();
111             res |= add(g);
112         }
113         return res;
114     }
115
116     /**
117      * Removes a Group from this GroupSet.
118      *
119      * @param group A Group.
120      * @return True if this GroupSet contained the Group
121      * before it was removed.
122      */

123     public boolean remove(Group group)
124     {
125         boolean res = contains(group);
126         nameMap.remove(group.getName());
127         idMap.remove(group.getIdAsObj());
128         return res;
129     }
130
131     /**
132      * Checks whether this GroupSet contains a Group.
133      *
134      * @param group A Group.
135      * @return True if this GroupSet contains the Group,
136      * false otherwise.
137      */

138     public boolean contains(Group group)
139     {
140         return nameMap.containsValue((Object JavaDoc) group);
141     }
142
143     /**
144      * Returns a Group with the given name, if it is contained in
145      * this GroupSet.
146      *
147      * @param groupName Name of Group.
148      * @return Group if argument matched a Group in this
149      * GroupSet; null if no match.
150      * @deprecated Use <a HREF="#getGroupByName">getGroupByName</a> instead.
151      */

152     public Group getGroup(String JavaDoc groupName)
153     {
154         return getGroupByName(groupName);
155     }
156
157     /**
158      * Returns a Group with the given name, if it is contained in
159      * this GroupSet.
160      *
161      * @param groupName Name of Group.
162      * @return Group if argument matched a Group in this
163      * GroupSet; null if no match.
164      */

165     public Group getGroupByName(String JavaDoc groupName)
166     {
167         return (StringUtils.isNotEmpty(groupName))
168                 ? (Group) nameMap.get(groupName) : null;
169     }
170
171     /**
172      * Returns a Group with the given id, if it is contained in
173      * this GroupSet.
174      *
175      * @param groupId Id of the group
176      * @return Group if argument matched a Group in this
177      * GroupSet; null if no match.
178      */

179     public Group getGroupById(int groupId)
180     {
181         return (groupId != 0)
182                 ? (Group) idMap.get(new Integer JavaDoc(groupId)) : null;
183     }
184
185     /**
186      * Returns an Array of Groups in this GroupSet.
187      *
188      * @return An Array of Group objects.
189      */

190     public Group[] getGroupsArray()
191     {
192         return (Group[]) getSet().toArray(new Group[0]);
193     }
194
195     /**
196      * Print out a GroupSet as a String
197      *
198      * @returns The Group Set as String
199      *
200      */

201     public String JavaDoc toString()
202     {
203         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
204         sb.append("GroupSet: ");
205
206         for(Iterator JavaDoc it = iterator(); it.hasNext();)
207         {
208             Group g = (Group) it.next();
209             sb.append('[');
210             sb.append(g.getName());
211             sb.append(" -> ");
212             sb.append(g.getIdAsObj());
213             sb.append(']');
214             if (it.hasNext())
215             {
216                 sb.append(", ");
217             }
218         }
219
220         return sb.toString();
221     }
222 }
223
Popular Tags