/// <summary> /// Attempts to fetch a FilterListEntry instance for the supplied category name, or create a /// new one if one does not exist. Whether one is created, or an existing instance is /// discovered, a valid, unique FilterListEntry for the supplied category shall be returned. /// </summary> /// <param name="categoryName"> /// The category name for which to fetch or generate a new FilterListEntry instance. /// </param> /// <returns> /// The unique FilterListEntry for the supplied category name, whether an existing instance /// was found or a new one was created. /// </returns> /// <remarks> /// This will always fail if more than 255 categories are created! /// </remarks> private bool TryFetchOrCreateCategoryMap <T>(string categoryName, PlainTextFilteringListType listType, out T model) where T : MappedFilterListCategoryModel { m_logger.Info("CATEGORY {0}", categoryName); MappedFilterListCategoryModel existingCategory = null; if (!m_generatedCategoriesMap.TryGetValue(categoryName, out existingCategory)) { // We can't generate anymore categories. Sorry, but the rest get ignored. if (m_generatedCategoriesMap.Count >= short.MaxValue) { m_logger.Error("The maximum number of filtering categories has been exceeded."); model = null; return(false); } if (typeof(T) == typeof(MappedBypassListCategoryModel)) { MappedFilterListCategoryModel secondCategory = null; if (TryFetchOrCreateCategoryMap(categoryName + "_as_whitelist", PlainTextFilteringListType.Whitelist, out secondCategory)) { var newModel = (T)(MappedFilterListCategoryModel) new MappedBypassListCategoryModel((byte)((m_generatedCategoriesMap.Count) + 1), secondCategory.CategoryId, categoryName, secondCategory.CategoryName); m_generatedCategoriesMap.GetOrAdd(categoryName, newModel); model = newModel; return(true); } else { model = null; return(false); } } else { var newModel = (T) new MappedFilterListCategoryModel((byte)((m_generatedCategoriesMap.Count) + 1), categoryName, listType); m_generatedCategoriesMap.GetOrAdd(categoryName, newModel); model = newModel; return(true); } } model = existingCategory as T; return(true); }
/// <summary> /// Constructs a new MappedFilterListCategoryModel instance. /// </summary> /// <param name="categoryId"> /// The generated category ID. /// </param> /// <param name="categoryName"> /// The category name. /// </param> public MappedFilterListCategoryModel(short categoryId, string categoryName, PlainTextFilteringListType listType) { CategoryId = categoryId; CategoryName = categoryName; ListType = listType; }