<commit_before>﻿using System;
using System.Collections.Concurrent;

namespace MicroORM.Mapping
{
    internal static class TableInfoContainer
    {
        private static ConcurrentDictionary<Type, TableInfo> _mappings = new ConcurrentDictionary<Type, TableInfo>();
        private static ConcurrentDictionary<Type, Type> _interfacePersistents = new ConcurrentDictionary<Type, Type>();
        private static TableInfo _lastMapping;

        /// <summary>
        /// Returns the mapping for a given object. If the mapping does not exist it is created by this routine.
        /// </summary>
        /// <param name="obj">The object the mapping is returned.</param>
        public static TableInfo GetTableInfo(object obj)
        {
            if (obj == null) throw new ArgumentNullException("obj");

            return GetTableInfo(obj.GetType());
        }

        /// <summary>
        /// Returns the mapping for the given persistent type. If the mapping does not exist it is 
        /// created by this routine.
        /// </summary>
        /// <param name="type">Type of object the mapping is returned.</param>
        public static TableInfo GetTableInfo(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");

            if (type == typeof(string) || type.IsValueType || type.IsEnum) return null;

            // Get the real persistent type.
            type = GetPersistentType(type);

            TableInfo tableInfo = null;

            if (_lastMapping != null && _lastMapping.PersistentType == type) return _lastMapping;

            if (!_mappings.TryGetValue(type, out tableInfo))
            {
                tableInfo = TableInfoBuilder.CreateTypeMapping(type);
                _mappings.TryAdd(type, tableInfo);
            }

            return _lastMapping = tableInfo;
        }

        /// <summary>
        /// Gets the persistent type from the given type.
        /// </summary>
        /// <param name="type">The type that's persistent type is returned.</param>
        public static Type GetPersistentType(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            // If not an interface return the given type.
            if (!type.IsInterface)
                return type;

            // Get the persistent type for the interface.
            Type t = null;
            if (_interfacePersistents.TryGetValue(type, out t))
                return t;

            // Throw an exception if the interface is not registered with a persistent.
            throw new TableInfoException(string.Format("There is no persistent type registered for the interface type: {0}.", type.FullName));
        }
    }
}<commit_msg>refactored.<commit_after>﻿using System;
using System.Collections.Concurrent;

namespace MicroORM.Mapping
{
    internal static class TableInfoContainer
    {
        private static ConcurrentDictionary<Type, TableInfo> _mappings = new ConcurrentDictionary<Type, TableInfo>();
        private static ConcurrentDictionary<Type, Type> _interfacePersistents = new ConcurrentDictionary<Type, Type>();
        private static TableInfo _lastMapping;

        /// <summary>
        /// Returns the mapping for a given object. If the mapping does not exist it is created by this routine.
        /// </summary>
        /// <param name="entity">The object the mapping is returned.</param>
        public static TableInfo GetTableInfo(object entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");

            return GetTableInfo(entity.GetType());
        }

        /// <summary>
        /// Returns the mapping for the given persistent type. If the mapping does not exist it is 
        /// created by this routine.
        /// </summary>
        /// <param name="entityType">Type of object the mapping is returned.</param>
        public static TableInfo GetTableInfo(Type entityType)
        {
            if (entityType == null) throw new ArgumentNullException("entityType");

            if (entityType == typeof(string)
                || entityType.IsValueType
                || entityType.IsEnum) { return null; }

            // Get the real persistent type.
            entityType = GetPersistentType(entityType);

            if (_lastMapping != null
                && _lastMapping.PersistentType == entityType) { return _lastMapping; }

            TableInfo tableInfo = null;
            if (!_mappings.TryGetValue(entityType, out tableInfo))
            {
                tableInfo = TableInfoBuilder.CreateTypeMapping(entityType);
                _mappings.TryAdd(entityType, tableInfo);
            }

            return _lastMapping = tableInfo;
        }

        /// <summary>
        /// Gets the persistent type from the given type.
        /// </summary>
        /// <param name="entityType">The type that's persistent type is returned.</param>
        public static Type GetPersistentType(Type entityType)
        {
            if (entityType == null)
                throw new ArgumentNullException("entityType");

            // If not an interface return the given type.
            if (!entityType.IsInterface) return entityType;

            // Get the persistent type for the interface.
            Type interfaceType = null;
            if (_interfacePersistents.TryGetValue(entityType, out interfaceType)) return interfaceType;

            // Throw an exception if the interface is not registered with a persistent.
            throw new TableInfoException(string.Format("There is no persistent type registered for the interface type: {0}.", entityType.FullName));
        }
    }
}
{'fraction_non_alphanumeric': 0.09082923401264933, 'fraction_numerical': 0.00035137034434293746, 'mean_word_length': 2.122874382885354, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '4216105', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1315, 'n_words': 460}