Пример #1
0
        /// <summary>
        /// </summary>
        /// <param name="filterCriteria"></param>
        /// <returns></returns>
        public virtual IPluralRulesEnumerable Query(PluralRuleInfo filterCriteria)
        {
            if (filterCriteria.Equals(PluralRuleInfo.Empty))
            {
                return(this);
            }

            int c = 0;

            foreach (var rule in this)
            {
                if (filterCriteria.FilterMatch(rule.Info))
                {
                    c++;
                }
            }
            IPluralRule[] result = new IPluralRule[c];

            int i = 0;

            foreach (var rule in this)
            {
                if (filterCriteria.FilterMatch(rule.Info))
                {
                    result[i++] = rule;
                }
            }

            return(new PluralRulesArray(result));
        }
Пример #2
0
        /// <summary>
        /// Filter rules by <paramref name="filterCriteria"/>.
        /// </summary>
        /// <param name="filterCriteria"></param>
        /// <returns><see cref="PluralRulesCasesEvaluatable"/> or <see cref="PluralRulesArray"/>, or null if content is empty</returns>
        public IPluralRulesEnumerable Query(PluralRuleInfo filterCriteria)
        {
            // Try get
            IPluralRulesEnumerable result = null;

            lock (m_lock) if (queries.TryGetValue(filterCriteria, out result))
                {
                    return(result);
                }

            // Query
            if (ruleSource is IPluralRulesQueryable queryable)
            {
                result = queryable.Query(filterCriteria);
                // Wrap into PluralRulesEvaluatable
                if (result != null && filterCriteria.Category != null && filterCriteria.Culture != null && filterCriteria.Case == null && filterCriteria.Optional == -1)
                {
                    result = new PluralRulesCasesEvaluatable(result);
                }
            }
            else if (ruleSource is IEnumerable <IPluralRule> enumr)
            {
                // Filter rules
                StructList8 <IPluralRule> list     = new StructList8 <IPluralRule>();
                StructList8 <string>      rulesets = new StructList8 <string>();
                foreach (IPluralRule rule in enumr)
                {
                    // Filter by criteria
                    if (!filterCriteria.FilterMatch(rule.Info))
                    {
                        continue;
                    }
                    // Gather a list of rulesets
                    if (rule.Info.RuleSet != null && !rulesets.Contains(rule.Info.RuleSet))
                    {
                        rulesets.Add(rule.Info.RuleSet);
                    }
                    // Add to list
                    list.Add(rule);
                }
                // No result
                if (list.Count == 0)
                {
                    result = null;
                }
                // Instantiate PluralRulesEvaluatable
                else if (rulesets.Count <= 1 && filterCriteria.Category != null && filterCriteria.Culture != null && filterCriteria.Case == null && filterCriteria.Optional == -1)
                {
                    result = new PluralRulesCasesEvaluatable(list.ToArray());
                }
                // Instantiate PluralRules.
                else
                {
                    result = new PluralRulesArray(list.ToArray());
                }
            }
            // Could not read source
            else
            {
                result = null;
            }
            // Write to cache, if is still new
            lock (m_lock) if (!queries.ContainsKey(filterCriteria))
                {
                    queries[filterCriteria] = result;
                }

            // Return result
            return(result);
        }