diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-04-21 20:37:17 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-04-21 20:37:17 -0400 |
commit | eead352af26d0fcc5cac147d0eb5ec384854d931 (patch) | |
tree | 78895ff201953f065667976c0e861168dddcebc1 /src/SMAPI/Framework/StateTracking/Comparers | |
parent | b346d28d3858b79c6c4cde55faac34ecdedeaff1 (diff) | |
download | SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.gz SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.bz2 SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.zip |
rewrite world/player state tracking (#453)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/Comparers')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Comparers/EquatableComparer.cs | 32 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Comparers/ObjectReferenceComparer.cs | 29 |
2 files changed, 61 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/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); + } + } +} |