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.Role; 62 63 /*** 64 * This class represents a set of Roles. It makes it easy to build a 65 * UI that would allow someone to add a group of Roles to a User. It 66 * wraps a TreeSet object to enforce that only Role objects are 67 * allowed in the set and only relevant methods are available. 68 * TreeSet's contain only unique Objects (no 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: RoleSet.java,v 1.2 2002/07/11 16:53:20 mpoeschl Exp $ 73 */ 74 public class RoleSet implements Serializable 75 { 76 /*** 77 * Role storage. 78 */ 79 private TreeSet set; 80 81 /*** 82 * Constructs an empty RoleSet 83 */ 84 public RoleSet() 85 { 86 set = new TreeSet(); 87 } 88 89 /*** 90 * Constructs a new RoleSet with specifed contents. 91 * 92 * If the given collection contains multiple objects that are 93 * identical WRT equals() method, some objects will be overwriten. 94 * 95 * @param roles A collection of roles to be contained in the set. 96 */ 97 public RoleSet(Collection roles) 98 { 99 this(); 100 add(roles); 101 } 102 103 /*** 104 * Adds a Role to this RoleSet. 105 * 106 * @param role A Role. 107 * @return True if Role was added; false if RoleSet already 108 * contained the Role. 109 */ 110 public boolean add(Role role) 111 { 112 return set.add((Object) role); 113 } 114 115 /*** 116 * Adds the Roles in a Collection to this RoleSet. 117 * 118 * @param roles A Collection of Roles. 119 * @return True if this RoleSet changed as a result; false 120 * if no change to this RoleSet occurred (this RoleSet 121 * already contained all members of the added RoleSet). 122 */ 123 public boolean add(Collection roles) 124 { 125 return set.addAll(roles); 126 } 127 128 /*** 129 * Adds the Roles in another RoleSet to this RoleSet. 130 * 131 * @param roleSet A RoleSet. 132 * @return True if this RoleSet changed as a result; false if no 133 * change to this RoleSet occurred (this RoleSet already contained 134 * all members of the added RoleSet). 135 */ 136 public boolean add(RoleSet roleSet) 137 { 138 return set.addAll(roleSet.set); 139 } 140 141 /*** 142 * Removes a Role from this RoleSet. 143 * 144 * @param role A Role. 145 * @return True if this RoleSet contained the Role before it was 146 * removed. 147 */ 148 public boolean remove(Role role) 149 { 150 return set.remove((Object) role); 151 } 152 153 /*** 154 * Removes all Roles from this RoleSet. 155 */ 156 public void clear() 157 { 158 set.clear(); 159 } 160 161 /*** 162 * Checks whether this RoleSet contains a Role. 163 * 164 * @param role A Role. 165 * @return True if this RoleSet contains the Role, false 166 * otherwise. 167 */ 168 public boolean contains(Role role) 169 { 170 return set.contains((Object) role); 171 } 172 173 /*** 174 * Compares by name a Role with the Roles contained in this 175 * RoleSet. 176 * 177 * @param roleName Name of Role. 178 * @return True if argument matched a Role in this RoleSet; false 179 * if no match. 180 */ 181 public boolean contains(String roleName) 182 { 183 Iterator iter = set.iterator(); 184 while (iter.hasNext()) 185 { 186 Role role = (Role)iter.next(); 187 if (roleName != null && roleName.equals(role.getName())) 188 { 189 return true; 190 } 191 } 192 return false; 193 } 194 195 /*** 196 * Returns a Role with the given name, if it is contained in this 197 * RoleSet. 198 * 199 * @param roleName Name of Role. 200 * @return Role if argument matched a Role in this RoleSet; null 201 * if no match. 202 */ 203 public Role getRole(String roleName) 204 { 205 Iterator iter = set.iterator(); 206 while (iter.hasNext()) 207 { 208 Role role = (Role)iter.next(); 209 if (roleName != null && roleName.equals(role.getName())) 210 { 211 return role; 212 } 213 } 214 return null; 215 } 216 217 /*** 218 * Returns an Roles[] of Roles in this RoleSet. 219 * 220 * @return A Role[]. 221 */ 222 public Role[] getRolesArray() 223 { 224 return (Role[])set.toArray(new Role[0]); 225 } 226 227 /*** 228 * Returns an Iterator for Roles in this RoleSet. 229 */ 230 public Iterator elements() 231 { 232 return set.iterator(); 233 } 234 235 /*** 236 * Returns size (cardinality) of this set. 237 * 238 * @return The cardinality of this RoleSet. 239 */ 240 public int size() 241 { 242 return set.size(); 243 } 244 }

This page was automatically generated by Maven