using System; using System.Collections.Generic; using System.Linq; using Swan.Collections; namespace Swan { /// <summary> /// Provide Enumerations helpers with internal cache. /// </summary> public class EnumHelper : SingletonBase<CollectionCacheRepository<Tuple<String, Object>>> { /// <summary> /// Gets all the names and enumerators from a specific Enum type. /// </summary> /// <typeparam name="T">The type of the attribute to be retrieved.</typeparam> /// <returns>A tuple of enumerator names and their value stored for the specified type.</returns> public static IEnumerable<Tuple<String, Object>> Retrieve<T>() where T : struct, IConvertible => Instance.Retrieve(typeof(T), t => Enum.GetValues(t).Cast<Object>().Select(item => Tuple.Create(Enum.GetName(t, item), item))); /// <summary> /// Gets the cached items with the enum item value. /// </summary> /// <typeparam name="T">The type of enumeration.</typeparam> /// <param name="humanize">if set to <c>true</c> [humanize].</param> /// <returns> /// A collection of Type/Tuple pairs /// that represents items with the enum item value. /// </returns> public static IEnumerable<Tuple<Int32, String>> GetItemsWithValue<T>(Boolean humanize = true) where T : struct, IConvertible => Retrieve<T>().Select(x => Tuple.Create((Int32)x.Item2, humanize ? x.Item1.Humanize() : x.Item1)); /// <summary> /// Gets the flag values. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">The value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <returns> /// A list of values in the flag. /// </returns> public static IEnumerable<Int32> GetFlagValues<TEnum>(Int32 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Int32)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x); /// <summary> /// Gets the flag values. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">The value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <returns> /// A list of values in the flag. /// </returns> public static IEnumerable<Int64> GetFlagValues<TEnum>(Int64 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Int64)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x); /// <summary> /// Gets the flag values. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">The value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <returns> /// A list of values in the flag. /// </returns> public static IEnumerable<Byte> GetFlagValues<TEnum>(Byte value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Byte)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x); /// <summary> /// Gets the flag names. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">the value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <param name="humanize">if set to <c>true</c> [humanize].</param> /// <returns> /// A list of flag names. /// </returns> public static IEnumerable<String> GetFlagNames<TEnum>(Int32 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Int32)f.Item2 != 0)).Where(x => ((Int32)x.Item2 & value) == (Int32)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1); /// <summary> /// Gets the flag names. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">The value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <param name="humanize">if set to <c>true</c> [humanize].</param> /// <returns> /// A list of flag names. /// </returns> public static IEnumerable<String> GetFlagNames<TEnum>(Int64 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Int64)f.Item2 != 0)).Where(x => ((Int64)x.Item2 & value) == (Int64)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1); /// <summary> /// Gets the flag names. /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="value">The value.</param> /// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param> /// <param name="humanize">if set to <c>true</c> [humanize].</param> /// <returns> /// A list of flag names. /// </returns> public static IEnumerable<String> GetFlagNames<TEnum>(Byte value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Byte)f.Item2 != 0)).Where(x => ((Byte)x.Item2 & value) == (Byte)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1); /// <summary> /// Gets the cached items with the enum item index. /// </summary> /// <typeparam name="T">The type of enumeration.</typeparam> /// <param name="humanize">if set to <c>true</c> [humanize].</param> /// <returns> /// A collection of Type/Tuple pairs that represents items with the enum item value. /// </returns> public static IEnumerable<Tuple<Int32, String>> GetItemsWithIndex<T>(Boolean humanize = true) where T : struct, IConvertible { Int32 i = 0; return Retrieve<T>().Select(x => Tuple.Create(i++, humanize ? x.Item1.Humanize() : x.Item1)); } } }