1 package org.apache.turbine.util.security;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23
24 import java.util.Collection;
25 import java.util.Iterator;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.turbine.om.security.Group;
29
30 /**
31 * This class represents a set of Groups. It's useful for building
32 * administration UI. It enforces that only
33 * Group objects are allowed in the set and only relevant methods
34 * are available.
35 *
36 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
37 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
38 * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @version $Id: GroupSet.java 1073174 2011-02-21 22:18:45Z tv $
41 */
42 public class GroupSet
43 extends SecuritySet<Group>
44 {
45 /** Serial version */
46 private static final long serialVersionUID = -80412911083063489L;
47
48 /**
49 * Constructs an empty GroupSet
50 */
51 public GroupSet()
52 {
53 super();
54 }
55
56 /**
57 * Constructs a new GroupSet with specified contents.
58 *
59 * If the given collection contains multiple objects that are
60 * identical WRT equals() method, some objects will be overwritten.
61 *
62 * @param groups A collection of groups to be contained in the set.
63 */
64 public GroupSet(Collection<Group> groups)
65 {
66 super();
67 add(groups);
68 }
69
70 /**
71 * Adds a Group to this GroupSet.
72 *
73 * @param group A Group.
74 * @return True if Group was added; false if GroupSet
75 * already contained the Group.
76 */
77 public boolean add(Group group)
78 {
79 boolean res = contains(group);
80 nameMap.put(group.getName(), group);
81 idMap.put(group.getIdAsObj(), group);
82 return res;
83 }
84
85 /**
86 * Adds the Groups in a Collection to this GroupSet.
87 *
88 * @param groups A Collection of Groups.
89 * @return True if this GroupSet changed as a result; false
90 * if no change to this GroupSet occurred (this GroupSet
91 * already contained all members of the added GroupSet).
92 */
93 public boolean add(Collection<Group> groups)
94 {
95 boolean res = false;
96 for (Group g : groups)
97 {
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(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 }