/
ValidationErrors.cs
158 lines (133 loc) · 4.4 KB
/
ValidationErrors.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace DevZest.Data
{
/// <summary>
/// Provides static methods to manipulate <see cref="IValidationErrors"/> object.
/// </summary>
public static class ValidationErrors
{
private class EmptyGroup : IValidationErrors
{
public static EmptyGroup Singleton = new EmptyGroup();
private EmptyGroup()
{
}
public int Count
{
get { return 0; }
}
public ValidationError this[int index]
{
get { throw new ArgumentOutOfRangeException(nameof(index)); }
}
public bool IsSealed
{
get { return true; }
}
public IValidationErrors Add(ValidationError value)
{
return value.VerifyNotNull(nameof(value));
}
public IValidationErrors Seal()
{
return this;
}
public IEnumerator<ValidationError> GetEnumerator()
{
return EmptyEnumerator<ValidationError>.Singleton;
}
IEnumerator IEnumerable.GetEnumerator()
{
return EmptyEnumerator<ValidationError>.Singleton;
}
}
private class ListGroup : IValidationErrors
{
private bool _isSealed;
private List<ValidationError> _list = new List<ValidationError>();
public ListGroup(ValidationError value1, ValidationError value2)
{
Debug.Assert(value1 != null && value2 != null);
Add(value1);
Add(value2);
}
private ListGroup()
{
}
public bool IsSealed
{
get { return _isSealed; }
}
public int Count
{
get { return _list.Count; }
}
public ValidationError this[int index]
{
get { return _list[index]; }
}
public IValidationErrors Seal()
{
_isSealed = true;
return this;
}
public IValidationErrors Add(ValidationError value)
{
value.VerifyNotNull(nameof(value));
if (!IsSealed)
{
_list.Add(value);
return this;
}
if (Count == 0)
return value;
else
{
var result = new ListGroup();
for (int i = 0; i < Count; i++)
result.Add(this[i]);
result.Add(value);
return result;
}
}
public IEnumerator<ValidationError> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _list.GetEnumerator();
}
}
/// <summary>
/// Gets an empty <see cref="IValidationErrors"/> object.
/// </summary>
public static IValidationErrors Empty
{
get { return EmptyGroup.Singleton; }
}
internal static IValidationErrors New(ValidationError value1, ValidationError value2)
{
Debug.Assert(value1 != null && value2 != null && value1 != value2);
return new ListGroup(value1, value2);
}
/// <summary>
/// Creates an <see cref="IValidationErrors"/> object from <see cref="ValidationError"/> objects.
/// </summary>
/// <param name="values">The <see cref="ValidationError"/> objects.</param>
/// <returns>The created <see cref="IValidationErrors"/> object.</returns>
public static IValidationErrors New(params ValidationError[] values)
{
values.VerifyNotNull(nameof(values));
if (values.Length == 0)
return Empty;
IValidationErrors result = values.VerifyNotNull(0, nameof(values));
for (int i = 1; i < values.Length; i++)
result = result.Add(values.VerifyNotNull(i, nameof(values)));
return result;
}
}
}