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

using ComputersFactory.Data.SalesReports.Converters.Contracts;

namespace ComputersFactory.Data.SalesReports.Converters
{
    public class ModelConverter : IModelConverter
    {
        public IList<TModelOut> Convert<TModelIn, TModelOut>(IEnumerable<TModelIn> inputData)
            where TModelOut : new()
        {
            if (inputData == null)
            {
                throw new ArgumentNullException(nameof(inputData));
            }

            var modelInProperties = this.GetCommonProperties(typeof(TModelIn), typeof(TModelOut));
            var modelOutProperties = this.GetCommonProperties(typeof(TModelOut), typeof(TModelIn));

            var resultingCollection = this.InitializeCollection<TModelOut>();
            foreach (var item in inputData)
            {
                var nextModelOutInstance = new TModelOut();
                foreach (var property in modelInProperties)
                {
                    var matchingModelOutProperty = modelOutProperties
                        .Where(prop => prop.Name == property.Name)
                        .FirstOrDefault();

                    if (matchingModelOutProperty == null)
                    {
                        throw new ArgumentNullException(nameof(matchingModelOutProperty));
                    }

                    try
                    {
                        var propertyValue = property.GetValue(item);
                        matchingModelOutProperty.SetValue(nextModelOutInstance, propertyValue);
                    }
                    catch (ArgumentException)
                    {
                        // It's a collection.
                    }
                }

                resultingCollection.Add(nextModelOutInstance);
            }

            return resultingCollection;
        }

        private IList<T> InitializeCollection<T>()
        {
            return new List<T>();
        }

        private IEnumerable<PropertyInfo> GetCommonProperties(Type getPropertiesFromType, Type commonPropertiesWithType)
        {
            var secondPropertiesNames = commonPropertiesWithType.GetProperties().Select(prop => prop.Name);

            var firstProperties = getPropertiesFromType.GetProperties()
                .Where(prop => secondPropertiesNames.Contains(prop.Name))
                .OrderBy(prop => prop.Name);

            return firstProperties;
        }
    }
}<commit_msg>changed identifier names<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using ComputersFactory.Data.SalesReports.Converters.Contracts;

namespace ComputersFactory.Data.SalesReports.Converters
{
    public class ModelConverter : IModelConverter
    {
        public IList<TModelOut> Convert<TModelIn, TModelOut>(IEnumerable<TModelIn> inputData)
            where TModelOut : new()
        {
            if (inputData == null)
            {
                throw new ArgumentNullException(nameof(inputData));
            }

            var modelInProperties = this.GetCommonPropertiesInfo(typeof(TModelIn), typeof(TModelOut));
            var modelOutProperties = this.GetCommonPropertiesInfo(typeof(TModelOut), typeof(TModelIn));

            var resultingCollection = this.InitializeCollection<TModelOut>();
            foreach (var modelInInstance in inputData)
            {
                var nextModelOutInstance = new TModelOut();
                foreach (var property in modelInProperties)
                {
                    var matchingModelOutProperty = modelOutProperties
                        .Where(prop => prop.Name == property.Name)
                        .FirstOrDefault();

                    if (matchingModelOutProperty == null)
                    {
                        throw new ArgumentNullException(nameof(matchingModelOutProperty));
                    }

                    try
                    {
                        var propertyValue = property.GetValue(modelInInstance);
                        matchingModelOutProperty.SetValue(nextModelOutInstance, propertyValue);
                    }
                    catch (ArgumentException)
                    {
                        // It's a collection.
                    }
                }

                resultingCollection.Add(nextModelOutInstance);
            }

            return resultingCollection;
        }

        private IList<T> InitializeCollection<T>()
        {
            return new List<T>();
        }

        private IEnumerable<PropertyInfo> GetCommonPropertiesInfo(Type getPropertiesFromType, Type commonPropertiesWithType)
        {
            var secondPropertiesNames = commonPropertiesWithType.GetProperties().Select(prop => prop.Name);

            var firstProperties = getPropertiesFromType.GetProperties()
                .Where(prop => secondPropertiesNames.Contains(prop.Name))
                .OrderBy(prop => prop.Name);

            return firstProperties;
        }
    }
}
{'fraction_non_alphanumeric': 0.07488727700450892, 'fraction_numerical': 0.0, 'mean_word_length': 1.74744211093161, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, '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': '2018814', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1108, 'n_words': 216}