-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionCount.cs
47 lines (39 loc) · 1.57 KB
/
CollectionCount.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
namespace Cecil.AspectN
{
public struct CollectionCount
{
private const int _ANY = -1;
private const int _ONE_PLUS = -2;
public static readonly CollectionCount ANY = new(_ANY);
public static readonly CollectionCount ZERO = new(0);
public static readonly CollectionCount Single = new(1);
public static readonly CollectionCount OnePlus = new(_ONE_PLUS);
public CollectionCount(int count)
{
Value = count;
}
public int Value { get; }
public static implicit operator CollectionCount(int count) => new(count);
public static bool operator ==(CollectionCount count1, CollectionCount count2)
{
if (count1.Value == _ANY || count2.Value == _ANY) return true;
if (count1.Value == _ONE_PLUS && count2.Value >= 1 || count2.Value == _ONE_PLUS && count1.Value >= 1) return true;
return count1.Value == count2.Value;
}
public static bool operator !=(CollectionCount count1, CollectionCount count2)
{
if (count1.Value == _ANY || count2.Value == _ANY) return false;
if (count1.Value == _ONE_PLUS && count2.Value >= 1 || count2.Value == _ONE_PLUS && count1.Value >= 1) return false;
return count1.Value != count2.Value;
}
public override bool Equals(object obj)
{
if (obj is not CollectionCount cc) return false;
return this == cc;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
}