Repository navigation
Expand file tree
/
Copy pathChange.cs
More file actions
179 lines (157 loc) · 6.53 KB
/
Copy pathChange.cs
File metadata and controls
179 lines (157 loc) · 6.53 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using DynamicData.Kernel;
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// Container to describe a single change to a cache.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
public readonly struct Change<TObject, TKey> : IEquatable<Change<TObject, TKey>>
where TObject : notnull
where TKey : notnull
{
/// <summary>
/// Initializes a new instance of the <see cref="Change{TObject, TKey}"/> struct.
/// </summary>
/// <param name="reason">The reason.</param>
/// <param name="key">The key.</param>
/// <param name="current">The current.</param>
/// <param name="index">The index.</param>
public Change(ChangeReason reason, TKey key, TObject current, int index = -1)
: this(reason, key, current, Optional.None<TObject>(), index)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Change{TObject, TKey}"/> struct.
/// Constructor for ChangeReason.Move.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="current">The current.</param>
/// <param name="currentIndex">The CurrentIndex.</param>
/// <param name="previousIndex">CurrentIndex of the previous.</param>
/// <exception cref="ArgumentException">
/// CurrentIndex must be greater than or equal to zero
/// or
/// PreviousIndex must be greater than or equal to zero.
/// </exception>
public Change(TKey key, TObject current, int currentIndex, int previousIndex)
: this()
{
if (currentIndex < 0)
{
throw new ArgumentException("CurrentIndex must be greater than or equal to zero");
}
if (previousIndex < 0)
{
throw new ArgumentException("PreviousIndex must be greater than or equal to zero");
}
Current = current;
Previous = Optional.None<TObject>();
Key = key;
Reason = ChangeReason.Moved;
CurrentIndex = currentIndex;
PreviousIndex = previousIndex;
}
/// <summary>
/// Initializes a new instance of the <see cref="Change{TObject, TKey}"/> struct.
/// </summary>
/// <param name="reason">The reason.</param>
/// <param name="key">The key.</param>
/// <param name="current">The current.</param>
/// <param name="previous">The previous.</param>
/// <param name="currentIndex">Value of the current.</param>
/// <param name="previousIndex">Value of the previous.</param>
/// <exception cref="ArgumentException">
/// For ChangeReason.Add, a previous value cannot be specified
/// or
/// For ChangeReason.Change, must supply previous value.
/// </exception>
public Change(ChangeReason reason, TKey key, TObject current, in Optional<TObject> previous, int currentIndex = -1, int previousIndex = -1)
: this()
{
Current = current;
Previous = previous;
Key = key;
Reason = reason;
CurrentIndex = currentIndex;
PreviousIndex = previousIndex;
if (reason == ChangeReason.Add && previous.HasValue)
{
throw new ArgumentException("For ChangeReason.Add, a previous value cannot be specified");
}
if (reason == ChangeReason.Update && !previous.HasValue)
{
throw new ArgumentException("For ChangeReason.Change, must supply previous value");
}
}
/// <summary>
/// Gets the unique key of the item which has changed.
/// </summary>
public TKey Key { get; }
/// <summary>
/// Gets the reason for the change.
/// </summary>
public ChangeReason Reason { get; }
/// <summary>
/// Gets the item which has changed.
/// </summary>
public TObject Current { get; }
/// <summary>
/// Gets the current index.
/// </summary>
public int CurrentIndex { get; }
/// <summary>
/// <para>Gets the previous change.</para>
/// <para>This is only when Reason==ChangeReason.Replace.</para>
/// </summary>
public Optional<TObject> Previous { get; }
/// <summary>
/// <para>Gets the previous change.</para>
/// <para>This is only when Reason==ChangeReason.Update or ChangeReason.Move.</para>
/// </summary>
public int PreviousIndex { get; }
/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <param name="left">The left value to compare.</param>
/// <param name="right">The right value to compare.</param>
/// <returns>If the two values are equal to each other.</returns>
public static bool operator ==(in Change<TObject, TKey> left, in Change<TObject, TKey> right) => left.Equals(right);
/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <param name="left">The left value to compare.</param>
/// <param name="right">The right value to compare.</param>
/// <returns>If the two values are not equal to each other.</returns>
public static bool operator !=(in Change<TObject, TKey> left, in Change<TObject, TKey> right) => !left.Equals(right);
/// <inheritdoc />
public bool Equals(Change<TObject, TKey> other) => EqualityComparer<TKey>.Default.Equals(Key, other.Key) && Reason == other.Reason && EqualityComparer<TObject?>.Default.Equals(Current, other.Current) && CurrentIndex == other.CurrentIndex && Previous.Equals(other.Previous) && PreviousIndex == other.PreviousIndex;
/// <inheritdoc />
public override bool Equals(object? obj)
{
if (obj is null)
{
return false;
}
return obj is Change<TObject, TKey> change && Equals(change);
}
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
var hashCode = EqualityComparer<TKey>.Default.GetHashCode(Key);
hashCode = (hashCode * 397) ^ (int)Reason;
hashCode = (hashCode * 397) ^ (Current is null ? 0 : EqualityComparer<TObject>.Default.GetHashCode(Current));
hashCode = (hashCode * 397) ^ CurrentIndex;
hashCode = (hashCode * 397) ^ Previous.GetHashCode();
hashCode = (hashCode * 397) ^ PreviousIndex;
return hashCode;
}
}
/// <inheritdoc />
public override string ToString() => $"{Reason}, Key: {Key}, Current: {Current}, Previous: {Previous}";
}