From d74709e583994d8a4f5505546eba688293acbd18 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 21 Oct 2022 09:34:47 -0500 Subject: add Waiter --- src/slskd/Common/WaitKey.cs | 106 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/slskd/Common/WaitKey.cs (limited to 'src/slskd/Common/WaitKey.cs') diff --git a/src/slskd/Common/WaitKey.cs b/src/slskd/Common/WaitKey.cs new file mode 100644 index 00000000..302064db --- /dev/null +++ b/src/slskd/Common/WaitKey.cs @@ -0,0 +1,106 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + + /// + /// Uniquely identifies a Wait. + /// + public sealed class WaitKey : IEquatable + { + /// + /// Initializes a new instance of the class. + /// + /// The parts which make up the key. + public WaitKey(params object[] tokenParts) + { + TokenParts = tokenParts; + Token = string.Join(":", TokenParts); + } + + /// + /// Gets the wait token. + /// + public string Token { get; } + + /// + /// Gets the parts which make up the key. + /// + public object[] TokenParts { get; } + + public static bool operator !=(WaitKey lhs, WaitKey rhs) + { + return !lhs.Equals(rhs); + } + + public static bool operator ==(WaitKey lhs, WaitKey rhs) + { + return lhs.Equals(rhs); + } + + /// + /// Compares the specified to this instance. + /// + /// The object to which to compare. + /// A value indicating whether the specified object is equal to this instance. + public override bool Equals(object obj) + { + try + { + return Equals((WaitKey)obj); + } + catch (InvalidCastException) + { + return false; + } + } + + /// + /// Compares the specified WaitKey to this instance. + /// + /// The WaitKey to which to compare. + /// A value indicating whether the specified WaitKey is equal to this instance. + public bool Equals(WaitKey other) + { + return Token == other.Token; + } + + /// + /// Returns the hash code of this instance. + /// + /// The hash code of this instance. + public override int GetHashCode() + { +#if NETSTANDARD2_0 + return string.IsNullOrEmpty(Token) ? 0 : Token.GetHashCode(); +#else + return string.IsNullOrEmpty(Token) ? 0 : Token.GetHashCode(StringComparison.InvariantCulture); +#endif + } + + /// + /// Returns the string representation of the key. + /// + /// The string representation of the key. + public override string ToString() + { + return Token; + } + } +} \ No newline at end of file -- cgit v1.2.3