From 3e50c90230bf4f7aa4efb69b3db47dddd1e43750 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 18 Jun 2017 20:55:12 -0400 Subject: [PATCH] add IEquatable interface to SDate (#307) --- src/StardewModdingAPI/Utilities/SDate.cs | 61 ++++++++++++++---------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/StardewModdingAPI/Utilities/SDate.cs b/src/StardewModdingAPI/Utilities/SDate.cs index 5f7ff030..e0613491 100644 --- a/src/StardewModdingAPI/Utilities/SDate.cs +++ b/src/StardewModdingAPI/Utilities/SDate.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Utilities { /// Represents a Stardew Valley date. - public class SDate + public class SDate : IEquatable { /********* ** Properties @@ -66,6 +66,12 @@ namespace StardewModdingAPI.Utilities this.Year = year; } + /// Get the current in-game date. + public static SDate Now() + { + return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year); + } + /// Get a new date with the given number of days added. /// The number of days to add. /// Returns the resulting date. @@ -108,15 +114,37 @@ namespace StardewModdingAPI.Utilities return $"{this.Day:00} {this.Season} Y{this.Year}"; } - /// Get the current in-game date. - public static SDate Now() + /**** + ** IEquatable + ****/ + /// Get whether this instance is equal to another. + /// The other value to compare. + public bool Equals(SDate other) { - return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year); + return this == other; } - /********* - ** Operator methods - *********/ + /// Get whether this instance is equal to another. + /// The other value to compare. + public override bool Equals(object obj) + { + return obj is SDate other && this == other; + } + + /// Get a hash code which uniquely identifies a date. + public override int GetHashCode() + { + // return the number of days since 01 spring Y1 + int yearIndex = this.Year - 1; + return + yearIndex * this.DaysInSeason * this.SeasonsInYear + + this.GetSeasonIndex() * this.DaysInSeason + + this.Day; + } + + /**** + ** Operators + ****/ /// Get whether one date is equal to another. /// The base date to compare. /// The other date to compare. @@ -166,25 +194,6 @@ namespace StardewModdingAPI.Utilities return date?.GetHashCode() < other?.GetHashCode(); } - /// Overrides the equals function. - /// Object being compared. - /// The equalaity of the object. - public override bool Equals(object obj) - { - return obj is SDate other && this == other; - } - - /// Get a hash code which uniquely identifies a date. - public override int GetHashCode() - { - // return the number of days since 01 spring Y1 - int yearIndex = this.Year - 1; - return - yearIndex * this.DaysInSeason * this.SeasonsInYear - + this.GetSeasonIndex() * this.DaysInSeason - + this.Day; - } - /********* ** Private methods