コード例 #1
0
        public static FilterChain CreateChain(Definitions defs, CreatorCallback creator)
        {
            FilterChain chain = new AndChain();

            if (defs != null && defs.Count > 0)
            {
                if (!defs.Any(f => f.Operation == Operations.Or))
                {
                    // Simple AND chain
                    defs.ForEach(def => chain.Add(creator(def)));
                }
                else
                {
                    // Mixed chain
                    Definition  prev = defs[0];
                    Definition  curr;
                    FilterChain or = new OrChain();
                    for (int index = 1; index < defs.Count; ++index)
                    {
                        curr = defs[index];
                        if (curr.Operation == Operations.Or && prev.Operation == Operations.Or)
                        {
                            // Add another "or"
                            or.Add(creator(prev));
                        }
                        else
                        {
                            if (or.Count != 0)
                            {
                                // End current "or" and create new chain
                                or.Add(creator(prev));
                                chain.Add(or);
                                or = new OrChain();
                            }
                            else
                            {
                                // Add single filter
                                chain.Add(creator(prev));
                            }
                        }

                        prev = curr;
                    }

                    // Add last pending
                    if (prev.Operation == Operations.Or)
                    {
                        or.Add(creator(prev));
                    }
                    if (or.Count > 0)
                    {
                        chain.Add(or);
                    }
                    if (prev.Operation == Operations.And)
                    {
                        chain.Add(creator(prev));
                    }
                }
            }

            return(chain);
        }