| | | 1 | | //======================================================================= |
| | | 2 | | // TraversalIntent.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using SwiftCollections.Utility; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Defines the exact starting medium and allowed target media for a path query. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct TraversalIntent : IEquatable<TraversalIntent> |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the exact traversal medium at the query start. |
| | | 20 | | /// </summary> |
| | | 21 | | public TraversalMedium StartMedium { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the nonempty set of known traversal media allowed at the target. |
| | | 25 | | /// </summary> |
| | | 26 | | public TraversalMedia TargetMedia { get; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Creates immutable traversal intent. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <exception cref="ArgumentException">Thrown when target media are empty or contain an unknown bit.</exception> |
| | | 32 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the start medium is unknown.</exception> |
| | | 33 | | public TraversalIntent( |
| | | 34 | | TraversalMedium startMedium, |
| | | 35 | | TraversalMedia targetMedia) |
| | | 36 | | { |
| | 1198 | 37 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1198 | 38 | | !TraversalTransitionDefinition.IsKnownMedium(startMedium), |
| | 1198 | 39 | | (int)startMedium, |
| | 1198 | 40 | | nameof(startMedium), |
| | 1198 | 41 | | "Start medium must identify one known traversal medium."); |
| | 1196 | 42 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1196 | 43 | | targetMedia == TraversalMedia.None |
| | 1196 | 44 | | || (targetMedia & ~NavigationCell.KnownMedia) != 0, |
| | 1196 | 45 | | nameof(targetMedia), |
| | 1196 | 46 | | "Target media must contain at least one known traversal-medium bit."); |
| | | 47 | | |
| | 1194 | 48 | | StartMedium = startMedium; |
| | 1194 | 49 | | TargetMedia = targetMedia; |
| | 1194 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc/> |
| | | 53 | | public bool Equals(TraversalIntent other) => |
| | 2705 | 54 | | StartMedium == other.StartMedium |
| | 2705 | 55 | | && TargetMedia == other.TargetMedia; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | 206 | 58 | | public override bool Equals(object? obj) => obj is TraversalIntent other && Equals(other); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public override int GetHashCode() |
| | | 62 | | { |
| | 3372 | 63 | | return SwiftHashTools.CombineHashCodes((int)StartMedium, (int)TargetMedia); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Returns whether two traversal intents have exactly equal values. |
| | | 68 | | /// </summary> |
| | 2498 | 69 | | public static bool operator ==(TraversalIntent left, TraversalIntent right) => left.Equals(right); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Returns whether two traversal intents differ. |
| | | 73 | | /// </summary> |
| | 1 | 74 | | public static bool operator !=(TraversalIntent left, TraversalIntent right) => !left.Equals(right); |
| | | 75 | | |
| | | 76 | | internal void Validate(string parameterName) |
| | | 77 | | { |
| | 2030 | 78 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2030 | 79 | | !TraversalTransitionDefinition.IsKnownMedium(StartMedium), |
| | 2030 | 80 | | parameterName, |
| | 2030 | 81 | | "Traversal intent must contain an exact known start medium and nonempty known target media."); |
| | 2030 | 82 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2030 | 83 | | TargetMedia == TraversalMedia.None, |
| | 2030 | 84 | | parameterName, |
| | 2030 | 85 | | "Traversal intent must contain an exact known start medium and nonempty known target media."); |
| | 2030 | 86 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2030 | 87 | | (TargetMedia & ~NavigationCell.KnownMedia) != 0, |
| | 2030 | 88 | | parameterName, |
| | 2030 | 89 | | "Traversal intent must contain an exact known start medium and nonempty known target media."); |
| | 2030 | 90 | | } |
| | | 91 | | } |