| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ValueRange.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Primitives.Intervals; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents a range of values of type T, defined by optional minimum and maximum boundaries. The range can be inclusi |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The type of the values in the range.</typeparam> |
| | | 16 | | public sealed class ValueRange<T> |
| | | 17 | | where T : struct, IComparable<T> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="ValueRange{T}"/> class with optional minimum and maximum boundaries |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="min">The minimum value of the range, or null if the range has no lower bound.</param> |
| | | 23 | | /// <param name="max">The maximum value of the range, or null if the range has no upper bound.</param> |
| | | 24 | | /// <param name="minInclusive">Indicates whether the minimum value is inclusive.</param> |
| | | 25 | | /// <param name="maxInclusive">Indicates whether the maximum value is inclusive.</param> |
| | | 26 | | /// <exception cref="ArgumentException">Thrown when the minimum value is greater than the maximum value.</exception> |
| | | 27 | | public ValueRange(T? min = null, T? max = null, bool minInclusive = true, bool maxInclusive = true) |
| | | 28 | | { |
| | 15 | 29 | | if (min.HasValue && max.HasValue && min.Value.CompareTo(max.Value) > 0) |
| | | 30 | | { |
| | 3 | 31 | | throw new ArgumentException("Min must be lower than max."); |
| | | 32 | | } |
| | | 33 | | |
| | 12 | 34 | | Min = min; |
| | 12 | 35 | | Max = max; |
| | 12 | 36 | | IsMinInclusive = minInclusive; |
| | 12 | 37 | | IsMaxInclusive = maxInclusive; |
| | 12 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the minimum value of the range, or null if the range has no lower bound. This property represents the lower |
| | | 42 | | /// </summary> |
| | | 43 | | public T? Min { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the maximum value of the range, or null if the range has no upper bound. This property represents the upper |
| | | 47 | | /// </summary> |
| | | 48 | | public T? Max { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets a value indicating whether the minimum boundary of the range is inclusive. If this property is true, values |
| | | 52 | | /// </summary> |
| | | 53 | | public bool IsMinInclusive { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a value indicating whether the maximum boundary of the range is inclusive. If this property is true, values |
| | | 57 | | /// </summary> |
| | | 58 | | public bool IsMaxInclusive { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets a value indicating whether the range has a defined minimum boundary. If this property is true, the Min prop |
| | | 62 | | /// </summary> |
| | 21 | 63 | | public bool HasMin => Min.HasValue; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a value indicating whether the range has a defined maximum boundary. If this property is true, the Max prop |
| | | 67 | | /// </summary> |
| | 18 | 68 | | public bool HasMax => Max.HasValue; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Determines whether a specified value falls within the range defined by the minimum and maximum boundaries, takin |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="value">The value to check against the range.</param> |
| | | 74 | | /// <returns>True if the value is within the range; otherwise, false.</returns> |
| | | 75 | | public bool Contains(T value) |
| | | 76 | | { |
| | 12 | 77 | | var validMin = |
| | 12 | 78 | | !HasMin || |
| | 12 | 79 | | value.CompareTo(Min!.Value) > 0 || |
| | 12 | 80 | | (value.CompareTo(Min.Value) == 0 && IsMinInclusive); |
| | | 81 | | |
| | 12 | 82 | | var validMax = |
| | 12 | 83 | | !HasMax || |
| | 12 | 84 | | value.CompareTo(Max!.Value) < 0 || |
| | 12 | 85 | | (value.CompareTo(Max.Value) == 0 && IsMaxInclusive); |
| | | 86 | | |
| | 12 | 87 | | return validMin && validMax; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Clamps a specified value to the range defined by the minimum and maximum boundaries. If the value is less than t |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="value">The value to clamp to the range.</param> |
| | | 94 | | /// <returns>The clamped value.</returns> |
| | | 95 | | public T Clamp(T value) => |
| | 9 | 96 | | HasMin && |
| | 9 | 97 | | value.CompareTo(Min!.Value) < 0 |
| | 9 | 98 | | ? Min.Value |
| | 9 | 99 | | : HasMax && |
| | 9 | 100 | | value.CompareTo(Max!.Value) > 0 |
| | 9 | 101 | | ? Max.Value |
| | 9 | 102 | | : value; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Ensures that a specified value falls within the range defined by the minimum and maximum boundaries. If the valu |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="value">The value to check against the range.</param> |
| | | 108 | | /// <param name="propertyName">The name of the property being validated.</param> |
| | | 109 | | /// <returns>The value if it is within the range.</returns> |
| | | 110 | | /// <exception cref="ArgumentOutOfRangeException">Thrown if the value is not within the range.</exception> |
| | | 111 | | public T EnsureInRange(T value, [CallerMemberName] string propertyName = "") => |
| | 3 | 112 | | !Contains(value) |
| | 3 | 113 | | ? throw new ArgumentOutOfRangeException(propertyName, value, "Value must be inside range.") |
| | 3 | 114 | | : value; |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Returns a string representation of the value range, indicating the minimum and maximum boundaries along with the |
| | | 118 | | /// </summary> |
| | | 119 | | /// <returns>A string representation of the value range.</returns> |
| | | 120 | | public override string ToString() |
| | | 121 | | { |
| | 3 | 122 | | var start = IsMinInclusive ? "[" : "]"; |
| | 3 | 123 | | var end = IsMaxInclusive ? "]" : "["; |
| | | 124 | | |
| | 3 | 125 | | return $"{start}{Min}; {Max}{end}"; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |