-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumeration.cs
190 lines (157 loc) · 5.92 KB
/
Enumeration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace TheAppsPajamas.Shared.Types
{
[DebuggerDisplay("{DisplayName} - {Value}")]
public abstract class Enumeration<TEnumeration> : Enumeration<TEnumeration, int>
where TEnumeration : Enumeration<TEnumeration>
{
protected Enumeration(int value, string displayName)
: base(value, displayName)
{
}
public static TEnumeration FromInt32(int value)
{
return FromValue(value);
}
public static bool TryFromInt32(int listItemValue, out TEnumeration result)
{
return TryParse(listItemValue, out result);
}
}
[DebuggerDisplay("{DisplayName} - {Value}")]
public abstract class Enumeration<TEnumeration, TValue> : IComparable<TEnumeration>, IEquatable<TEnumeration>
where TEnumeration : Enumeration<TEnumeration, TValue>
where TValue : IComparable
{
private static readonly Lazy<TEnumeration[]> Enumerations = new Lazy<TEnumeration[]>(GetEnumerations);
readonly string _displayName;
TValue _value;
protected Enumeration(TValue value, string displayName)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_value = value;
_displayName = displayName;
}
public TValue Value
{
get { return _value; }
}
public string DisplayName
{
get { return _displayName; }
}
public int CompareTo(TEnumeration other)
{
return Value.CompareTo(other == default(TEnumeration) ? default(TValue) : other.Value);
}
public override sealed string ToString()
{
return DisplayName;
}
public static TEnumeration[] GetAll()
{
return Enumerations.Value;
}
private static TEnumeration[] GetEnumerations()
{
Type enumerationType = typeof(TEnumeration);
return enumerationType
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(info => enumerationType.IsAssignableFrom(info.FieldType))
.Select(info => info.GetValue(null))
.Cast<TEnumeration>()
.ToArray();
}
[Conditional("DEBUG")]
public static void TestAll()
{
var enumerations = GetAll();
var possibleDuplicates = enumerations.GroupBy(x => x.Value)
.Select(x => new {
Count = x.Count(),
Value = x.Key
})
.OrderByDescending(x => x.Count);
var duplicates = possibleDuplicates.Where(x => x.Count > 1).ToList();
if (duplicates.Any())
{
var firstDuplicate = duplicates.FirstOrDefault();
throw new Exception($"Duplicated Value found Enumeration Type {enumerations.FirstOrDefault().GetType().FullName} for Value {firstDuplicate.Value}");
}
}
public static IEnumerable<TTypeOut> GetAllOf<TTypeOut>()
{
Type enumerationType = typeof(TEnumeration);
Type enumerationTypeOut = typeof(TTypeOut);
return enumerationType
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(info => enumerationTypeOut.IsAssignableFrom(info.FieldType))
.Select(info => info.GetValue(null))
.Cast<TTypeOut>();
}
public override bool Equals(object obj)
{
return Equals(obj as TEnumeration);
}
public bool Equals(TEnumeration other)
{
return other != null && ValueEquals(other.Value);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static bool operator ==(Enumeration<TEnumeration, TValue> left, Enumeration<TEnumeration, TValue> right)
{
return Equals(left, right);
}
public static bool operator !=(Enumeration<TEnumeration, TValue> left, Enumeration<TEnumeration, TValue> right)
{
return !Equals(left, right);
}
public static TEnumeration FromValue(TValue value)
{
return Parse(value, "value", item => item.Value.Equals(value));
}
public static TEnumeration Parse(string displayName)
{
return Parse(displayName, "display name", item => item.DisplayName == displayName);
}
static bool TryParse(Func<TEnumeration, bool> predicate, out TEnumeration result)
{
result = GetAll().FirstOrDefault(predicate);
return result != null;
}
private static TEnumeration Parse(object value, string description, Func<TEnumeration, bool> predicate)
{
TEnumeration result;
if (!TryParse(predicate, out result))
{
string message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(TEnumeration));
throw new ArgumentException(message, "value");
}
return result;
}
public static bool TryParse(TValue value, out TEnumeration result)
{
return TryParse(e => e.ValueEquals(value), out result);
}
public static bool TryParse(string displayName, out TEnumeration result)
{
return TryParse(e => e.DisplayName == displayName, out result);
}
protected virtual bool ValueEquals(TValue value)
{
return Value.Equals(value);
}
}
}