using System;
using System.Collections.Generic;
namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
/// A watcher which detects changes to a value using a specified instance.
internal class ComparableWatcher : IValueWatcher
{
/*********
** Properties
*********/
/// Get the current value.
private readonly Func GetValue;
/// The equality comparer.
private readonly IEqualityComparer Comparer;
/*********
** Accessors
*********/
/// The field value at the last reset.
public T PreviousValue { get; private set; }
/// The latest value.
public T 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.PreviousValue = getValue();
}
/// 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() { }
}
}