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 import java.util.Collection;
20 import java.util.Iterator;
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üttel</a>
35 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
36 * @version $Id: GroupSet.java 278822 2005-09-05 19:53:05Z henning $
37 */
38 public class GroupSet
39 extends SecuritySet
40 {
41 /*** Serial Version UID */
42 private static final long serialVersionUID = 34735375738993720L;
43
44 /***
45 * Constructs an empty GroupSet
46 */
47 public GroupSet()
48 {
49 super();
50 }
51
52 /***
53 * Constructs a new GroupSet with specified contents.
54 *
55 * If the given collection contains multiple objects that are
56 * identical WRT equals() method, some objects will be overwritten.
57 *
58 * @param groups A collection of groups to be contained in the set.
59 */
60 public GroupSet(Collection groups)
61 {
62 super();
63 add(groups);
64 }
65
66 /***
67 * Adds a Group to this GroupSet.
68 *
69 * @param group A Group.
70 * @return True if Group was added; false if GroupSet
71 * already contained the Group.
72 */
73 public boolean add(Group group)
74 {
75 boolean res = contains(group);
76 nameMap.put(group.getName(), group);
77 idMap.put(group.getIdAsObj(), group);
78 return res;
79 }
80
81 /***
82 * Adds the Groups in a Collection to this GroupSet.
83 *
84 * @param groups A Collection of Groups.
85 * @return True if this GroupSet changed as a result; false
86 * if no change to this GroupSet occurred (this GroupSet
87 * already contained all members of the added GroupSet).
88 */
89 public boolean add(Collection groups)
90 {
91 boolean res = false;
92 for (Iterator it = groups.iterator(); it.hasNext();)
93 {
94 Group g = (Group) it.next();
95 res |= add(g);
96 }
97 return res;
98 }
99
100 /***
101 * Adds the Groups in another GroupSet to this GroupSet.
102 *
103 * @param groupSet A GroupSet.
104 * @return True if this GroupSet changed as a result; false
105 * if no change to this GroupSet occurred (this GroupSet
106 * already contained all members of the added GroupSet).
107 */
108 public boolean add(GroupSet groupSet)
109 {
110 boolean res = false;
111 for( Iterator it = groupSet.iterator(); it.hasNext();)
112 {
113 Group g = (Group) it.next();
114 res |= add(g);
115 }
116 return res;
117 }
118
119 /***
120 * Removes a Group from this GroupSet.
121 *
122 * @param group A Group.
123 * @return True if this GroupSet contained the Group
124 * before it was removed.
125 */
126 public boolean remove(Group group)
127 {
128 boolean res = contains(group);
129 nameMap.remove(group.getName());
130 idMap.remove(group.getIdAsObj());
131 return res;
132 }
133
134 /***
135 * Checks whether this GroupSet contains a Group.
136 *
137 * @param group A Group.
138 * @return True if this GroupSet contains the Group,
139 * false otherwise.
140 */
141 public boolean contains(Group group)
142 {
143 return nameMap.containsValue((Object) group);
144 }
145
146 /***
147 * Returns a Group with the given name, if it is contained in
148 * this GroupSet.
149 *
150 * @param groupName Name of Group.
151 * @return Group if argument matched a Group in this
152 * GroupSet; null if no match.
153 * @deprecated Use <a href="#getGroupByName">getGroupByName</a> instead.
154 */
155 public Group getGroup(String groupName)
156 {
157 return getGroupByName(groupName);
158 }
159
160 /***
161 * Returns a Group with the given name, if it is contained in
162 * this GroupSet.
163 *
164 * @param groupName Name of Group.
165 * @return Group if argument matched a Group in this
166 * GroupSet; null if no match.
167 */
168 public Group getGroupByName(String groupName)
169 {
170 return (StringUtils.isNotEmpty(groupName))
171 ? (Group) nameMap.get(groupName) : null;
172 }
173
174 /***
175 * Returns a Group with the given id, if it is contained in
176 * this GroupSet.
177 *
178 * @param groupId Id of the group
179 * @return Group if argument matched a Group in this
180 * GroupSet; null if no match.
181 */
182 public Group getGroupById(int groupId)
183 {
184 return (groupId != 0)
185 ? (Group) idMap.get(new Integer(groupId)) : null;
186 }
187
188 /***
189 * Returns an Array of Groups in this GroupSet.
190 *
191 * @return An Array of Group objects.
192 */
193 public Group[] getGroupsArray()
194 {
195 return (Group[]) getSet().toArray(new Group[0]);
196 }
197
198 /***
199 * Print out a GroupSet as a String
200 *
201 * @returns The Group Set as String
202 *
203 */
204 public String toString()
205 {
206 StringBuffer sb = new StringBuffer();
207 sb.append("GroupSet: ");
208
209 for(Iterator it = iterator(); it.hasNext();)
210 {
211 Group g = (Group) it.next();
212 sb.append('[');
213 sb.append(g.getName());
214 sb.append(" -> ");
215 sb.append(g.getIdAsObj());
216 sb.append(']');
217 if (it.hasNext())
218 {
219 sb.append(", ");
220 }
221 }
222
223 return sb.toString();
224 }
225 }