1 package org.apache.fulcrum.security.util; 2 3 56 57 import java.io.Serializable ; 58 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 import java.util.TreeSet ; 62 63 import org.apache.fulcrum.security.entity.Permission; 64 import org.apache.fulcrum.security.entity.SecurityEntity; 65 66 78 public class PermissionSet implements Serializable 79 { 80 81 private TreeSet set; 82 83 86 public PermissionSet() 87 { 88 set = new TreeSet (); 89 } 90 91 99 public PermissionSet(Collection permissions) 100 { 101 this(); 102 add(permissions); 103 } 104 105 112 public boolean add(Permission permission) 113 { 114 return set.add((Object ) permission); 115 } 116 117 126 public boolean add(Collection permissions) 127 { 128 return set.addAll(permissions); 129 } 130 131 141 public boolean add(PermissionSet permissionSet) 142 { 143 return set.addAll(permissionSet.set); 144 } 145 146 153 public boolean remove(Permission permission) 154 { 155 return set.remove((Object ) permission); 156 } 157 158 161 public void clear() 162 { 163 set.clear(); 164 } 165 166 173 public boolean contains(Permission permission) 174 { 175 return set.contains((Object ) permission); 176 } 177 178 187 public boolean contains(String permissionName) 188 { 189 Iterator iter = set.iterator(); 190 while (iter.hasNext()) 191 { 192 Permission permission = (Permission) iter.next(); 193 if (permissionName != null && 194 permissionName.equals(((SecurityEntity) permission).getName())) 195 { 196 return true; 197 } 198 } 199 return false; 200 } 201 202 210 public Permission getPermission(String permissionName) 211 { 212 Iterator iter = set.iterator(); 213 while (iter.hasNext()) 214 { 215 Permission permission = (Permission) iter.next(); 216 if (permissionName != null && 217 permissionName.equals(((SecurityEntity) permission).getName())) 218 { 219 return permission; 220 } 221 } 222 return null; 223 } 224 225 230 public Permission [] getPermissionsArray() 231 { 232 return (Permission []) set.toArray(new Permission[0]); 233 } 234 235 240 public Iterator elements() 241 { 242 return set.iterator(); 243 } 244 245 250 public int size() 251 { 252 return set.size(); 253 } 254 255 260 public String toString() 261 { 262 StringBuffer sbuf = new StringBuffer (12 * size()); 263 Iterator i = set.iterator(); 264 while (i.hasNext()) 265 { 266 sbuf.append(((Permission) i.next()).getName()) 267 .append(", "); 268 } 269 return sbuf.toString(); 270 } 271 } 272 | Popular Tags |