|
| 1 | +// |
| 2 | +// Copyright (c) Pete Sramek. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +namespace PolylineAlgorithm.Validation; |
| 7 | + |
| 8 | +using PolylineAlgorithm.Internal; |
| 9 | +using System; |
| 10 | +using System.Diagnostics; |
| 11 | +using System.Runtime.InteropServices; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Represents a range within coordinate value, latitude or longitude, is considered valid. |
| 15 | +/// </summary> |
| 16 | +[DebuggerDisplay(@"{ Min: {Min}, Max: {Max} }")] |
| 17 | +[StructLayout(LayoutKind.Sequential, Pack = 8, Size = 16)] |
| 18 | +public readonly struct CoordinateRange : IEquatable<CoordinateRange> { |
| 19 | + public CoordinateRange(double min, double max) { |
| 20 | + if (min >= max) { |
| 21 | + throw new ArgumentException(string.Format("", min.ToString(), max.ToString()), paramName: nameof(min)); |
| 22 | + } |
| 23 | + |
| 24 | + Min = min; |
| 25 | + Max = max; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Represents inclusive minimal value of the range. |
| 30 | + /// </summary> |
| 31 | + public readonly double Min { get; } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Represents inclusive maximal value of the range. |
| 35 | + /// </summary> |
| 36 | + public readonly double Max { get; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Indicates whether the <paramref name="value"/> is within the range. |
| 40 | + /// </summary> |
| 41 | + /// <param name="value">A value to be validated is in range.</param> |
| 42 | + /// <returns><see langword="true" /> if <paramref name="value"/> is within the range; otherwise, <see langword="false"/>.</returns> |
| 43 | + public bool IsInRange(double value) => value >= Min && value <= Max; |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Returns the formatted string respresentation of this instance. |
| 47 | + /// </summary> |
| 48 | + /// <returns>The formatted string respresentation of this instance.</returns> |
| 49 | + /// <remarks>{ Min: [double], Max: [double] }</remarks> |
| 50 | + public override string ToString() { |
| 51 | + return $"{{ {nameof(Min)}: {Min}, {nameof(Max)}: {Max} }}"; |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + public override bool Equals(object? obj) { |
| 56 | + return obj is CoordinateRange range && Equals(range); |
| 57 | + } |
| 58 | + |
| 59 | + /// <inheritdoc /> |
| 60 | + public bool Equals(CoordinateRange other) { |
| 61 | + return Min == other.Min && |
| 62 | + Max == other.Max; |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc /> |
| 66 | + public override int GetHashCode() { |
| 67 | + return HashCode.Combine(Min, Max); |
| 68 | + } |
| 69 | + |
| 70 | + public static bool operator ==(CoordinateRange left, CoordinateRange right) { |
| 71 | + return left.Equals(right); |
| 72 | + } |
| 73 | + |
| 74 | + public static bool operator !=(CoordinateRange left, CoordinateRange right) { |
| 75 | + return !(left == right); |
| 76 | + } |
| 77 | +} |
0 commit comments