diff options
Diffstat (limited to 'src/SMAPI/Framework/SnapshotListDiff.cs')
-rw-r--r-- | src/SMAPI/Framework/SnapshotListDiff.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/SnapshotListDiff.cs b/src/SMAPI/Framework/SnapshotListDiff.cs new file mode 100644 index 00000000..d4d5df50 --- /dev/null +++ b/src/SMAPI/Framework/SnapshotListDiff.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using StardewModdingAPI.Framework.StateTracking; + +namespace StardewModdingAPI.Framework +{ + /// <summary>A snapshot of a tracked list.</summary> + /// <typeparam name="T">The tracked list value type.</typeparam> + internal class SnapshotListDiff<T> + { + /********* + ** Fields + *********/ + /// <summary>The removed values.</summary> + private readonly List<T> RemovedImpl = new List<T>(); + + /// <summary>The added values.</summary> + private readonly List<T> AddedImpl = new List<T>(); + + + /********* + ** Accessors + *********/ + /// <summary>Whether the value changed since the last update.</summary> + public bool IsChanged { get; private set; } + + /// <summary>The removed values.</summary> + public IEnumerable<T> Removed => this.RemovedImpl; + + /// <summary>The added values.</summary> + public IEnumerable<T> Added => this.AddedImpl; + + + /********* + ** Public methods + *********/ + /// <summary>Update the snapshot.</summary> + /// <param name="isChanged">Whether the value changed since the last update.</param> + /// <param name="removed">The removed values.</param> + /// <param name="added">The added values.</param> + public void Update(bool isChanged, IEnumerable<T> removed, IEnumerable<T> added) + { + this.IsChanged = isChanged; + + this.RemovedImpl.Clear(); + this.RemovedImpl.AddRange(removed); + + this.AddedImpl.Clear(); + this.AddedImpl.AddRange(added); + } + + /// <summary>Update the snapshot.</summary> + /// <param name="watcher">The value watcher to snapshot.</param> + public void Update(ICollectionWatcher<T> watcher) + { + this.Update(watcher.IsChanged, watcher.Removed, watcher.Added); + } + } +} |