summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/Comparers
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-08-01 11:07:29 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-08-01 11:07:29 -0400
commit60b41195778af33fd609eab66d9ae3f1d1165e8f (patch)
tree7128b906d40e94c56c34ed6058f27bc31c31a08b /src/SMAPI/Framework/StateTracking/Comparers
parentb9bc1a6d17cafa0a97b46ffecda432cfc2f23b51 (diff)
parent52cf953f685c65b2b6814e375ec9a5ffa03c440a (diff)
downloadSMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.gz
SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.bz2
SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/Comparers')
-rw-r--r--src/SMAPI/Framework/StateTracking/Comparers/EquatableComparer.cs32
-rw-r--r--src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs31
-rw-r--r--src/SMAPI/Framework/StateTracking/Comparers/ObjectReferenceComparer.cs29
3 files changed, 92 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/Comparers/EquatableComparer.cs b/src/SMAPI/Framework/StateTracking/Comparers/EquatableComparer.cs
new file mode 100644
index 00000000..a96ffdb6
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/Comparers/EquatableComparer.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace StardewModdingAPI.Framework.StateTracking.Comparers
+{
+ /// <summary>Compares instances using <see cref="IEqualityComparer{T}.Equals(T,T)"/>.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ internal class EquatableComparer<T> : IEqualityComparer<T> where T : IEquatable<T>
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Determines whether the specified objects are equal.</summary>
+ /// <returns>true if the specified objects are equal; otherwise, false.</returns>
+ /// <param name="x">The first object to compare.</param>
+ /// <param name="y">The second object to compare.</param>
+ public bool Equals(T x, T y)
+ {
+ if (x == null)
+ return y == null;
+ return x.Equals(y);
+ }
+
+ /// <summary>Get a hash code for the specified object.</summary>
+ /// <param name="obj">The value.</param>
+ public int GetHashCode(T obj)
+ {
+ return RuntimeHelpers.GetHashCode(obj);
+ }
+ }
+}
diff --git a/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs b/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs
new file mode 100644
index 00000000..cc1d6553
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs
@@ -0,0 +1,31 @@
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace StardewModdingAPI.Framework.StateTracking.Comparers
+{
+ /// <summary>Compares values using their <see cref="object.Equals(object)"/> method. This should only be used when <see cref="EquatableComparer{T}"/> won't work, since this doesn't validate whether they're comparable.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ internal class GenericEqualsComparer<T> : IEqualityComparer<T>
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Determines whether the specified objects are equal.</summary>
+ /// <returns>true if the specified objects are equal; otherwise, false.</returns>
+ /// <param name="x">The first object to compare.</param>
+ /// <param name="y">The second object to compare.</param>
+ public bool Equals(T x, T y)
+ {
+ if (x == null)
+ return y == null;
+ return x.Equals(y);
+ }
+
+ /// <summary>Get a hash code for the specified object.</summary>
+ /// <param name="obj">The value.</param>
+ public int GetHashCode(T obj)
+ {
+ return RuntimeHelpers.GetHashCode(obj);
+ }
+ }
+}
diff --git a/src/SMAPI/Framework/StateTracking/Comparers/ObjectReferenceComparer.cs b/src/SMAPI/Framework/StateTracking/Comparers/ObjectReferenceComparer.cs
new file mode 100644
index 00000000..ef9adafb
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/Comparers/ObjectReferenceComparer.cs
@@ -0,0 +1,29 @@
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace StardewModdingAPI.Framework.StateTracking.Comparers
+{
+ /// <summary>A comparer which considers two references equal if they point to the same instance.</summary>
+ /// <typeparam name="T">The value type.</typeparam>
+ internal class ObjectReferenceComparer<T> : IEqualityComparer<T>
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Determines whether the specified objects are equal.</summary>
+ /// <returns>true if the specified objects are equal; otherwise, false.</returns>
+ /// <param name="x">The first object to compare.</param>
+ /// <param name="y">The second object to compare.</param>
+ public bool Equals(T x, T y)
+ {
+ return object.ReferenceEquals(x, y);
+ }
+
+ /// <summary>Get a hash code for the specified object.</summary>
+ /// <param name="obj">The value.</param>
+ public int GetHashCode(T obj)
+ {
+ return RuntimeHelpers.GetHashCode(obj);
+ }
+ }
+}