using System; using System.Collections.Generic; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// A watcher which detects changes to a value using a specified instance. /// The comparable value type. internal class ComparableWatcher : IValueWatcher { /********* ** Fields *********/ /// Get the current value. private readonly Func GetValue; /// The equality comparer. private readonly IEqualityComparer Comparer; /********* ** Accessors *********/ /// The field value at the last reset. public TValue PreviousValue { get; private set; } /// The latest value. public TValue CurrentValue { get; private set; } /// Whether the value changed since the last reset. public bool IsChanged { get; private set; } /********* ** Public methods *********/ /// Construct an instance. /// Get the current value. /// The equality comparer which indicates whether two values are the same. public ComparableWatcher(Func getValue, IEqualityComparer comparer) { this.GetValue = getValue; this.Comparer = comparer; this.CurrentValue = getValue(); this.PreviousValue = this.CurrentValue; } /// Update the current value if needed. public void Update() { this.CurrentValue = this.GetValue(); this.IsChanged = !this.Comparer.Equals(this.PreviousValue, this.CurrentValue); } /// Set the current value as the baseline. public void Reset() { this.PreviousValue = this.CurrentValue; this.IsChanged = false; } /// Release any references if needed when the field is no longer needed. public void Dispose() { } } }