internal HtmlDataQualityCategory(
            [CanBeNull] DataQualityCategory category,
            [CanBeNull] HtmlDataQualityCategoryOptions options,
            [NotNull] HtmlDataQualityCategoryComparer categoryComparer,
            [NotNull] HtmlQualitySpecificationElementComparer elementComparer)
        {
            Assert.ArgumentNotNull(categoryComparer, nameof(categoryComparer));
            Assert.ArgumentNotNull(elementComparer, nameof(elementComparer));

            _category         = category;
            _categoryComparer = categoryComparer;
            _elementComparer  = elementComparer;

            if (category == null)
            {
                _isUndefinedCategory = true;
                _uniqueName          = "<nocategory>";

                _name         = string.Empty;
                _abbreviation = string.Empty;
            }
            else
            {
                _isUndefinedCategory = false;
                _uniqueName          = category.GetQualifiedName("||");

                _name         = GetDisplayName(category, options);
                _abbreviation = category.Abbreviation ?? string.Empty;
            }
        }
        private static HtmlDataQualityCategory AddDataQualityCategory(
            [CanBeNull] DataQualityCategory category,
            [NotNull] HtmlDataQualityCategoryComparer categoryComparer,
            [NotNull] HtmlQualitySpecificationElementComparer elementComparer,
            [NotNull] IDictionary <string, HtmlDataQualityCategory> reportCategories,
            [CanBeNull] IHtmlDataQualityCategoryOptionsProvider optionsProvider = null)
        {
            string key = GetCategoryKey(category);

            HtmlDataQualityCategory result;

            if (reportCategories.TryGetValue(key, out result))
            {
                // already added (including parents)
                return(result);
            }

            HtmlDataQualityCategoryOptions options =
                GetReportCategoryOptions(optionsProvider, category);

            if (category != null && options != null && options.IgnoreCategoryLevel)
            {
                // skip this category level
                result = AddDataQualityCategory(category.ParentCategory,
                                                categoryComparer,
                                                elementComparer,
                                                reportCategories,
                                                optionsProvider);

                reportCategories.Add(key, result);
                return(result);
            }

            result = new HtmlDataQualityCategory(category,
                                                 options,
                                                 categoryComparer,
                                                 elementComparer);
            reportCategories.Add(key, result);

            if (category != null && category.ParentCategory != null)
            {
                HtmlDataQualityCategory parent = AddDataQualityCategory(category.ParentCategory,
                                                                        categoryComparer,
                                                                        elementComparer,
                                                                        reportCategories,
                                                                        optionsProvider);
                if (!parent.IsUndefinedCategory)
                {
                    result.ParentCategory = parent;
                    result.ParentCategory.IncludeSubCategory(result);
                }
            }

            return(result);
        }
        private static IEnumerable <HtmlDataQualityCategory> GroupByCategories(
            [NotNull] IEnumerable <QualitySpecificationElement> elements,
            [NotNull] IDictionary <TestDescriptor, HtmlTestDescriptor> testDescriptors,
            [NotNull] HtmlDataQualityCategoryComparer categoryComparer,
            [NotNull] HtmlQualitySpecificationElementComparer elementComparer,
            [CanBeNull] IHtmlDataQualityCategoryOptionsProvider optionsProvider,
            [NotNull] out List <HtmlQualitySpecificationElement>
            htmlQualitySpecificationElements)
        {
            List <QualitySpecificationElement> elementsList = elements.ToList();

            IDictionary <string, HtmlDataQualityCategory> reportCategories =
                MapReportCategories(elementsList,
                                    categoryComparer,
                                    elementComparer,
                                    optionsProvider);

            htmlQualitySpecificationElements = new List <HtmlQualitySpecificationElement>();

            foreach (QualitySpecificationElement element in elementsList)
            {
                HtmlDataQualityCategory reportCategory =
                    reportCategories[GetCategoryKey(element.QualityCondition.Category)];

                HtmlTestDescriptor htmlTestDescriptor =
                    testDescriptors[element.QualityCondition.TestDescriptor];

                var htmlQualityCondition = new HtmlQualityCondition(
                    element.QualityCondition, htmlTestDescriptor, reportCategory);

                var htmlElement = new HtmlQualitySpecificationElement(htmlQualityCondition,
                                                                      element);

                reportCategory.AddQualitySpecificationElement(htmlElement);
                htmlQualitySpecificationElements.Add(htmlElement);

                htmlTestDescriptor.AddReferencingElement(htmlElement);
            }

            htmlQualitySpecificationElements.Sort(elementComparer);

            // exclude undefined root category if it does not contain any quality conditions

            return(reportCategories.Values
                   .Where(cat => !cat.IsRoot ||
                          !cat.IsUndefinedCategory ||
                          cat.QualitySpecificationElements.Count > 0)
                   .Distinct()
                   .OrderBy(c => c, categoryComparer)
                   .ToList());
        }
        private static Dictionary <string, HtmlDataQualityCategory> MapReportCategories
            ([NotNull] IEnumerable <QualitySpecificationElement> issueGroups,
            [NotNull] HtmlDataQualityCategoryComparer categoryComparer,
            [NotNull] HtmlQualitySpecificationElementComparer qualityConditionComparer,
            [CanBeNull] IHtmlDataQualityCategoryOptionsProvider optionsProvider = null)
        {
            var result = new Dictionary <string, HtmlDataQualityCategory>();

            foreach (QualitySpecificationElement issueGroup in issueGroups)
            {
                // add the next non-ignored category
                AddDataQualityCategory(issueGroup.QualityCondition.Category,
                                       categoryComparer,
                                       qualityConditionComparer,
                                       result,
                                       optionsProvider);
            }

            return(result);
        }