<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ploeh.AutoFixture.Kernel
{
    /// <summary>
    /// Creates specimens by returning the first specimen created by its children.
    /// </summary>
    public class CompositeSpecimenBuilder : ISpecimenBuilderNode
    {
        private readonly List<ISpecimenBuilder> builders;

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeSpecimenBuilder"/> class.
        /// </summary>
#warning Remove when Builders are changed to IEnumerable<ISpecimenBuilder>
        public CompositeSpecimenBuilder()
            : this(Enumerable.Empty<ISpecimenBuilder>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeSpecimenBuilder"/> class with the
        /// supplied builders.
        /// </summary>
        /// <param name="builders">The child builders.</param>
        public CompositeSpecimenBuilder(IEnumerable<ISpecimenBuilder> builders)
            : this(builders.ToArray())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeSpecimenBuilder"/> class with the
        /// supplied builders.
        /// </summary>
        /// <param name="builders">The child builders.</param>
        public CompositeSpecimenBuilder(params ISpecimenBuilder[] builders)
        {
            if (builders == null)
            {
                throw new ArgumentNullException("builders");
            }

            this.builders = builders.ToList();
        }

        /// <summary>
        /// Gets the child builders.
        /// </summary>
        [Obsolete("In AutoFixture 3 this property will be weakened to return only IEnumerable<ISpecimenBuilder>. If you want to 'add' a new ISpecimenBuilder to a CompositeSpecimenBuilder, you can use the Compose method and pass in the current instance cancatenated with the new instance. Note that CompositeSpecimenBuilder itself implements IEnumerable<ISpecimenBuilder>, so you can enumerate over all the contained buliders by iterating directly over the instance instead of iterating over the Builders property.")]
        public IList<ISpecimenBuilder> Builders
        {
            get { return this.builders; }
        }

        /// <summary>
        /// Creates a new specimen by delegating to <see cref="Builders"/>.
        /// </summary>
        /// <param name="request">The request that describes what to create.</param>
        /// <param name="context">A container that can be used to create other specimens.</param>
        /// <returns>The first result created by <see cref="Builders"/>.</returns>
        public object Create(object request, ISpecimenContext context)
        {
            return (from b in this.builders
                    let result = b.Create(request, context)
                    where !(result is NoSpecimen)
                    select result).DefaultIfEmpty(new NoSpecimen(request)).FirstOrDefault();
        }

        /// <summary>Composes the supplied builders.</summary>
        /// <param name="builders">The builders to compose.</param>
        /// <returns>
        /// A new <see cref="ISpecimenBuilderNode" /> instance containing
        /// <paramref name="builders" /> as child nodes.
        /// </returns>
        public virtual ISpecimenBuilderNode Compose(IEnumerable<ISpecimenBuilder> builders)
        {
            return new CompositeSpecimenBuilder(builders);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="IEnumerator{ISpecimenBuilder}" /> that can be used to
        /// iterate through the collection.
        /// </returns>
        public IEnumerator<ISpecimenBuilder> GetEnumerator()
        {
            return this.builders.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator" /> object that can
        /// be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        internal static ISpecimenBuilder ComposeIfMultiple(IEnumerable<ISpecimenBuilder> builders)
        {
            var isSingle = builders.Take(2).Count() == 1;
            if (isSingle)
                return builders.Single();

            return new CompositeSpecimenBuilder(builders);
        }

        internal static ISpecimenBuilderNode UnwrapIfSingle(ISpecimenBuilderNode node)
        {
            var c = node as CompositeSpecimenBuilder;
            if (c == null)
                return node;
            var isSingle = c.builders.Take(2).Count() == 1;
            if (isSingle)
            {
                var n = c.builders.Single() as ISpecimenBuilderNode;
                if (n != null)
                    return n;
            }
            return node;
        }
    }
}
<commit_msg>Removed redundant constructor.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ploeh.AutoFixture.Kernel
{
    /// <summary>
    /// Creates specimens by returning the first specimen created by its children.
    /// </summary>
    public class CompositeSpecimenBuilder : ISpecimenBuilderNode
    {
        private readonly List<ISpecimenBuilder> builders;

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeSpecimenBuilder"/> class with the
        /// supplied builders.
        /// </summary>
        /// <param name="builders">The child builders.</param>
        public CompositeSpecimenBuilder(IEnumerable<ISpecimenBuilder> builders)
            : this(builders.ToArray())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeSpecimenBuilder"/> class with the
        /// supplied builders.
        /// </summary>
        /// <param name="builders">The child builders.</param>
        public CompositeSpecimenBuilder(params ISpecimenBuilder[] builders)
        {
            if (builders == null)
            {
                throw new ArgumentNullException("builders");
            }

            this.builders = builders.ToList();
        }

        /// <summary>
        /// Gets the child builders.
        /// </summary>
        [Obsolete("In AutoFixture 3 this property will be weakened to return only IEnumerable<ISpecimenBuilder>. If you want to 'add' a new ISpecimenBuilder to a CompositeSpecimenBuilder, you can use the Compose method and pass in the current instance cancatenated with the new instance. Note that CompositeSpecimenBuilder itself implements IEnumerable<ISpecimenBuilder>, so you can enumerate over all the contained buliders by iterating directly over the instance instead of iterating over the Builders property.")]
        public IList<ISpecimenBuilder> Builders
        {
            get { return this.builders; }
        }

        /// <summary>
        /// Creates a new specimen by delegating to <see cref="Builders"/>.
        /// </summary>
        /// <param name="request">The request that describes what to create.</param>
        /// <param name="context">A container that can be used to create other specimens.</param>
        /// <returns>The first result created by <see cref="Builders"/>.</returns>
        public object Create(object request, ISpecimenContext context)
        {
            return (from b in this.builders
                    let result = b.Create(request, context)
                    where !(result is NoSpecimen)
                    select result).DefaultIfEmpty(new NoSpecimen(request)).FirstOrDefault();
        }

        /// <summary>Composes the supplied builders.</summary>
        /// <param name="builders">The builders to compose.</param>
        /// <returns>
        /// A new <see cref="ISpecimenBuilderNode" /> instance containing
        /// <paramref name="builders" /> as child nodes.
        /// </returns>
        public virtual ISpecimenBuilderNode Compose(IEnumerable<ISpecimenBuilder> builders)
        {
            return new CompositeSpecimenBuilder(builders);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="IEnumerator{ISpecimenBuilder}" /> that can be used to
        /// iterate through the collection.
        /// </returns>
        public IEnumerator<ISpecimenBuilder> GetEnumerator()
        {
            return this.builders.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator" /> object that can
        /// be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        internal static ISpecimenBuilder ComposeIfMultiple(IEnumerable<ISpecimenBuilder> builders)
        {
            var isSingle = builders.Take(2).Count() == 1;
            if (isSingle)
                return builders.Single();

            return new CompositeSpecimenBuilder(builders);
        }

        internal static ISpecimenBuilderNode UnwrapIfSingle(ISpecimenBuilderNode node)
        {
            var c = node as CompositeSpecimenBuilder;
            if (c == null)
                return node;
            var isSingle = c.builders.Take(2).Count() == 1;
            if (isSingle)
            {
                var n = c.builders.Single() as ISpecimenBuilderNode;
                if (n != null)
                    return n;
            }
            return node;
        }
    }
}

{'fraction_non_alphanumeric': 0.10023332685203189, 'fraction_numerical': 0.0009721952167995333, 'mean_word_length': 1.8472183780791587, 'pattern_counts': {'":': 0, '<': 108, '<?xml version=': 0, '>': 108, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1685529', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2457, 'n_words': 742}