| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAreaPolicyKey.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 | | /// Identifies one immutable navigation-area policy revision. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct NavigationAreaPolicyKey : IEquatable<NavigationAreaPolicyKey> |
| | | 17 | | { |
| | | 18 | | /// <summary>Creates a stable policy identity and positive revision.</summary> |
| | | 19 | | public NavigationAreaPolicyKey(string policyId, long revision) |
| | | 20 | | { |
| | 376 | 21 | | SwiftThrowHelper.ThrowIfArgument( |
| | 376 | 22 | | string.IsNullOrWhiteSpace(policyId), |
| | 376 | 23 | | nameof(policyId), |
| | 376 | 24 | | "Policy ID cannot be null, empty, or whitespace."); |
| | 375 | 25 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 375 | 26 | | revision <= 0, |
| | 375 | 27 | | actualValue: null, |
| | 375 | 28 | | nameof(revision), |
| | 375 | 29 | | "Policy revision must be positive."); |
| | | 30 | | |
| | 374 | 31 | | PolicyId = policyId; |
| | 374 | 32 | | Revision = revision; |
| | 374 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets the stable ordinal host-owned policy identifier.</summary> |
| | | 36 | | public string PolicyId { get; } |
| | | 37 | | |
| | | 38 | | /// <summary>Gets the positive immutable content revision.</summary> |
| | | 39 | | public long Revision { get; } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | | 42 | | public bool Equals(NavigationAreaPolicyKey other) => |
| | 31078 | 43 | | string.Equals(PolicyId, other.PolicyId, StringComparison.Ordinal) |
| | 31078 | 44 | | && Revision == other.Revision; |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | 211 | 47 | | public override bool Equals(object? obj) => obj is NavigationAreaPolicyKey other && Equals(other); |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | public override int GetHashCode() |
| | | 51 | | { |
| | 3370 | 52 | | int idHash = PolicyId == null |
| | 3370 | 53 | | ? 0 |
| | 3370 | 54 | | : SwiftHashTools.GetDeterministicStringEqualityComparer().GetHashCode(PolicyId); |
| | 3370 | 55 | | return SwiftHashTools.CombineHashCodes(idHash, Revision.GetHashCode()); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary>Tests two policy keys for equality.</summary> |
| | 2499 | 59 | | public static bool operator ==(NavigationAreaPolicyKey left, NavigationAreaPolicyKey right) => left.Equals(right); |
| | | 60 | | |
| | | 61 | | /// <summary>Tests two policy keys for inequality.</summary> |
| | 1 | 62 | | public static bool operator !=(NavigationAreaPolicyKey left, NavigationAreaPolicyKey right) => !left.Equals(right); |
| | | 63 | | |
| | | 64 | | internal void Validate(string parameterName) |
| | | 65 | | { |
| | 2226 | 66 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2226 | 67 | | string.IsNullOrWhiteSpace(PolicyId), |
| | 2226 | 68 | | parameterName, |
| | 2226 | 69 | | "Area policy key must contain a stable ID and positive revision."); |
| | 2226 | 70 | | SwiftThrowHelper.ThrowIfArgument( |
| | 2226 | 71 | | Revision <= 0, |
| | 2226 | 72 | | parameterName, |
| | 2226 | 73 | | "Area policy key must contain a stable ID and positive revision."); |
| | 2226 | 74 | | } |
| | | 75 | | } |