Coverage Report - org.apache.turbine.util.security.PermissionSet
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionSet
0%
0/38
0%
0/12
1,545
 
 1  
 package org.apache.turbine.util.security;
 2  
 
 3  
 
 4  
 /*
 5  
  * Licensed to the Apache Software Foundation (ASF) under one
 6  
  * or more contributor license agreements.  See the NOTICE file
 7  
  * distributed with this work for additional information
 8  
  * regarding copyright ownership.  The ASF licenses this file
 9  
  * to you under the Apache License, Version 2.0 (the
 10  
  * "License"); you may not use this file except in compliance
 11  
  * with the License.  You may obtain a copy of the License at
 12  
  *
 13  
  *   http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing,
 16  
  * software distributed under the License is distributed on an
 17  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 18  
  * KIND, either express or implied.  See the License for the
 19  
  * specific language governing permissions and limitations
 20  
  * under the License.
 21  
  */
 22  
 
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.Iterator;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.apache.turbine.om.security.Permission;
 29  
 
 30  
 /**
 31  
  * This class represents a set of Permissions.  It makes it easy to
 32  
  * build a UI that would allow someone to add a group of Permissions
 33  
  * to a Role.  It enforces that only
 34  
  * Permission objects are allowed in the set and only relevant methods
 35  
  * are available.
 36  
  *
 37  
  * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
 38  
  * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
 39  
  * @author <a href="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
 40  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 41  
  * @version $Id: PermissionSet.java 1073174 2011-02-21 22:18:45Z tv $
 42  
  */
 43  
 public class PermissionSet
 44  
     extends SecuritySet<Permission>
 45  
 {
 46  
     /** Serial version */
 47  
         private static final long serialVersionUID = 7740960537240820226L;
 48  
 
 49  
         /**
 50  
      * Constructs an empty PermissionSet
 51  
      */
 52  
     public PermissionSet()
 53  
     {
 54  0
         super();
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Constructs a new PermissionSet with specified contents.
 59  
      *
 60  
      * If the given collection contains multiple objects that are
 61  
      * identical WRT equals() method, some objects will be overwritten.
 62  
      *
 63  
      * @param permissions A collection of permissions to be contained in the set.
 64  
      */
 65  
     public PermissionSet(Collection<Permission> permissions)
 66  
     {
 67  0
         super();
 68  0
         add(permissions);
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Adds a Permission to this PermissionSet.
 73  
      *
 74  
      * @param permission A Permission.
 75  
      * @return True if Permission was added; false if PermissionSet
 76  
      * already contained the Permission.
 77  
      */
 78  
     public boolean add(Permission permission)
 79  
     {
 80  0
         boolean res = contains(permission);
 81  0
         nameMap.put(permission.getName(), permission);
 82  0
         idMap.put(permission.getIdAsObj(), permission);
 83  0
         return res;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Adds the Permissions in a Collection to this PermissionSet.
 88  
      *
 89  
      * @param permissions A Collection of Permissions.
 90  
      * @return True if this PermissionSet changed as a result; false
 91  
      * if no change to this PermissionSet occurred (this PermissionSet
 92  
      * already contained all members of the added PermissionSet).
 93  
      */
 94  
     public boolean add(Collection<Permission> permissions)
 95  
     {
 96  0
         boolean res = false;
 97  0
         for (Permission p : permissions)
 98  
         {
 99  0
             res |= add(p);
 100  
         }
 101  0
         return res;
 102  
     }
 103  
 
 104  
     /**
 105  
      * Adds the Permissions in another PermissionSet to this
 106  
      * PermissionSet.
 107  
      *
 108  
      * @param permissionSet A PermissionSet.
 109  
      * @return True if this PermissionSet changed as a result; false
 110  
      * if no change to this PermissionSet occurred (this PermissionSet
 111  
      * already contained all members of the added PermissionSet).
 112  
      */
 113  
     public boolean add(PermissionSet permissionSet)
 114  
     {
 115  0
         boolean res = false;
 116  0
         for(Permission p : permissionSet)
 117  
         {
 118  0
             res |= add(p);
 119  
         }
 120  0
         return res;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Removes a Permission from this PermissionSet.
 125  
      *
 126  
      * @param permission A Permission.
 127  
      * @return True if this PermissionSet contained the Permission
 128  
      * before it was removed.
 129  
      */
 130  
     public boolean remove(Permission permission)
 131  
     {
 132  0
         boolean res = contains(permission);
 133  0
         nameMap.remove(permission.getName());
 134  0
         idMap.remove(permission.getIdAsObj());
 135  0
         return res;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Checks whether this PermissionSet contains a Permission.
 140  
      *
 141  
      * @param permission A Permission.
 142  
      * @return True if this PermissionSet contains the Permission,
 143  
      * false otherwise.
 144  
      */
 145  
     public boolean contains(Permission permission)
 146  
     {
 147  0
         return nameMap.containsValue(permission);
 148  
     }
 149  
 
 150  
     /**
 151  
      * Returns a Permission with the given name, if it is contained in
 152  
      * this PermissionSet.
 153  
      *
 154  
      * @param permissionName Name of Permission.
 155  
      * @return Permission if argument matched a Permission in this
 156  
      * PermissionSet; null if no match.
 157  
      */
 158  
     public Permission getPermissionByName(String permissionName)
 159  
     {
 160  0
         return (StringUtils.isNotEmpty(permissionName))
 161  
                 ? (Permission) nameMap.get(permissionName) : null;
 162  
     }
 163  
 
 164  
     /**
 165  
      * Returns a Permission with the given id, if it is contained in
 166  
      * this PermissionSet.
 167  
      *
 168  
      * @param permissionId Id of the Permission.
 169  
      * @return Permission if argument matched a Permission in this
 170  
      * PermissionSet; null if no match.
 171  
      */
 172  
     public Permission getPermissionById(int permissionId)
 173  
     {
 174  0
         return (permissionId != 0)
 175  
                 ? (Permission) idMap.get(new Integer(permissionId)) : null;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Returns an Array of Permissions in this PermissionSet.
 180  
      *
 181  
      * @return An Array of Permission Objects.
 182  
      */
 183  
     public Permission[] getPermissionsArray()
 184  
     {
 185  0
         return getSet().toArray(new Permission[0]);
 186  
     }
 187  
 
 188  
     /**
 189  
      * Print out a PermissionSet as a String
 190  
      *
 191  
      * @returns The Permission Set as String
 192  
      *
 193  
      */
 194  
     @Override
 195  
     public String toString()
 196  
     {
 197  0
         StringBuffer sb = new StringBuffer();
 198  0
         sb.append("PermissionSet: ");
 199  
 
 200  0
         for(Iterator<Permission> it = iterator(); it.hasNext();)
 201  
         {
 202  0
             Permission p = it.next();
 203  0
             sb.append('[');
 204  0
             sb.append(p.getName());
 205  0
             sb.append(" -> ");
 206  0
             sb.append(p.getIdAsObj());
 207  0
             sb.append(']');
 208  0
             if (it.hasNext())
 209  
             {
 210  0
                 sb.append(", ");
 211  
             }
 212  0
         }
 213  
 
 214  0
         return sb.toString();
 215  
     }
 216  
 }