/
Interval.cs
235 lines (199 loc) · 8.5 KB
/
Interval.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
using System;
using Whathecode.System.Operators;
namespace Whathecode.System
{
/// <summary>
/// Class specifying an interval from a value, to a value. Borders may be included or excluded. This type is immutable.
/// TODO: The 'ToString' and 'Parse' methods were removed since they relied on TypeConverter, which is not present in the .NET Core Library. How to support this?
/// </summary>
/// <remarks>
/// This is a wrapper class which simply redirect calls to a more generic base type.
/// </remarks>
/// <typeparam name = "T">The type used to specify the interval, and used for the calculations.</typeparam>
public class Interval<T> : AbstractInterval<T>
where T : IComparable<T>
{
readonly static bool _isIntegralType;
// TODO: Is there any benefit moving these converter functions to a factory which injects them through constructor injection instead?
/// <summary>
/// A function which can convert <typeparamref name="T" /> to a double representation.
/// The function is used at runtime by any instance of this type to perform double calculations.
/// </summary>
public static Func<T, double> ConvertValueToDouble { get; set; }
/// <summary>
/// A function which can convert a double to <typeparamref name="T" />.
/// This function is used at runtime by any instance of this type to perform double calculations.
/// </summary>
public static Func<double, T> ConvertDoubleToValue { get; set; }
static Interval()
{
_isIntegralType = TypeHelper.IsIntegralNumericType<T>();
// Verify whether default convertion operators are available to and from double.
try
{
ConvertValueToDouble = CastOperator<T, double>.Cast;
ConvertDoubleToValue = CastOperator<double, T>.Cast;
}
catch (TypeInitializationException)
{
ConvertValueToDouble = null;
ConvertDoubleToValue = null;
}
}
/// <summary>
/// Create a new interval with a specified start and end, both included in the interval.
/// </summary>
/// <param name = "start">The start of the interval, included in the interval.</param>
/// <param name = "end">The end of the interval, included in the interval.</param>
public Interval( T start, T end )
: this( start, true, end, true ) {}
/// <summary>
/// Create a new interval with a specified start and end.
/// </summary>
/// <param name = "start">The start of the interval.</param>
/// <param name = "isStartIncluded">Is the value at the start of the interval included in the interval.</param>
/// <param name = "end">The end of the interval.</param>
/// <param name = "isEndIncluded">Is the value at the end of the interval included in the interval.</param>
public Interval( T start, bool isStartIncluded, T end, bool isEndIncluded )
: base( start, isStartIncluded, end, isEndIncluded ) {}
/// <summary>
/// Create a less generic interval from a more generic base type.
/// </summary>
/// <param name = "interval">The more generic base type.</param>
public Interval( IInterval<T, T> interval )
: base( interval.Start, interval.IsStartIncluded, interval.End, interval.IsEndIncluded ) {}
protected override bool IsIntegralType => _isIntegralType;
protected override IInterval<T, T> CreateInstance( T start, bool isStartIncluded, T end, bool isEndIncluded )
{
return new Interval<T>( start, isStartIncluded, end, isEndIncluded );
}
protected override IInterval<T> ReduceGenerics( IInterval<T, T> interval )
{
return new Interval<T>( interval.Start, interval.IsStartIncluded, interval.End, interval.IsEndIncluded );
}
protected override double Convert( T value )
{
CheckForInvalidImplementation();
return ConvertValueToDouble( value );
}
protected override T Convert( double value )
{
CheckForInvalidImplementation();
return ConvertDoubleToValue( value );
}
void CheckForInvalidImplementation()
{
if (ConvertValueToDouble == null || ConvertDoubleToValue == null)
{
Type type = typeof( Interval<T> );
const string message =
"In order to use {0} you need to set 'ConvertSizeToDouble' and 'ConvertDoubleToSize'. " +
"These converters could not be generated automatically for the specified type parameters.";
throw new InvalidImplementationException( string.Format( message, type ) );
}
}
protected override T Subtract( T value1, T value2 )
{
return Operator<T>.Subtract( value1, value2 );
}
protected override T AddSize( T value, T size )
{
return Operator<T>.Add( value, size );
}
}
/// <summary>
/// Class specifying an interval from a value, to a value. Borders may be included or excluded. This type is immutable.
/// </summary>
/// <typeparam name = "T">The type used to specify the interval, and used for the calculations.</typeparam>
/// <typeparam name = "TSize">The type used to specify distances in between two values of <typeparamref name="T" />.</typeparam>
public partial class Interval<T, TSize> : AbstractInterval<T, TSize>
where T : IComparable<T>
where TSize : IComparable<TSize>
{
readonly static bool _isIntegralType;
// TODO: Is there any benefit moving these converter functions to a factory which injects them through constructor injection instead?
/// <summary>
/// A function which can convert <typeparamref name="TSize" /> to a double representation.
/// The function is used at runtime by any instance of this type to perform double calculations.
/// </summary>
public static Func<TSize, double> ConvertSizeToDouble { get; set; }
/// <summary>
/// A function which can convert a double to <typeparamref name="TSize" />.
/// This function is used at runtime by any instance of this type to perform double calculations.
/// </summary>
public static Func<double, TSize> ConvertDoubleToSize { get; set; }
static Interval()
{
_isIntegralType = TypeHelper.IsIntegralNumericType<TSize>();
// Verify whether default convertion operators are available to and from double.
try
{
ConvertSizeToDouble = CastOperator<TSize, double>.Cast;
ConvertDoubleToSize = CastOperator<double, TSize>.Cast;
}
catch ( TypeInitializationException )
{
ConvertSizeToDouble = null;
ConvertDoubleToSize = null;
}
}
/// <summary>
/// Create a new interval with a specified start and end, both included in the interval.
/// </summary>
/// <param name = "start">The start of the interval, included in the interval.</param>
/// <param name = "end">The end of the interval, included in the interval.</param>
public Interval( T start, T end )
: this( start, true, end, true ) { }
/// <summary>
/// Create a new interval with a specified start and end.
/// </summary>
/// <param name = "start">The start of the interval.</param>
/// <param name = "isStartIncluded">Is the value at the start of the interval included in the interval.</param>
/// <param name = "end">The end of the interval.</param>
/// <param name = "isEndIncluded">Is the value at the end of the interval included in the interval.</param>
public Interval( T start, bool isStartIncluded, T end, bool isEndIncluded )
: base( start, isStartIncluded, end, isEndIncluded ) { }
protected override bool IsIntegralType => _isIntegralType;
protected override IInterval<T, TSize> CreateInstance( T start, bool isStartIncluded, T end, bool isEndIncluded )
{
return new Interval<T, TSize>( start, isStartIncluded, end, isEndIncluded );
}
protected override double Convert( TSize size )
{
CheckForInvalidImplementation();
return ConvertSizeToDouble( size );
}
protected override TSize Convert( double size )
{
CheckForInvalidImplementation();
return ConvertDoubleToSize( size );
}
void CheckForInvalidImplementation()
{
if ( ConvertSizeToDouble == null || ConvertDoubleToSize == null )
{
Type type = typeof( Interval<T, TSize> );
const string message =
"In order to use {0} you need to set 'ConvertSizeToDouble' and 'ConvertDoubleToSize'. " +
"These converters could not be generated automatically for the specified type parameters.";
throw new InvalidImplementationException( string.Format( message, type ) );
}
}
protected override TSize Subtract( T value1, T value2 )
{
return Operator<T, TSize>.Subtract( value1, value2 );
}
protected override T SubtractSize( T value, TSize size )
{
return Operator<T, TSize>.SubtractSize( value, size );
}
protected override TSize SubtractSizes( TSize value1, TSize value2 )
{
return Operator<TSize>.Subtract( value1, value2 );
}
protected override T AddSize( T value, TSize size )
{
return Operator<T, TSize>.AddSize( value, size );
}
}
}