예제 #1
0
        internal void AddElementToContainer(IElement element)
        {
            // Because we can't tell ahead of time if there will be
            // more than one element added to an element container,
            // we need to check at run-time.
            //
            // If the provided element is the first in the chain, we
            // assume it's the only one, and simply add it to the
            // container (whatever type that may be).
            // If a second element comes along, and it needs to go
            // in that container, then that means there is a
            // "sequence" of elements. Remove the first element
            // that was added earlier, add it and the new element
            // to a new sequence object, and put the sequence
            // into the container.
            //
            // Example of a "chain" (probably not the best term):
            //
            // |-- 0 --|------------ 1 ------------|-- 2 --| // <-- Chain #
            // Say("X").Optionally(r => r.Say("Y")).Say("Z")

            if (_countInChain == 1)
            {
                ISequence sequence = new Sequence();

                sequence.AddElement(_container.Pop());
                sequence.AddElement(element);

                _container.AddElement(sequence);
                _currentSequence = sequence;
            }
            else if (_countInChain > 1)
            {
                _currentSequence.AddElement(element);
            }
            else
            {
                _container.AddElement(element);
            }

            _countInChain++;
        }