<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationConfigurationBase.cs" company="OBeautifulCode">
//   Copyright (c) OBeautifulCode 2018. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace OBeautifulCode.Serialization
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using OBeautifulCode.Assertion.Recipes;
    using OBeautifulCode.Type.Recipes;

    using static System.FormattableString;

    /// <summary>
    /// Common configuration base across all kinds of serialization.
    /// </summary>
    public abstract partial class SerializationConfigurationBase
    {
        private readonly object syncConfigure = new object();

        private readonly Dictionary<Type, RegistrationDetails> registeredTypeToRegistrationDetailsMap = new Dictionary<Type, RegistrationDetails>();

        private bool initialized;

        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationConfigurationBase"/> class.
        /// </summary>
        protected SerializationConfigurationBase()
        {
            this.SerializationConfigurationType = this.GetType().ToSerializationConfigurationType();
        }

        /// <summary>
        /// Gets a map of the dependent (all ancestors) serialization configuration types to their initialized instance.
        /// </summary>
        public IReadOnlyDictionary<SerializationConfigurationType, SerializationConfigurationBase> DependentSerializationConfigurationTypeToInstanceMap { get; private set; }

        /// <summary>
        /// Gets all specified dependent configuration types, including all internal configuration types
        /// unless this this configuration type is <see cref="IIgnoreDefaultDependencies"/>.
        /// </summary>
        public IReadOnlyCollection<SerializationConfigurationType> DependentSerializationConfigurationTypesWithDefaultsIfApplicable => this.GetDependentSerializationConfigurationTypesWithDefaultsIfApplicable();

        /// <summary>
        /// Gets a map of registered type to the type of serialization configuration that registered that type.
        /// </summary>
        protected IReadOnlyDictionary<Type, RegistrationDetails> RegisteredTypeToRegistrationDetailsMap => this.registeredTypeToRegistrationDetailsMap;

        /// <summary>
        /// Gets the serialization configuration type of the current instance.
        /// </summary>
        protected SerializationConfigurationType SerializationConfigurationType { get; }

        /// <summary>
        /// Run configuration logic.
        /// </summary>
        /// <param name="dependentSerializationConfigurationTypeToInstanceMap">Map of dependent configuration type to configured instance.</param>
        public void Initialize(
            IReadOnlyDictionary<SerializationConfigurationType, SerializationConfigurationBase> dependentSerializationConfigurationTypeToInstanceMap)
        {
            new { dependentSerializationConfigurationTypeToInstanceMap }.AsArg().Must().NotBeNull().And().NotContainAnyKeyValuePairsWithNullValue();

            if (!this.initialized)
            {
                lock (this.syncConfigure)
                {
                    if (!this.initialized)
                    {
                        this.DependentSerializationConfigurationTypeToInstanceMap = dependentSerializationConfigurationTypeToInstanceMap;

                        this.RegisterTypesFromDependentSerializationConfigurations();

                        var typesToRegister = new Queue<TypeToRegister>(this.TypesToRegister ?? new TypeToRegister[0]);

                        this.ProcessTypesToRegister(typesToRegister);

                        this.FinalizeInitialization();

                        this.initialized = true;
                    }
                }
            }
        }

        /// <summary>
        /// Determines if the specified type has been registered.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        /// true if the type has been registered, otherwise false.
        /// </returns>
        public bool IsRegisteredType(
            Type type)
        {
            new { type }.AsArg().Must().NotBeNull();
            new { type.ContainsGenericParameters }.AsArg().Must().BeFalse();

            var result = this.registeredTypeToRegistrationDetailsMap.ContainsKey(type);

            return result;
        }

        private IReadOnlyCollection<SerializationConfigurationType> GetDependentSerializationConfigurationTypesWithDefaultsIfApplicable()
        {
            var result = this.DependentSerializationConfigurationTypes.ToList();

            if (!(this is IIgnoreDefaultDependencies))
            {
                result.AddRange(this.DefaultDependentSerializationConfigurationTypes);
            }

            return result;
        }
<commit_msg>IsRegisteredType now supported Generics<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationConfigurationBase.cs" company="OBeautifulCode">
//   Copyright (c) OBeautifulCode 2018. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace OBeautifulCode.Serialization
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using OBeautifulCode.Assertion.Recipes;
    using OBeautifulCode.Type.Recipes;

    using static System.FormattableString;

    /// <summary>
    /// Common configuration base across all kinds of serialization.
    /// </summary>
    public abstract partial class SerializationConfigurationBase
    {
        private readonly object syncConfigure = new object();

        private readonly Dictionary<Type, RegistrationDetails> registeredTypeToRegistrationDetailsMap = new Dictionary<Type, RegistrationDetails>();

        private bool initialized;

        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationConfigurationBase"/> class.
        /// </summary>
        protected SerializationConfigurationBase()
        {
            this.SerializationConfigurationType = this.GetType().ToSerializationConfigurationType();
        }

        /// <summary>
        /// Gets a map of the dependent (all ancestors) serialization configuration types to their initialized instance.
        /// </summary>
        public IReadOnlyDictionary<SerializationConfigurationType, SerializationConfigurationBase> DependentSerializationConfigurationTypeToInstanceMap { get; private set; }

        /// <summary>
        /// Gets all specified dependent configuration types, including all internal configuration types
        /// unless this this configuration type is <see cref="IIgnoreDefaultDependencies"/>.
        /// </summary>
        public IReadOnlyCollection<SerializationConfigurationType> DependentSerializationConfigurationTypesWithDefaultsIfApplicable => this.GetDependentSerializationConfigurationTypesWithDefaultsIfApplicable();

        /// <summary>
        /// Gets a map of registered type to the type of serialization configuration that registered that type.
        /// </summary>
        protected IReadOnlyDictionary<Type, RegistrationDetails> RegisteredTypeToRegistrationDetailsMap => this.registeredTypeToRegistrationDetailsMap;

        /// <summary>
        /// Gets the serialization configuration type of the current instance.
        /// </summary>
        protected SerializationConfigurationType SerializationConfigurationType { get; }

        /// <summary>
        /// Run configuration logic.
        /// </summary>
        /// <param name="dependentSerializationConfigurationTypeToInstanceMap">Map of dependent configuration type to configured instance.</param>
        public void Initialize(
            IReadOnlyDictionary<SerializationConfigurationType, SerializationConfigurationBase> dependentSerializationConfigurationTypeToInstanceMap)
        {
            new { dependentSerializationConfigurationTypeToInstanceMap }.AsArg().Must().NotBeNull().And().NotContainAnyKeyValuePairsWithNullValue();

            if (!this.initialized)
            {
                lock (this.syncConfigure)
                {
                    if (!this.initialized)
                    {
                        this.DependentSerializationConfigurationTypeToInstanceMap = dependentSerializationConfigurationTypeToInstanceMap;

                        this.RegisterTypesFromDependentSerializationConfigurations();

                        var typesToRegister = new Queue<TypeToRegister>(this.TypesToRegister ?? new TypeToRegister[0]);

                        this.ProcessTypesToRegister(typesToRegister);

                        this.FinalizeInitialization();

                        this.initialized = true;
                    }
                }
            }
        }

        /// <summary>
        /// Determines if the specified type has been registered.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        /// true if the type has been registered, otherwise false.
        /// </returns>
        public bool IsRegisteredType(
            Type type)
        {
            new { type }.AsArg().Must().NotBeNull();
            new { type.ContainsGenericParameters }.AsArg().Must().BeFalse();

            bool result;

            if (this.registeredTypeToRegistrationDetailsMap.ContainsKey(type))
            {
                result = true;
            }
            else if (type.IsGenericType)
            {
                var genericTypeDefinition = type.GetGenericTypeDefinition();

                result = this.registeredTypeToRegistrationDetailsMap.ContainsKey(genericTypeDefinition) && type.GenericTypeArguments.All(this.IsRegisteredType);
            }
            else
            {
                result = false;
            }

            return result;
        }

        private IReadOnlyCollection<SerializationConfigurationType> GetDependentSerializationConfigurationTypesWithDefaultsIfApplicable()
        {
            var result = this.DependentSerializationConfigurationTypes.ToList();

            if (!(this is IIgnoreDefaultDependencies))
            {
                result.AddRange(this.DefaultDependentSerializationConfigurationTypes);
            }

            return result;
        }

{'fraction_non_alphanumeric': 0.12293320878094348, 'fraction_numerical': 0.0009341429238673517, 'mean_word_length': 2.090646651270208, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 75, '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': '5716863', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2177, 'n_words': 530}