View Javadoc
1 package org.apache.turbine.util.security; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.Serializable; 58 import java.util.Collection; 59 import java.util.Iterator; 60 import java.util.TreeSet; 61 import org.apache.turbine.om.security.Group; 62 63 /*** 64 * This class represents a set of Groups. It's useful for building 65 * administration UI. It wraps a TreeSet object to enforce that only 66 * Group objects are allowed in the set and only relevant methods 67 * are available. TreeSet's contain only unique Objects (no 68 * duplicates). 69 * 70 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 71 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 72 * @version $Id: GroupSet.java,v 1.3 2002/07/16 12:15:30 henning Exp $ 73 */ 74 public class GroupSet implements Serializable 75 { 76 /*** Set to hold the Group Set */ 77 private TreeSet set; 78 79 /*** 80 * Constructs an empty GroupSet 81 */ 82 public GroupSet() 83 { 84 set = new TreeSet(); 85 } 86 87 /*** 88 * Constructs a new GroupSet with specifed contents. 89 * 90 * If the given collection contains multiple objects that are 91 * identical WRT equals() method, some objects will be overwriten. 92 * 93 * @param groups A collection of groups to be contained in the set. 94 */ 95 public GroupSet(Collection groups) 96 { 97 this(); 98 add(groups); 99 } 100 101 /*** 102 * Adds a Group to this GroupSet. 103 * 104 * @param group A Group. 105 * @return True if Group was added; false if GroupSet 106 * already contained the Group. 107 */ 108 public boolean add(Group group) 109 { 110 return set.add((Object) group); 111 } 112 113 /*** 114 * Adds the Groups in a Collection to this GroupSet. 115 * 116 * @param groups A Collection of Groups. 117 * @return True if this GroupSet changed as a result; false 118 * if no change to this GroupSet occurred (this GroupSet 119 * already contained all members of the added GroupSet). 120 */ 121 public boolean add(Collection groups) 122 { 123 return set.addAll(groups); 124 } 125 126 /*** 127 * Adds the Groups in another GroupSet to this GroupSet. 128 * 129 * @param groupSet A GroupSet. 130 * @return True if this GroupSet changed as a result; false 131 * if no change to this GroupSet occurred (this GroupSet 132 * already contained all members of the added GroupSet). 133 */ 134 public boolean add(GroupSet groupSet) 135 { 136 return set.addAll(groupSet.set); 137 } 138 139 /*** 140 * Removes a Group from this GroupSet. 141 * 142 * @param group A Group. 143 * @return True if this GroupSet contained the Group 144 * before it was removed. 145 */ 146 public boolean remove(Group group) 147 { 148 return set.remove((Object) group); 149 } 150 151 /*** 152 * Removes all Groups from this GroupSet. 153 */ 154 public void clear() 155 { 156 set.clear(); 157 } 158 159 /*** 160 * Checks whether this GroupSet contains a Group. 161 * 162 * @param group A Group. 163 * @return True if this GroupSet contains the Group, 164 * false otherwise. 165 */ 166 public boolean contains(Group group) 167 { 168 return set.contains((Object) group); 169 } 170 171 /*** 172 * Compares by name a Group with the Groups contained in 173 * this GroupSet. 174 * 175 * @param groupName Name of Group. 176 * @return True if argument matched a Group in this 177 * GroupSet; false if no match. 178 */ 179 public boolean contains(String groupName) 180 { 181 Iterator iter = set.iterator(); 182 while (iter.hasNext()) 183 { 184 Group group = (Group) iter.next(); 185 if (groupName != null && groupName.equals(group.getName())) 186 { 187 return true; 188 } 189 } 190 return false; 191 } 192 193 /*** 194 * Returns a Group with the given name, if it is contained in 195 * this GroupSet. 196 * 197 * @param groupName Name of Group. 198 * @return Group if argument matched a Group in this 199 * GroupSet; null if no match. 200 */ 201 public Group getGroup(String groupName) 202 { 203 Iterator iter = set.iterator(); 204 while (iter.hasNext()) 205 { 206 Group group = (Group) iter.next(); 207 if ( groupName != null && groupName.equals(group.getName())) 208 { 209 return group; 210 } 211 } 212 return null; 213 } 214 215 /*** 216 * Returns an Groups[] of Groups in this GroupSet. 217 * 218 * @return A Group[]. 219 */ 220 public Group[] getGroupsArray() 221 { 222 return (Group[]) set.toArray(new Group[0]); 223 } 224 225 /*** 226 * Returns an Iterator for Groups in this GroupSet. 227 */ 228 public Iterator elements() 229 { 230 return set.iterator(); 231 } 232 233 /*** 234 * Returns size (cardinality) of this set. 235 * 236 * @return The cardinality of this GroupSet. 237 */ 238 public int size() 239 { 240 return set.size(); 241 } 242 }

This page was automatically generated by Maven