class TreeBasedPolicyEnforcer implements Enforcer {

    public static TreeBasedPolicyEnforcer createInstance(final Policy policy) {
        checkNotNull(policy, "policy");
        final Map<String, PolicyTreeNode> tree = new HashMap<>();

        final Set<PolicyEntry> policyEntries = policy.getEntriesSet();
        policyEntries.forEach(policyEntry -> {

            final Subjects subjects = policyEntry.getSubjects();
            subjects.forEach(subject -> {
                final SubjectId subjectId = subject.getId();
                final String subjectIdString = subjectId.toString();
                final PolicyTreeNode parentNode = tree.computeIfAbsent(subjectIdString, SubjectNode::of);

                final Resources resources = policyEntry.getResources();
                resources.forEach(resource -> {
                    final PolicyTreeNode rootChild = parentNode.computeIfAbsent(resource.getType(), t -> {
                        final Set<String> emptySet = Collections.emptySet();
                        return ResourceNode.of(parentNode, t, EffectedPermissions.newInstance(emptySet, emptySet));
                    });
                    addResourceSubTree((ResourceNode) rootChild, resource, resource.getPath());
                });
            });
        });

        return new TreeBasedPolicyEnforcer(tree);
    }

    private  TreeBasedPolicyEnforcer(final Map<String, PolicyTreeNode> tree);

}