<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="EnumExtensions.cs" company="None">
//     (c) 2014-2015 <NAME>
// </copyright>
//-----------------------------------------------------------------------

namespace ReimuPlugins.Common
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;

    /// <summary>
    /// Provides some extension methods for enumeration types.
    /// </summary>
    public static class EnumExtensions
    {
        /// <summary>
        /// Gets a short name of the specified enumeration value.
        /// </summary>
        /// <typeparam name="T">The enumeration type.</typeparam>
        /// <param name="enumValue">An enumeration value.</param>
        /// <returns>A short name of <paramref name="enumValue"/>.</returns>
        [CLSCompliant(false)]
        public static string ToShortName<T>(this T enumValue)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            EnumAltNameAttribute attr;
            if (AttributeCache<T, EnumAltNameAttribute>.Cache.TryGetValue(enumValue, out attr))
                return attr.ShortName;
            else
                return string.Empty;
        }

        /// <summary>
        /// Gets a long name of the specified enumeration value.
        /// </summary>
        /// <typeparam name="T">The enumeration type.</typeparam>
        /// <param name="enumValue">An enumeration value.</param>
        /// <returns>A long name of <paramref name="enumValue"/>.</returns>
        [CLSCompliant(false)]
        public static string ToLongName<T>(this T enumValue)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            EnumAltNameAttribute attr;
            if (AttributeCache<T, EnumAltNameAttribute>.Cache.TryGetValue(enumValue, out attr))
                return attr.LongName;
            else
                return string.Empty;
        }

        /// <summary>
        /// Provides cache of attribute information.
        /// </summary>
        /// <typeparam name="TEnum">The enumeration type.</typeparam>
        /// <typeparam name="TAttribute">The attribute type for <typeparamref name="TEnum"/>.</typeparam>
        private static class AttributeCache<TEnum, TAttribute>
            where TEnum : struct, IComparable, IFormattable, IConvertible
            where TAttribute : Attribute
        {
            /// <summary>
            /// Caches attribute information collected by reflection.
            /// </summary>
            public static readonly Dictionary<TEnum, TAttribute> Cache = InitializeCache();

            /// <summary>
            /// Initializes the cache.
            /// </summary><commit_msg>Common: Fix to avoid SA1503<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="EnumExtensions.cs" company="None">
//     (c) 2014-2015 <NAME>
// </copyright>
//-----------------------------------------------------------------------

namespace ReimuPlugins.Common
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;

    /// <summary>
    /// Provides some extension methods for enumeration types.
    /// </summary>
    public static class EnumExtensions
    {
        /// <summary>
        /// Gets a short name of the specified enumeration value.
        /// </summary>
        /// <typeparam name="T">The enumeration type.</typeparam>
        /// <param name="enumValue">An enumeration value.</param>
        /// <returns>A short name of <paramref name="enumValue"/>.</returns>
        [CLSCompliant(false)]
        public static string ToShortName<T>(this T enumValue)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            EnumAltNameAttribute attr;
            return AttributeCache<T, EnumAltNameAttribute>.Cache.TryGetValue(enumValue, out attr)
                ? attr.ShortName : string.Empty;
        }

        /// <summary>
        /// Gets a long name of the specified enumeration value.
        /// </summary>
        /// <typeparam name="T">The enumeration type.</typeparam>
        /// <param name="enumValue">An enumeration value.</param>
        /// <returns>A long name of <paramref name="enumValue"/>.</returns>
        [CLSCompliant(false)]
        public static string ToLongName<T>(this T enumValue)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            EnumAltNameAttribute attr;
            return AttributeCache<T, EnumAltNameAttribute>.Cache.TryGetValue(enumValue, out attr)
                ? attr.LongName : string.Empty;
        }

        /// <summary>
        /// Provides cache of attribute information.
        /// </summary>
        /// <typeparam name="TEnum">The enumeration type.</typeparam>
        /// <typeparam name="TAttribute">The attribute type for <typeparamref name="TEnum"/>.</typeparam>
        private static class AttributeCache<TEnum, TAttribute>
            where TEnum : struct, IComparable, IFormattable, IConvertible
            where TAttribute : Attribute
        {
            /// <summary>
            /// Caches attribute information collected by reflection.
            /// </summary>
            public static readonly Dictionary<TEnum, TAttribute> Cache = InitializeCache();

            /// <summary>
            /// Initializes the cache.
            /// </summary>
{'fraction_non_alphanumeric': 0.16492052023121387, 'fraction_numerical': 0.0036127167630057803, 'mean_word_length': 1.7560975609756098, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, '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': '6852733', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1316, 'n_words': 363}