1 package org.apache.turbine.util.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collection;
23 import java.util.Iterator;
24
25 import org.apache.commons.lang.StringUtils;
26
27 import org.apache.turbine.om.security.Group;
28
29 /***
30 * This class represents a set of Groups. It's useful for building
31 * administration UI. It enforces that only
32 * Group objects are allowed in the set and only relevant methods
33 * are available.
34 *
35 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
36 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
37 * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
38 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39 * @version $Id: GroupSet.java 534527 2007-05-02 16:10:59Z tv $
40 */
41 public class GroupSet
42 extends SecuritySet
43 {
44 /*** Serial Version UID */
45 private static final long serialVersionUID = 34735375738993720L;
46
47 /***
48 * Constructs an empty GroupSet
49 */
50 public GroupSet()
51 {
52 super();
53 }
54
55 /***
56 * Constructs a new GroupSet with specified contents.
57 *
58 * If the given collection contains multiple objects that are
59 * identical WRT equals() method, some objects will be overwritten.
60 *
61 * @param groups A collection of groups to be contained in the set.
62 */
63 public GroupSet(Collection groups)
64 {
65 super();
66 add(groups);
67 }
68
69 /***
70 * Adds a Group to this GroupSet.
71 *
72 * @param group A Group.
73 * @return True if Group was added; false if GroupSet
74 * already contained the Group.
75 */
76 public boolean add(Group group)
77 {
78 boolean res = contains(group);
79 nameMap.put(group.getName(), group);
80 idMap.put(group.getIdAsObj(), group);
81 return res;
82 }
83
84 /***
85 * Adds the Groups in a Collection to this GroupSet.
86 *
87 * @param groups A Collection of Groups.
88 * @return True if this GroupSet changed as a result; false
89 * if no change to this GroupSet occurred (this GroupSet
90 * already contained all members of the added GroupSet).
91 */
92 public boolean add(Collection groups)
93 {
94 boolean res = false;
95 for (Iterator it = groups.iterator(); it.hasNext();)
96 {
97 Group g = (Group) it.next();
98 res |= add(g);
99 }
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( Iterator it = groupSet.iterator(); it.hasNext();)
115 {
116 Group g = (Group) it.next();
117 res |= add(g);
118 }
119 return res;
120 }
121
122 /***
123 * Removes a Group from this GroupSet.
124 *
125 * @param group A Group.
126 * @return True if this GroupSet contained the Group
127 * before it was removed.
128 */
129 public boolean remove(Group group)
130 {
131 boolean res = contains(group);
132 nameMap.remove(group.getName());
133 idMap.remove(group.getIdAsObj());
134 return res;
135 }
136
137 /***
138 * Checks whether this GroupSet contains a Group.
139 *
140 * @param group A Group.
141 * @return True if this GroupSet contains the Group,
142 * false otherwise.
143 */
144 public boolean contains(Group group)
145 {
146 return nameMap.containsValue((Object) group);
147 }
148
149 /***
150 * Returns a Group with the given name, if it is contained in
151 * this GroupSet.
152 *
153 * @param groupName Name of Group.
154 * @return Group if argument matched a Group in this
155 * GroupSet; null if no match.
156 * @deprecated Use <a href="#getGroupByName">getGroupByName</a> instead.
157 */
158 public Group getGroup(String groupName)
159 {
160 return getGroupByName(groupName);
161 }
162
163 /***
164 * Returns a Group with the given name, if it is contained in
165 * this GroupSet.
166 *
167 * @param groupName Name of Group.
168 * @return Group if argument matched a Group in this
169 * GroupSet; null if no match.
170 */
171 public Group getGroupByName(String groupName)
172 {
173 return (StringUtils.isNotEmpty(groupName))
174 ? (Group) nameMap.get(groupName) : null;
175 }
176
177 /***
178 * Returns a Group with the given id, if it is contained in
179 * this GroupSet.
180 *
181 * @param groupId Id of the group
182 * @return Group if argument matched a Group in this
183 * GroupSet; null if no match.
184 */
185 public Group getGroupById(int groupId)
186 {
187 return (groupId != 0)
188 ? (Group) idMap.get(new Integer(groupId)) : null;
189 }
190
191 /***
192 * Returns an Array of Groups in this GroupSet.
193 *
194 * @return An Array of Group objects.
195 */
196 public Group[] getGroupsArray()
197 {
198 return (Group[]) getSet().toArray(new Group[0]);
199 }
200
201 /***
202 * Print out a GroupSet as a String
203 *
204 * @returns The Group Set as String
205 *
206 */
207 public String toString()
208 {
209 StringBuffer sb = new StringBuffer();
210 sb.append("GroupSet: ");
211
212 for(Iterator it = iterator(); it.hasNext();)
213 {
214 Group g = (Group) it.next();
215 sb.append('[');
216 sb.append(g.getName());
217 sb.append(" -> ");
218 sb.append(g.getIdAsObj());
219 sb.append(']');
220 if (it.hasNext())
221 {
222 sb.append(", ");
223 }
224 }
225
226 return sb.toString();
227 }
228 }