Class PermissionUtil


  • public final class PermissionUtil
    extends Object
    General permission utility methods and constants.
    Author:
    David M. Lloyd
    • Field Detail

      • EMPTY_PERMISSION_COLLECTION

        public static final PermissionCollection EMPTY_PERMISSION_COLLECTION
        A permission collection which is empty.
      • NO_PERMISSIONS

        public static final Permission[] NO_PERMISSIONS
        An array with no permissions in it.
    • Constructor Detail

      • PermissionUtil

        private PermissionUtil()
    • Method Detail

      • parseActions

        public static int parseActions​(String actionsString,
                                       ToIntFunction<String> function)
                                throws IllegalArgumentException
        Parse an actions string, using the given function to map action strings to bits.
        Parameters:
        actionsString - the actions string (must not be null)
        function - the mapping function (must not be null)
        Returns:
        the union of all the action bits
        Throws:
        IllegalArgumentException - if function throws this exception (indicating an invalid action string)
      • parseActions

        public static long parseActions​(String actionsString,
                                        ToLongFunction<String> function)
                                 throws IllegalArgumentException
        Parse an actions string, using the given function to map action strings to bits.
        Parameters:
        actionsString - the actions string (must not be null)
        function - the mapping function (must not be null)
        Returns:
        the union of all the action bits
        Throws:
        IllegalArgumentException - if function throws this exception (indicating an invalid action string)
      • toActionsString

        public static String toActionsString​(int actionBits,
                                             IntFunction<String> mappingFunction)
        Deparse an action bit set, using the given function to map action bits to strings. If the bits are all clear, the empty string "" is returned.
        Parameters:
        actionBits - the action bit set
        mappingFunction - the mapping function (must not be null)
        Returns:
        the actions string (not null)
      • toActionsString

        public static String toActionsString​(long actionBits,
                                             LongFunction<String> mappingFunction)
        Deparse an action bit set, using the given function to map action bits to strings. If the bits are all clear, the empty string "" is returned.
        Parameters:
        actionBits - the action bit set
        mappingFunction - the mapping function (must not be null)
        Returns:
        the actions string (not null)
      • iterable

        public static Iterable<Permission> iterable​(PermissionCollection pc)
        Create an iterable view over a permission collection.
        Parameters:
        pc - the permission collection (must not be null)
        Returns:
        the iterable view (not null)
      • forEachIn

        public static void forEachIn​(PermissionCollection collection,
                                     Consumer<Permission> consumer)
        Perform an action for each permission in the given collection.
        Parameters:
        collection - the collection (must not be null)
        consumer - the consumer to which each permission should be passed (must not be null)
      • forEachIn

        public static <P> P forEachIn​(PermissionCollection collection,
                                      BiConsumer<P,​Permission> consumer,
                                      P parameter)
        Perform an action for each permission in the given collection.
        Type Parameters:
        P - the type of the parameter
        Parameters:
        collection - the collection (must not be null)
        parameter - the parameter to pass to the consumer
        consumer - the consumer to which each permission should be passed (must not be null)
        Returns:
        the parameter that was passed in
      • forEachIn

        public static boolean forEachIn​(PermissionCollection collection,
                                        Predicate<Permission> predicate)
        Run a test for each permission in the given collection. If the predicate returns false for any element, false is returned; otherwise, true is returned.
        Parameters:
        collection - the collection (must not be null)
        predicate - the predicate to apply to each element (must not be null)
        Returns:
        true if the predicate matched all the permissions in the collection, false otherwise
      • forEachIn

        public static <P> boolean forEachIn​(PermissionCollection collection,
                                            BiPredicate<P,​Permission> predicate,
                                            P parameter)
        Run a test for each permission in the given collection. If the predicate returns false for any element, false is returned; otherwise, true is returned.
        Type Parameters:
        P - the type of the parameter
        Parameters:
        collection - the collection (must not be null)
        parameter - the parameter to pass to the consumer
        predicate - the predicate to apply to each element (must not be null)
        Returns:
        true if the predicate matched all the permissions in the collection, false otherwise
      • union

        public static PermissionCollection union​(PermissionCollection pc1,
                                                 PermissionCollection pc2)
        Create a permission collection that is the union of two permission collections. The permission collections must be read-only.
        Parameters:
        pc1 - the first permission collection (must not be null)
        pc2 - the second permission collection (must not be null)
        Returns:
        a new permission collection that is the union of the two collections (not null)
      • intersection

        public static PermissionCollection intersection​(PermissionCollection pc1,
                                                        PermissionCollection pc2)
        Create a permission collection that is the intersection of two permission collections. The permission collections must be read-only.
        Parameters:
        pc1 - the first permission collection (must not be null)
        pc2 - the second permission collection (must not be null)
        Returns:
        a new permission collection that is the intersection of the two collections (not null)
      • impliesAll

        public static boolean impliesAll​(PermissionCollection collection,
                                         PermissionCollection testCollection)
        Determine if one collection implies all the permissions in the other collection.
        Parameters:
        collection - the collection to check against (must not be null)
        testCollection - the collection whose permissions are to be tested (must not be null)
        Returns:
        true if collection implies all of the permissions in testCollection, false otherwise
      • equals

        public static boolean equals​(PermissionCollection pc1,
                                     PermissionCollection pc2)
        Determine if two permission collections are equal, that is, each collection implies all of the permissions in the other collection.
        Parameters:
        pc1 - the first collection (must not be null)
        pc2 - the second collection (must not be null)
        Returns:
        true if the collections imply one another, false otherwise
      • addAll

        public static PermissionCollection addAll​(PermissionCollection target,
                                                  PermissionCollection source)
        Add all of the permissions from the source collection to the target collection.
        Parameters:
        target - the target collection (must not be null)
        source - the source collection (must not be null)
        Returns:
        the target collection (not null)
      • addAll

        public static PermissionCollection addAll​(PermissionCollection target,
                                                  Collection<Permission> source)
        Add all of the permissions from the source collection to the target collection.
        Parameters:
        target - the target collection (must not be null)
        source - the source collection (must not be null)
        Returns:
        the target collection (not null)
      • add

        public static PermissionCollection add​(PermissionCollection target,
                                               Permission source)
        Add a permission to a collection, returning the target collection. If the permission is null, it is not added.
        Parameters:
        target - the target collection (must not be null)
        source - the permission to add
        Returns:
        the target collection (not null)
      • createPermission

        public static Permission createPermission​(ClassLoader classLoader,
                                                  String className,
                                                  String name,
                                                  String actions)
        Instantiate a permission with the given class name, permission name, and actions.
        Parameters:
        classLoader - the class loader to search in (null indicates the system class loader)
        className - the name of the permission class to instantiate (must not be null)
        name - the permission name (may be null if allowed by the permission class)
        actions - the permission actions (may be null if allowed by the permission class)
        Returns:
        the permission object (not null)
        Throws:
        InvalidPermissionClassException - if the permission class does not exist or is not valid
        ClassCastException - if the class name does not refer to a subclass of Permission
      • createPermission

        public static Permission createPermission​(Class<? extends Permission> permissionClass,
                                                  String name,
                                                  String actions)
        Instantiate a permission with the given class, permission name, and actions.
        Parameters:
        permissionClass - the permission class to instantiate (must not be null)
        name - the permission name (may be null if allowed by the permission class)
        actions - the permission actions (may be null if allowed by the permission class)
        Returns:
        the permission object (not null)
        Throws:
        InvalidPermissionClassException - if the permission class does not exist or is not valid
      • readOnlyCollectionOf

        public static PermissionCollection readOnlyCollectionOf​(Permission... permissions)
        Get a read-only collection of the given permissions.
        Parameters:
        permissions - the permissions to assign
        Returns:
        the read-only collection