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