class JsonCnd {

    public static JsonValue privilegesToJson(final @NotNull List<PrivilegeDefinition> privDefs,
                                             final @NotNull NamespaceMapping mapping) {
        final NamePathResolver resolver = new DefaultNamePathResolver(mapping);
        if (privDefs.stream().allMatch(def -> !def.isAbstract() && def.getDeclaredAggregateNames().isEmpty())) {
            return privDefs.stream()
                    .map(compose1(PrivilegeDefinition::getName, uncheck1(jcrNameOrResidual(resolver))))
                    .map(JavaxJson::wrap)
                    .sorted(Comparator.comparing(JSON_VALUE_STRING))
                    .collect(JsonCollectors.toJsonArray());
        }
        return privDefs.stream()
                .map(def -> toEntry(def, (JsonValue) obj()
                        .key(PRIVILEGE_KEY_ABSTRACT).opt(def.isAbstract(), false)
                        .key(PRIVILEGE_KEY_CONTAINS)
                        .opt(def.getDeclaredAggregateNames().stream()
                                .map(uncheck1(jcrNameOrResidual(resolver)))
                                .collect(Collectors.toList()))
                        .get()))
                .map(mapKey(compose1(PrivilegeDefinition::getName, uncheck1(jcrNameOrResidual(resolver)))))
                .sorted(Map.Entry.comparingByKey())
                .collect(JsonCollectors.toJsonObject());
    }

    private  JsonCnd();

}