summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs
blob: c31be1fcc32aa32353de6067d6e63f3159e2f582 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Netcode;
using StardewModdingAPI.Framework.StateTracking.Comparers;

namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
{
    /// <summary>Provides convenience wrappers for creating watchers.</summary>
    internal static class WatcherFactory
    {
        /*********
        ** Public methods
        *********/
        /****
        ** Values
        ****/
        /// <summary>Get a watcher which compares values using their <see cref="object.Equals(object)"/> method. This method should only be used when <see cref="ForEquatable{T}"/> won't work, since this doesn't validate whether they're comparable.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="getValue">Get the current value.</param>
        public static IValueWatcher<T> ForGenericEquality<T>(string name, Func<T> getValue)
            where T : struct
        {
            return new ComparableWatcher<T>(name, getValue, new GenericEqualsComparer<T>());
        }

        /// <summary>Get a watcher for an <see cref="IEquatable{T}"/> value.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="getValue">Get the current value.</param>
        public static IValueWatcher<T> ForEquatable<T>(string name, Func<T> getValue)
            where T : IEquatable<T>
        {
            return new ComparableWatcher<T>(name, getValue, new EquatableComparer<T>());
        }

        /// <summary>Get a watcher which detects when an object reference changes.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="getValue">Get the current value.</param>
        public static IValueWatcher<T> ForReference<T>(string name, Func<T> getValue)
        {
            return new ComparableWatcher<T>(name, getValue, new ObjectReferenceComparer<T>());
        }

        /// <summary>Get a watcher for a net collection.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <typeparam name="TSelf">The net field instance type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="field">The net collection.</param>
        public static IValueWatcher<T> ForNetValue<T, TSelf>(string name, NetFieldBase<T, TSelf> field) where TSelf : NetFieldBase<T, TSelf>
        {
            return new NetValueWatcher<T, TSelf>(name, field);
        }

        /****
        ** Collections
        ****/
        /// <summary>Get a watcher which detects when an object reference in a collection changes.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="collection">The observable collection.</param>
        public static ICollectionWatcher<T> ForReferenceList<T>(string name, ICollection<T> collection)
        {
            return new ComparableListWatcher<T>(name, collection, new ObjectReferenceComparer<T>());
        }

        /// <summary>Get a watcher for an observable collection.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="collection">The observable collection.</param>
        public static ICollectionWatcher<T> ForObservableCollection<T>(string name, ObservableCollection<T> collection)
        {
            return new ObservableCollectionWatcher<T>(name, collection);
        }

        /// <summary>Get a watcher for a collection that never changes.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        public static ICollectionWatcher<T> ForImmutableCollection<T>()
        {
            return ImmutableCollectionWatcher<T>.Instance;
        }

        /// <summary>Get a watcher for a net collection.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="collection">The net collection.</param>
        public static ICollectionWatcher<T> ForNetCollection<T>(string name, NetCollection<T> collection)
            where T : class, INetObject<INetSerializable>
        {
            return new NetCollectionWatcher<T>(name, collection);
        }

        /// <summary>Get a watcher for a net list.</summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <param name="collection">The net list.</param>
        public static ICollectionWatcher<T> ForNetList<T>(string name, NetList<T, NetRef<T>> collection)
            where T : class, INetObject<INetSerializable>
        {
            return new NetListWatcher<T>(name, collection);
        }

        /// <summary>Get a watcher for a net dictionary.</summary>
        /// <param name="name">A name which identifies what the watcher is watching, used for troubleshooting.</param>
        /// <typeparam name="TKey">The dictionary key type.</typeparam>
        /// <typeparam name="TValue">The dictionary value type.</typeparam>
        /// <typeparam name="TField">The net type equivalent to <typeparamref name="TValue"/>.</typeparam>
        /// <typeparam name="TSerialDict">The serializable dictionary type that can store the keys and values.</typeparam>
        /// <typeparam name="TSelf">The net field instance type.</typeparam>
        /// <param name="field">The net field.</param>
        public static NetDictionaryWatcher<TKey, TValue, TField, TSerialDict, TSelf> ForNetDictionary<TKey, TValue, TField, TSerialDict, TSelf>(string name, NetDictionary<TKey, TValue, TField, TSerialDict, TSelf> field)
            where TKey : notnull
            where TField : class, INetObject<INetSerializable>, new()
            where TSerialDict : IDictionary<TKey, TValue>, new()
            where TSelf : NetDictionary<TKey, TValue, TField, TSerialDict, TSelf>
        {
            return new NetDictionaryWatcher<TKey, TValue, TField, TSerialDict, TSelf>(name, field);
        }
    }
}