From 90f804c7c6a6417c944c5d2ecca7fd9eac7f54b8 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 29 Jan 2024 17:05:13 +0100 Subject: [PATCH] Equal overrides --- CShocker/Devices/Abstract/Device.cs | 15 +++++++++++++++ CShocker/Devices/Abstract/OpenShockDevice.cs | 17 ++++++++++++++++- CShocker/Ranges/DurationRange.cs | 5 +++++ CShocker/Ranges/IntensityRange.cs | 5 +++++ CShocker/Ranges/RandomIntegerRange.cs | 7 +++++++ CShocker/Shockers/OpenShockShocker.cs | 16 ++++++++++++++++ CShocker/Shockers/PiShockShocker.cs | 15 +++++++++++++++ 7 files changed, 79 insertions(+), 1 deletion(-) diff --git a/CShocker/Devices/Abstract/Device.cs b/CShocker/Devices/Abstract/Device.cs index d1057b6..4e57b84 100644 --- a/CShocker/Devices/Abstract/Device.cs +++ b/CShocker/Devices/Abstract/Device.cs @@ -69,6 +69,21 @@ public abstract class Device : IDisposable $"DurationRange: {DurationRange}\n\r"; } + public override bool Equals(object? obj) + { + return obj is Device d && Equals(d); + } + + protected bool Equals(Device other) + { + return IntensityRange.Equals(other.IntensityRange) && DurationRange.Equals(other.DurationRange) && ApiType == other.ApiType; + } + + public override int GetHashCode() + { + return HashCode.Combine(IntensityRange, DurationRange, (int)ApiType); + } + public void Dispose() { _workQueue = false; diff --git a/CShocker/Devices/Abstract/OpenShockDevice.cs b/CShocker/Devices/Abstract/OpenShockDevice.cs index 99fc951..5bf82ac 100644 --- a/CShocker/Devices/Abstract/OpenShockDevice.cs +++ b/CShocker/Devices/Abstract/OpenShockDevice.cs @@ -18,7 +18,22 @@ public abstract class OpenShockDevice : Device this.Endpoint = endpoint; this.ApiKey = apiKey; } - + + public override bool Equals(object? obj) + { + return obj is OpenShockDevice osd && Equals(osd); + } + + private bool Equals(OpenShockDevice other) + { + return base.Equals(other) && Endpoint == other.Endpoint && ApiKey == other.ApiKey; + } + + public override int GetHashCode() + { + return HashCode.Combine(Endpoint, ApiKey); + } + public List GetShockers() { List shockers = new(); diff --git a/CShocker/Ranges/DurationRange.cs b/CShocker/Ranges/DurationRange.cs index 499f056..59481c6 100644 --- a/CShocker/Ranges/DurationRange.cs +++ b/CShocker/Ranges/DurationRange.cs @@ -6,4 +6,9 @@ public class DurationRange : RandomIntegerRange { } + + public override bool Equals(object? obj) + { + return obj is IntensityRange && base.Equals(obj); + } } \ No newline at end of file diff --git a/CShocker/Ranges/IntensityRange.cs b/CShocker/Ranges/IntensityRange.cs index e7f5219..9b57e5e 100644 --- a/CShocker/Ranges/IntensityRange.cs +++ b/CShocker/Ranges/IntensityRange.cs @@ -6,4 +6,9 @@ public class IntensityRange : RandomIntegerRange { } + + public override bool Equals(object? obj) + { + return obj is IntensityRange && base.Equals(obj); + } } \ No newline at end of file diff --git a/CShocker/Ranges/RandomIntegerRange.cs b/CShocker/Ranges/RandomIntegerRange.cs index 38b723d..eb618b3 100644 --- a/CShocker/Ranges/RandomIntegerRange.cs +++ b/CShocker/Ranges/RandomIntegerRange.cs @@ -24,4 +24,11 @@ public abstract class RandomIntegerRange { return $"Min: {Min} Max: {Max}"; } + + public override bool Equals(object? obj) + { + return obj is RandomIntegerRange rir && + this.Min == rir.Min && + this.Max == rir.Max; + } } \ No newline at end of file diff --git a/CShocker/Shockers/OpenShockShocker.cs b/CShocker/Shockers/OpenShockShocker.cs index 11590f7..0681332 100644 --- a/CShocker/Shockers/OpenShockShocker.cs +++ b/CShocker/Shockers/OpenShockShocker.cs @@ -29,4 +29,20 @@ public struct OpenShockShocker : IShocker $"Created On: {createdOn}\n" + $"Paused: {isPaused}\n\r"; } + + public override bool Equals(object? obj) + { + return obj is OpenShockShocker oss && Equals(oss); + + } + + private bool Equals(OpenShockShocker other) + { + return id == other.id && rfId == other.rfId && model == other.model && createdOn.Equals(other.createdOn); + } + + public override int GetHashCode() + { + return HashCode.Combine(id, rfId, (int)model, createdOn); + } } \ No newline at end of file diff --git a/CShocker/Shockers/PiShockShocker.cs b/CShocker/Shockers/PiShockShocker.cs index efb9e24..670f120 100644 --- a/CShocker/Shockers/PiShockShocker.cs +++ b/CShocker/Shockers/PiShockShocker.cs @@ -5,4 +5,19 @@ namespace CShocker.Shockers; public struct PiShockShocker : IShocker { public string Code; + + public override bool Equals(object? obj) + { + return obj is PiShockShocker pss && Equals(pss); + } + + private bool Equals(PiShockShocker other) + { + return Code == other.Code; + } + + public override int GetHashCode() + { + return Code.GetHashCode(); + } } \ No newline at end of file