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