-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeSpanGenerator.cs
277 lines (254 loc) · 13.1 KB
/
TimeSpanGenerator.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
namespace Peddler {
/// <summary>
/// A generator for values of type <see cref="TimeSpan" />. Depending on the
/// constructor used, the generator can either utilize a range of all possible
/// <see cref="TimeSpan" /> values or a constrained, smaller range.
/// </summary>
/// <remarks>
/// The range of possible <see cref="TimeSpan" /> values will always be contiguous.
/// Each <see cref="TimeSpan" /> is considered distinct from another by
/// the number of ticks used to represent its value.
/// </remarks>
public class TimeSpanGenerator : IIntegralGenerator<TimeSpan> {
private static EqualityComparer<TimeSpan> defaultEqualityComparer { get; }
private static Comparer<TimeSpan> defaultComparer { get; }
static TimeSpanGenerator() {
defaultEqualityComparer = EqualityComparer<TimeSpan>.Default;
defaultComparer = Comparer<TimeSpan>.Default;
}
private Int64Generator tickGenerator { get; }
/// <summary>
/// Inclusively, the minimum possible <see cref="TimeSpan" />
/// that can be created by this <see cref="TimeSpanGenerator" />.
/// </summary>
/// <remarks>
/// As a <see cref="TimeSpan" /> is represented by a number of ticks,
/// this can be negative.
/// </remarks>
public TimeSpan Low { get; }
/// <summary>
/// Exclusively, the maximum possible <see cref="TimeSpan" />
/// that can be created by this <see cref="TimeSpanGenerator" />.
/// </summary>
/// <remarks>
/// As a <see cref="TimeSpan" /> is represented by a number of ticks,
/// this can be negative.
/// </remarks>
public TimeSpan High { get; }
/// <summary>
/// The comparer used to determine if two <see cref="TimeSpan" />
/// instances are equal in value.
/// </summary>
public IEqualityComparer<TimeSpan> EqualityComparer { get; } = defaultEqualityComparer;
/// <summary>
/// The comparison used to determine if one <see cref="TimeSpan" />
/// instance is earlier than, the same time as, or later than another
/// <see cref="TimeSpan" /> instance.
/// </summary>
public IComparer<TimeSpan> Comparer { get; } = defaultComparer;
/// <summary>
/// Instantiates a <see cref="TimeSpanGenerator" /> that can create
/// <see cref="TimeSpan" /> values which range in ticks from
/// <see cref="TimeSpan.Zero" /> to <see cref="TimeSpan.MaxValue" />.
/// </summary>
public TimeSpanGenerator() :
this(TimeSpan.Zero, TimeSpan.MaxValue) {}
/// <summary>
/// Instantiates a <see cref="TimeSpanGenerator" /> that can create
/// <see cref="TimeSpan" /> values which range in ticks from
/// <paramref name="low" /> to <see cref="TimeSpan.MaxValue" />.
/// </summary>
/// <param name="low">
/// A <see cref="TimeSpan" /> that represents, inclusively, the smallest
/// number of ticks a <see cref="TimeSpan" /> can have that is created
/// by this <see cref="DateTimeGenerator" />.
/// </param>
public TimeSpanGenerator(TimeSpan low) :
this(low, TimeSpan.MaxValue) {}
/// <summary>
/// Instantiates a <see cref="TimeSpanGenerator" /> that can create
/// <see cref="TimeSpan" /> values which range in ticks from
/// <paramref name="low" /> to <paramref name="high" />.
/// </summary>
/// <param name="low">
/// A <see cref="TimeSpan" /> that represents, inclusively, the smallest
/// number of ticks a <see cref="TimeSpan" /> can have that is created
/// by this <see cref="DateTimeGenerator" />.
/// </param>
/// <param name="high">
/// A <see cref="TimeSpan" /> that represents, exclusively, the greatest
/// number of ticks a <see cref="TimeSpan" /> can have that is created
/// by this <see cref="DateTimeGenerator" />.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="low" /> has fewer ticks than
/// <paramref name="high" />.
/// </exception>
public TimeSpanGenerator(TimeSpan low, TimeSpan high) {
if (low.CompareTo(high) >= 0) {
throw new ArgumentException(
$"The {typeof(TimeSpan).Name} '{nameof(low)}' argument must have " +
$"fewer ticks than the {typeof(TimeSpan).Name} '{nameof(high)}' argument."
);
}
this.tickGenerator = new Int64Generator(low.Ticks, high.Ticks);
this.Low = low;
this.High = high;
}
private TimeSpan NextImpl(Func<Int64> getNextTicks) {
return new TimeSpan(getNextTicks());
}
private TimeSpan NextImpl(TimeSpan other, Func<Int64, Int64> getNextTicks) {
return this.NextImpl(() => getNextTicks(other.Ticks));
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> value that has a greater
/// than or equal to number of ticks than <see cref="Low" />, but a
/// fewer number of ticks than <see cref="High" />.
/// </summary>
/// <returns>
/// An instance of type <see cref="TimeSpan" /> that has a greater
/// than or equal to number of ticks than <see cref="Low" />, but a
/// fewer number of ticks than <see cref="High" />.
/// </returns>
public TimeSpan Next() {
return this.NextImpl(this.tickGenerator.Next);
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> value that has a greater than
/// or equal to number of ticks than <see cref="Low" />, a fewer number
/// of ticks than <see cref="High" />, but an different number of ticks
/// than <paramref name="other" />.
/// </summary>
/// <remarks>
/// The value provided for <paramref name="other" /> does not, itself,
/// need to be between <see cref="Low" /> and <see cref="High" />.
/// </remarks>
/// <param name="other">
/// A <see cref="TimeSpan" /> that is distinct (in terms of ticks)
/// from the <see cref="TimeSpan" /> that is to be returned.
/// </param>
/// <returns>
/// A <see cref="TimeSpan" /> that is distinct (in terms of ticks) from
/// <paramref name="other" />, but also has a greater than or equal number
/// of ticks than <see cref="Low" />, and fewer ticks than <see cref="High" />.
/// </returns>
/// <exception cref="UnableToGenerateValueException">
/// Thrown when this generator is unable to provide a value has a greater than
/// or equal number of ticks than <see cref="Low" /> and fewer ticks than
/// <see cref="High" />. This can happen if <see cref="Low" /> and
/// <see cref="High" /> have an effective range of one unique quantity of ticks,
/// and the distinct value provided via the <paramref name="other" /> parameter
/// has that many ticks.
/// </exception>
public TimeSpan NextDistinct(TimeSpan other) {
return this.NextImpl(other, this.tickGenerator.NextDistinct);
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> that will have a greater
/// number of ticks than <paramref name="other" />, but be between
/// <see cref="Low" /> (inclusively) and <see cref="High" /> (exclusively).
/// </summary>
/// <remarks>
/// The value provided for <paramref name="other" /> does not, itself,
/// need to be between <see cref="Low" /> and <see cref="High" />.
/// </remarks>
/// <param name="other">
/// A <see cref="TimeSpan" /> that has a number of ticks that is
/// less than the <see cref="TimeSpan" /> that will be generated.
/// </param>
/// <returns>
/// A <see cref="TimeSpan" /> that is greater than both
/// <paramref name="other" /> and <see cref="Low" /> in terms of ticks.
/// </returns>
/// <exception cref="UnableToGenerateValueException">
/// Thrown when this generator is unable to provide a <see cref="TimeSpan" />
/// that is greater than <paramref name="other" /> and <see cref="Low" />
/// in terms of ticks. This can happen if <see cref="High" /> has fewer ticks
/// than <paramref name="other" />.
/// </exception>
public TimeSpan NextGreaterThan(TimeSpan other) {
return this.NextImpl(other, this.tickGenerator.NextGreaterThan);
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> that will have a greater or equal
/// number of ticks than <paramref name="other" />, but be between
/// <see cref="Low" /> (inclusively) and <see cref="High" /> (exclusively).
/// </summary>
/// <remarks>
/// The value provided for <paramref name="other" /> does not, itself,
/// need to be between <see cref="Low" /> and <see cref="High" />.
/// </remarks>
/// <param name="other">
/// A <see cref="TimeSpan" /> that has a number of ticks that is
/// less than or equal to the <see cref="TimeSpan" /> that will be generated.
/// </param>
/// <returns>
/// A <see cref="TimeSpan" /> that is greater than or equal to both
/// <paramref name="other" /> and <see cref="Low" /> in terms of ticks.
/// </returns>
/// <exception cref="UnableToGenerateValueException">
/// Thrown when this generator is unable to provide a <see cref="TimeSpan" />
/// that is greater than or equal to <paramref name="other" /> and <see cref="Low" />
/// in terms of ticks. This can happen if <see cref="High" /> has fewer ticks
/// than <paramref name="other" />.
/// </exception>
public TimeSpan NextGreaterThanOrEqualTo(TimeSpan other) {
return this.NextImpl(other, this.tickGenerator.NextGreaterThanOrEqualTo);
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> that will have a fewer
/// number of ticks than <paramref name="other" />, but be between
/// <see cref="Low" /> (inclusively) and <see cref="High" /> (exclusively).
/// </summary>
/// <remarks>
/// The value provided for <paramref name="other" /> does not, itself,
/// need to be between <see cref="Low" /> and <see cref="High" />.
/// </remarks>
/// <param name="other">
/// A <see cref="TimeSpan" /> that has a number of ticks that is
/// greater than the <see cref="TimeSpan" /> that will be generated.
/// </param>
/// <returns>
/// A <see cref="TimeSpan" /> that is less than both
/// <paramref name="other" /> and <see cref="High" /> in terms of ticks.
/// </returns>
/// <exception cref="UnableToGenerateValueException">
/// Thrown when this generator is unable to provide a <see cref="TimeSpan" />
/// that is less than <paramref name="other" /> and <see cref="High" />
/// in terms of ticks. This can happen if <see cref="Low" /> has fewer ticks than
/// <paramref name="other" />.
/// </exception>
public TimeSpan NextLessThan(TimeSpan other) {
return this.NextImpl(other, this.tickGenerator.NextLessThan);
}
/// <summary>
/// Creates a new <see cref="TimeSpan" /> that will have a fewer or equal
/// number of ticks than <paramref name="other" />, but be between
/// <see cref="Low" /> (inclusively) and <see cref="High" /> (exclusively).
/// </summary>
/// <remarks>
/// The value provided for <paramref name="other" /> does not, itself,
/// need to be between <see cref="Low" /> and <see cref="High" />.
/// </remarks>
/// <param name="other">
/// A <see cref="TimeSpan" /> that has a number of ticks that is greater
/// than or equal to the <see cref="TimeSpan" /> that will be generated.
/// </param>
/// <returns>
/// A <see cref="TimeSpan" /> that is less than or equal to both
/// <paramref name="other" /> and <see cref="High" /> in terms of ticks.
/// </returns>
/// <exception cref="UnableToGenerateValueException">
/// Thrown when this generator is unable to provide a <see cref="TimeSpan" />
/// that is less than or equal to <paramref name="other" /> and <see cref="High" />
/// in terms of ticks. This can happen if <see cref="Low" /> has fewer ticks than
/// <paramref name="other" />.
/// </exception>
public TimeSpan NextLessThanOrEqualTo(TimeSpan other) {
return this.NextImpl(other, this.tickGenerator.NextLessThanOrEqualTo);
}
}
}