summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/SnapshotListDiff.cs
blob: 2d0efa0d03f02cd11f99419f901e18de53abeee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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();
            if (removed != null)
                this.RemovedImpl.AddRange(removed);

            this.AddedImpl.Clear();
            if (added != null)
                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);
        }
    }
}