Skip to content

Instantly share code, notes, and snippets.

@uzzu
Created December 22, 2014 15:26
Show Gist options
  • Save uzzu/5412336c0532508dd17c to your computer and use it in GitHub Desktop.
Save uzzu/5412336c0532508dd17c to your computer and use it in GitHub Desktop.
Enumeration.cs
using System;
using System.Collections.Generic;
using System.Reflection;
public abstract class Enumeration : IComparable
{
#region Properties
public int Value { get { return value; } }
public string Name { get { return name; } }
public bool IsDefault { get { return isDefault; } }
#endregion
#region Fields
readonly int value;
readonly string name;
readonly bool isDefault;
#endregion
#region Constructor
protected Enumeration(int value, string name) : this(value, name, false)
{
}
protected Enumeration(int value, string name, bool isDefault)
{
this.value = value;
this.name = name;
this.isDefault = isDefault;
}
#endregion
#region Collection
public static List<TEnum> Of<TEnum>(TEnum enumeration, params TEnum[] moreEnumerations) where TEnum : Enumeration
{
var results = new List<TEnum>();
results.AddUnique(enumeration, moreEnumerations);
return results;
}
public static List<TEnum> All<TEnum>() where TEnum : Enumeration
{
var enumerations = new List<TEnum>();
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var field in fields)
{
var enumeration = field.GetValue(null) as TEnum;
if (enumeration == null)
{
continue;
}
enumerations.Add(enumeration);
}
return enumerations;
}
#endregion
#region Parse Value
public static TEnum FromValue<TEnum>(int value) where TEnum : Enumeration
{
return FromValue<TEnum>(value, Default<TEnum>());
}
public static TEnum FromValue<TEnum>(int value, TEnum defaultValue) where TEnum : Enumeration
{
return Parse<TEnum>(enumeration => value == enumeration.Value, defaultValue);
}
public static TEnum FromValueStrictly<TEnum>(int value) where TEnum : Enumeration
{
// SingleOrDefault
var result = default(TEnum);
foreach (var enumeration in All<TEnum>())
{
if (value == enumeration.Value)
{
result = enumeration;
break;
}
}
if (result == default(TEnum))
{
throw new ArgumentException(string.Format("Failed to parse {0}. The value was {1}", typeof(TEnum).FullName, value));
}
return result;
}
#endregion
#region Parse Name
public static TEnum FromName<TEnum>(string name) where TEnum : Enumeration
{
return FromName<TEnum>(name, Default<TEnum>());
}
public static TEnum FromName<TEnum>(string name, TEnum defaultValue) where TEnum : Enumeration
{
return Parse<TEnum>(enumeration => name == enumeration.Name, defaultValue);
}
public static TEnum FromNameStrictly<TEnum>(string name) where TEnum : Enumeration
{
// SingleOrDefault
var result = default(TEnum);
foreach (var enumeration in All<TEnum>())
{
if (name == enumeration.Name)
{
result = enumeration;
break;
}
}
if (result == default(TEnum))
{
throw new ArgumentException(string.Format("Failed to parse {0}. The name was {1}", typeof(TEnum).FullName, name));
}
return result;
}
#endregion
#region Utilities
protected static TEnum Parse<TEnum>(Predicate<TEnum> predicate, TEnum defaultValue) where TEnum : Enumeration
{
var result = defaultValue;
foreach (var enumeration in All<TEnum>())
{
if (predicate(enumeration))
{
result = enumeration;
break;
}
}
return result;
}
protected static TEnum Default<TEnum>() where TEnum : Enumeration
{
var defaultValue = default(TEnum);
var enumerations = All<TEnum>();
foreach (var enumeration in enumerations)
{
if (enumeration.IsDefault)
{
defaultValue = enumeration;
break;
}
}
return defaultValue;
}
#endregion
#region Public Methods
public bool IsAnyOf(Enumeration enumeration, params Enumeration[] moreEnumerations)
{
if (enumeration != null)
{
if (Equals(enumeration))
{
return true;
}
}
if (moreEnumerations != null && moreEnumerations.Length > 0)
{
for (var i = 0; i < moreEnumerations.Length; i++)
{
var moreEnumeration = moreEnumerations[i];
if (moreEnumeration == null)
{
continue;
}
if (Equals(moreEnumeration))
{
return true;
}
}
}
return false;
}
public override bool Equals(object that)
{
if (ReferenceEquals(this, that))
{
return true;
}
if (that == null)
{
return false;
}
var thatEnum = that as Enumeration;
if (thatEnum == null)
{
return false;
}
return Value.Equals(thatEnum.Value)
&& GetType().Equals(that.GetType());
}
public virtual int CompareTo(object that)
{
if (ReferenceEquals(this, that))
{
return 0;
}
if (that == null)
{
return 1;
}
var thatEnum = that as Enumeration;
if (thatEnum == null)
{
return 1;
}
return Value.CompareTo(thatEnum.Value);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Name;
}
#endregion
#region Implicit Operator
public static implicit operator int(Enumeration enumeration)
{
return enumeration.Value;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment