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