All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.n2oapp.framework.access.api.model.ComplexPermission Maven / Gradle / Ivy

There is a newer version: 7.28.1
Show newest version
package net.n2oapp.framework.access.api.model;

import java.util.List;
import java.util.Optional;

/**
 * User: operehod
 * Date: 17.02.2015
 * Time: 11:49
 */
public class ComplexPermission extends Permission {

    private List permissions;

    public ComplexPermission(List permissions) {
        super(resolve(permissions));
        for (Permission permission : permissions) {
            if (!permission.isAllowed()) {
                setDetailedMessage(permission.getDetailedMessage());
                setTechMessage(permission.getTechMessage());
            }
        }
        this.permissions = permissions;
    }

    private static boolean resolve(List permissions) {
        for (Permission permission : permissions) {
            if (!permission.isAllowed())
                return false;
        }
        return true;
    }

    public List getPermissions() {
        return permissions;
    }

    @SuppressWarnings("unchecked")
    public  Optional getPermissionByType(Class type) {
        return (Optional) permissions.stream()
                .map(p -> {
                    Optional res = p instanceof ComplexPermission ? ((ComplexPermission)p).getPermissionByType(type) : Optional.empty();
                    return res.isPresent() ? res.get() : p;
                })
                .filter(p -> p.getClass() == type).findFirst();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy