| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | namespace Plainquire.Filter.Abstractions; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Represents a range of <typeparamref name="TType"/>. |
| | 8 | | /// </summary> |
| | 9 | | public class Range<TType> : IEquatable<Range<TType>>, IConvertible where TType : IComparable<TType> |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The start of range. |
| | 13 | | /// </summary> |
| | 14 | | public TType? Start { get; } |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// The end of range. |
| | 18 | | /// </summary> |
| | 19 | | public TType? End { get; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Gets the distance between <see cref="Start"/> and <see cref="End"/>. |
| | 23 | | /// </summary> |
| | 24 | | /// <autogeneratedoc /> |
| 5 | 25 | | public double Distance => ToDouble(End) - ToDouble(Start); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="Range{TType}"/> class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="start">The start of range.</param> |
| | 31 | | /// <param name="end">The end of range.</param> |
| | 32 | | public Range(TType? start, TType? end) |
| | 33 | | { |
| 35245 | 34 | | Start = start; |
| 35245 | 35 | | End = end; |
| 35245 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc /> |
| | 39 | | public override string ToString() |
| | 40 | | { |
| 332 | 41 | | var start = ToString(Start); |
| 332 | 42 | | var end = ToString(End); |
| | 43 | |
|
| 332 | 44 | | return $"{start}_{end}"; |
| | 45 | |
|
| | 46 | | //if (start != null && end != null) |
| | 47 | | // return $"{start}_{end}"; |
| | 48 | | //if (start != null) |
| | 49 | | // return start; |
| | 50 | | //if (end != null) |
| | 51 | | // return end; |
| | 52 | | //return string.Empty; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <inheritdoc /> |
| | 56 | | public override bool Equals(object? obj) |
| 20 | 57 | | => Equals(obj as Range<TType>); |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public bool Equals(Range<TType>? other) |
| 20 | 61 | | => other != null && EqualityComparer<TType?>.Default.Equals(Start, other.Start) && EqualityComparer<TType?>.Defa |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public override int GetHashCode() |
| 1 | 65 | | => HashCode.Combine(Start, End); |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Indicates whether the <paramref name="val1"/> object is equal to <paramref name="val2"/> object. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 71 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 72 | | public static bool operator ==(Range<TType>? val1, Range<TType>? val2) |
| 178 | 73 | | => EqualityComparer<Range<TType>?>.Default.Equals(val1, val2); |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Indicates whether the <paramref name="val1"/> object is not equal to <paramref name="val2"/> object. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 79 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 80 | | public static bool operator !=(Range<TType>? val1, Range<TType>? val2) |
| 20 | 81 | | => !(val1 == val2); |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Indicates whether the range of <paramref name="val1"/> is lower than the range of <paramref name="val2"/> object |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 87 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 88 | | public static bool operator <(Range<TType> val1, Range<TType> val2) |
| 2 | 89 | | => ((IConvertible)val1).ToDecimal(null) < ((IConvertible)val2).ToDecimal(null); |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Indicates whether the range of <paramref name="val1"/> is greater than the range of <paramref name="val2"/> obje |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 95 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 96 | | public static bool operator >(Range<TType> val1, Range<TType> val2) |
| 2 | 97 | | => ((IConvertible)val1).ToDecimal(null) > ((IConvertible)val2).ToDecimal(null); |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Indicates whether the range of <paramref name="val1"/> is lower than or equal to the range of <paramref name="va |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 103 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 104 | | public static bool operator <=(Range<TType> val1, Range<TType> val2) |
| 2 | 105 | | => ((IConvertible)val1).ToDecimal(null) <= ((IConvertible)val2).ToDecimal(null); |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Indicates whether the range of <paramref name="val1"/> is greater than or equal to the range of <paramref name=" |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="val1">The object to compare with <paramref name="val2"/>.</param> |
| | 111 | | /// <param name="val2">The object to compare with <paramref name="val1"/>.</param> |
| | 112 | | public static bool operator >=(Range<TType> val1, Range<TType> val2) |
| 2 | 113 | | => ((IConvertible)val1).ToDecimal(null) >= ((IConvertible)val2).ToDecimal(null); |
| | 114 | |
|
| | 115 | | private static string? ToString(TType? value) |
| 664 | 116 | | => value switch |
| 664 | 117 | | { |
| 4 | 118 | | null => null, |
| 2 | 119 | | DateTime dateTime => dateTime.ToString("o"), |
| 648 | 120 | | DateTimeOffset dateTime => dateTime.ToString("o"), |
| 10 | 121 | | _ => value.ToString() |
| 664 | 122 | | }; |
| | 123 | |
|
| | 124 | | private static double ToDouble(TType? val) |
| 114 | 125 | | => val switch |
| 114 | 126 | | { |
| 2 | 127 | | null => 0, |
| 2 | 128 | | DateTime dateTime => dateTime.Ticks, |
| 2 | 129 | | DateTimeOffset dateTime => dateTime.Ticks, |
| 106 | 130 | | IConvertible convertible => convertible.ToDouble(null), |
| 2 | 131 | | _ => throw new InvalidOperationException($"The type {typeof(TType).Name} is not convertible to {nameof(Doubl |
| 114 | 132 | | }; |
| | 133 | |
|
| | 134 | | #region IConvertible |
| | 135 | | TypeCode IConvertible.GetTypeCode() |
| 1 | 136 | | => TypeCode.Object; |
| | 137 | |
|
| | 138 | | bool IConvertible.ToBoolean(IFormatProvider? provider) |
| 1 | 139 | | => throw new InvalidCastException($"Invalid cast from {nameof(Range)} to {nameof(Boolean)}"); |
| | 140 | |
|
| | 141 | | char IConvertible.ToChar(IFormatProvider? provider) |
| 1 | 142 | | => throw new InvalidCastException($"Invalid cast from {nameof(Range)} to {nameof(Char)}"); |
| | 143 | |
|
| | 144 | | DateTime IConvertible.ToDateTime(IFormatProvider? provider) |
| 1 | 145 | | => throw new InvalidCastException($"Invalid cast from {nameof(Range)} to {nameof(DateTime)}"); |
| | 146 | |
|
| | 147 | | string IConvertible.ToString(IFormatProvider? provider) |
| 2 | 148 | | => this.ToString(); |
| | 149 | |
|
| | 150 | | object IConvertible.ToType(Type conversionType, IFormatProvider? provider) |
| | 151 | | { |
| | 152 | | try |
| | 153 | | { |
| 3 | 154 | | return ((IConvertible)((IConvertible)this).ToDouble(provider)).ToType(conversionType, provider); |
| | 155 | | } |
| 1 | 156 | | catch (OverflowException) |
| | 157 | | { |
| 1 | 158 | | throw new OverflowException($"Range was too large for a {conversionType.Name}"); |
| | 159 | | } |
| 2 | 160 | | } |
| | 161 | |
|
| | 162 | | byte IConvertible.ToByte(IFormatProvider? provider) |
| | 163 | | { |
| 3 | 164 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 165 | | if (result <= byte.MaxValue) |
| 2 | 166 | | return (byte)result; |
| 1 | 167 | | throw new OverflowException($"Range was too large for a {nameof(Byte)}"); |
| | 168 | | } |
| | 169 | |
|
| | 170 | | decimal IConvertible.ToDecimal(IFormatProvider? provider) |
| | 171 | | { |
| | 172 | | try |
| | 173 | | { |
| 19 | 174 | | return Convert.ToDecimal(ToDouble(End) - ToDouble(Start)); |
| | 175 | | } |
| 1 | 176 | | catch (OverflowException) |
| | 177 | | { |
| 1 | 178 | | throw new OverflowException($"Range was too large for a {nameof(Decimal)}"); |
| | 179 | | } |
| 18 | 180 | | } |
| | 181 | |
|
| | 182 | | double IConvertible.ToDouble(IFormatProvider? provider) |
| 7 | 183 | | => ToDouble(End) - ToDouble(Start); |
| | 184 | |
|
| | 185 | | short IConvertible.ToInt16(IFormatProvider? provider) |
| | 186 | | { |
| 3 | 187 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 188 | | if (result <= short.MaxValue) |
| 2 | 189 | | return (short)result; |
| 1 | 190 | | throw new OverflowException($"Range was too large for a {nameof(Int16)}"); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | int IConvertible.ToInt32(IFormatProvider? provider) |
| | 194 | | { |
| 3 | 195 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 196 | | if (result <= int.MaxValue) |
| 2 | 197 | | return (int)result; |
| 1 | 198 | | throw new OverflowException($"Range was too large for a {nameof(Int32)}"); |
| | 199 | | } |
| | 200 | |
|
| | 201 | | long IConvertible.ToInt64(IFormatProvider? provider) |
| | 202 | | { |
| 3 | 203 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 204 | | if (result <= long.MaxValue) |
| 2 | 205 | | return (long)result; |
| 1 | 206 | | throw new OverflowException($"Range was too large for a {nameof(Int64)}"); |
| | 207 | | } |
| | 208 | |
|
| | 209 | | sbyte IConvertible.ToSByte(IFormatProvider? provider) |
| | 210 | | { |
| 3 | 211 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 212 | | if (result <= sbyte.MaxValue) |
| 2 | 213 | | return (sbyte)result; |
| 1 | 214 | | throw new OverflowException($"Range was too large for a {nameof(SByte)}"); |
| | 215 | | } |
| | 216 | |
|
| | 217 | | float IConvertible.ToSingle(IFormatProvider? provider) |
| | 218 | | { |
| 3 | 219 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 220 | | if (result < float.MaxValue) |
| 2 | 221 | | return (float)result; |
| 1 | 222 | | throw new OverflowException($"Range was too large for a {nameof(Single)}"); |
| | 223 | | } |
| | 224 | |
|
| | 225 | | ushort IConvertible.ToUInt16(IFormatProvider? provider) |
| | 226 | | { |
| 3 | 227 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 228 | | if (result <= ushort.MaxValue) |
| 2 | 229 | | return (ushort)result; |
| 1 | 230 | | throw new OverflowException($"Range was too large for a {nameof(UInt16)}"); |
| | 231 | | } |
| | 232 | |
|
| | 233 | | uint IConvertible.ToUInt32(IFormatProvider? provider) |
| | 234 | | { |
| 3 | 235 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 236 | | if (result <= uint.MaxValue) |
| 2 | 237 | | return (uint)result; |
| 1 | 238 | | throw new OverflowException($"Range was too large for a {nameof(UInt32)}"); |
| | 239 | | } |
| | 240 | |
|
| | 241 | | ulong IConvertible.ToUInt64(IFormatProvider? provider) |
| | 242 | | { |
| 3 | 243 | | var result = ToDouble(End) - ToDouble(Start); |
| 3 | 244 | | if (result <= ulong.MaxValue) |
| 2 | 245 | | return (ulong)result; |
| 1 | 246 | | throw new OverflowException($"Range was too large for a {nameof(UInt64)}"); |
| | 247 | | } |
| | 248 | | #endregion |
| | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary> |
| | 252 | | /// Utility class to create a <see cref="Range{TType}"/>. |
| | 253 | | /// </summary> |
| | 254 | | public static class Range |
| | 255 | | { |
| | 256 | | /// <summary> |
| | 257 | | /// Creates a <see cref="Range{TType}"/>. |
| | 258 | | /// </summary> |
| | 259 | | /// <typeparam name="TType">The type of the values represented by range.</typeparam> |
| | 260 | | /// <param name="start">The start of range.</param> |
| | 261 | | /// <param name="end">The end of range.</param> |
| | 262 | | public static Range<TType> Create<TType>(TType? start, TType? end) where TType : IComparable<TType> |
| | 263 | | => new(start, end); |
| | 264 | | } |