<commit_before>﻿using System;
using System.Collections.Generic;
using System.Reflection;
using static yocto.Preconditions;
// ReSharper disable SuggestVarOrType_BuiltInTypes

namespace yocto
{
    internal class Constructor
    {
        private readonly ConstructorInfo _constructorInfo;
        private readonly List<IInstanceFactory> _paramFactories;
        private readonly Func<object> _factory;

        public Constructor(IContainer container, Type implementationType, Func<object> factory)
        {
            CheckIsNotNull(nameof(container), container);
            CheckIsNotNull(nameof(implementationType), implementationType);

            if (factory != null)
            {
                _factory = factory;
                return;
            }

            var validConstructors = GetValidConstructors(container, implementationType);

            int numOfValidConstructors = validConstructors.Count;

            switch (numOfValidConstructors)
            {
                case 0:
                    throw new Exception($"Could not find a constructor to create the type. [{implementationType.Name}]");
                case 1:
                    _constructorInfo = validConstructors[0];
                    _paramFactories = GetParameterFactories(container, _constructorInfo);
                    break;
                default:
                    throw new Exception($"Found more than one constructor to create the type. [{implementationType.Name}]");
            }
        }

        public T Create<T>() where T : class
        {
            if (_factory == null)
                return CreateUsingConstructor<T>();

            return (T) _factory();
        }

        private T CreateUsingConstructor<T>() where T : class
        {
            var parameters = _constructorInfo.GetParameters();
            var paramObjects = new List<object>(parameters.Length);

            for (int i = 0; i < parameters.Length; i++)
            {
                var parameterFactory = _paramFactories[i];
                var o = parameterFactory.Create<object>();

                paramObjects.Add(o);
            }

            return (T) _constructorInfo.Invoke(paramObjects.ToArray());<commit_msg>Updated Constructor<commit_after>﻿using System;
using System.Collections.Generic;
using System.Reflection;
using static yocto.Preconditions;
// ReSharper disable SuggestVarOrType_BuiltInTypes

namespace yocto
{
    internal class Constructor
    {
        private readonly IContainer _container;
        private readonly ConstructorInfo _constructorInfo;
        private readonly Func<object> _factory;

        public Constructor(IContainer container, Type implementationType, Func<object> factory)
        {
            CheckIsNotNull(nameof(container), container);
            CheckIsNotNull(nameof(implementationType), implementationType);

            if (factory != null)
            {
                _factory = factory;
                return;
            }

            var validConstructors = GetValidConstructors(container, implementationType);

            int numOfValidConstructors = validConstructors.Count;

            switch (numOfValidConstructors)
            {
                case 0:
                    throw new Exception($"Could not find a constructor to create the type. [{implementationType.Name}]");
                case 1:
                    _constructorInfo = validConstructors[0];
                    _container = container;
                    break;
                default:
                    throw new Exception($"Found more than one constructor to create the type. [{implementationType.Name}]");
            }
        }

        public T Create<T>() where T : class
        {
            if (_factory == null)
                return CreateUsingConstructor<T>();

            return (T) _factory();
        }

        private T CreateUsingConstructor<T>() where T : class
        {
            var parameters = _constructorInfo.GetParameters();
            var paramObjects = new List<object>(parameters.Length);

            var paramFactories = GetParameterFactories(_container, _constructorInfo);

            for (int i = 0; i < parameters.Length; i++)
            {
                var parameterFactory = paramFactories[i];
                var o = parameterFactory.Create<object>();

                paramObjects.Add(o);
            }

            return (T) _constructorInfo.Invoke(paramObjects.ToArray());
{'fraction_non_alphanumeric': 0.07831729693443723, 'fraction_numerical': 0.0017901096442157081, 'mean_word_length': 1.9504950495049505, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, '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': '5545062', 'n_tokens_mistral': 1054, 'n_tokens_neox': 992, 'n_words': 271}