Repository navigation
Expand file tree
/
Copy pathChangeSet.cs
More file actions
179 lines (158 loc) · 5.41 KB
/
Copy pathChangeSet.cs
File metadata and controls
179 lines (158 loc) · 5.41 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
using System;
using System.Collections;
using System.Collections.Generic;
using DynamicData.Annotations;
using DynamicData.Kernel;
namespace DynamicData
{
/// <summary>
/// A set of changes applied to the
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
public class ChangeSet<TObject, TKey> : IChangeSet<TObject, TKey>
{
private List<Change<TObject, TKey>> Items { get; } = new List<Change<TObject, TKey>>();
private int _adds;
private int _removes;
private int _evaluates;
private int _updates;
private int _moves;
/// <summary>
/// An empty change set
/// </summary>
public static readonly IChangeSet<TObject, TKey> Empty = new ChangeSet<TObject, TKey>();
/// <summary>
/// Initializes a new instance of the <see cref="ChangeSet{TObject, TKey}"/> class.
/// </summary>
public ChangeSet()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ChangeSet{TObject, TKey}"/> class.
/// </summary>
/// <param name="items">The items.</param>
public ChangeSet([NotNull] IEnumerable<Change<TObject, TKey>> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
Items = items.AsList();
Items.ForEach(change => Add(change, true));
}
/// <summary>
/// Initializes a new instance of the <see cref="ChangeSet{TObject, TKey}"/> class.
/// </summary>
/// <param name="reason">The reason.</param>
/// <param name="key">The key.</param>
/// <param name="current">The current.</param>
public ChangeSet(ChangeReason reason, TKey key, TObject current)
: this(reason, key, current, Optional.None<TObject>())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ChangeSet{TObject, TKey}"/> class.
/// </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>
private ChangeSet(ChangeReason reason, TKey key, TObject current, Optional<TObject> previous)
: this()
{
Add(new Change<TObject, TKey>(reason, key, current, previous));
}
/// <summary>
/// Adds the specified item.
/// </summary>
/// <param name="item">The item.</param>
public void Add(Change<TObject, TKey> item)
{
Add(item, false);
}
/// <summary>
/// Adds the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="countOnly">set to true if the item has already been added</param>
private void Add(Change<TObject, TKey> item, bool countOnly)
{
switch (item.Reason)
{
case ChangeReason.Add:
_adds++;
break;
case ChangeReason.Update:
_updates++;
break;
case ChangeReason.Remove:
_removes++;
break;
case ChangeReason.Evaluate:
_evaluates++;
break;
case ChangeReason.Moved:
_moves++;
break;
}
if (!countOnly) Items.Add(item);
}
/// <summary>
/// Gets or sets the capacity.
/// </summary>
/// <value>
/// The capacity.
/// </value>
public int Capacity
{
get { return Items.Capacity; }
set { Items.Capacity = value; }
}
/// <summary>
/// The total update count
/// </summary>
public int Count => Items.Count;
/// <summary>
/// Gets the number of additions
/// </summary>
public int Adds => _adds;
/// <summary>
/// Gets the number of updates
/// </summary>
public int Updates => _updates;
/// <summary>
/// Gets the number of removes
/// </summary>
public int Removes => _removes;
/// <summary>
/// The number of requeries
/// </summary>
public int Evaluates => _evaluates;
/// <summary>
/// Gets the number of moves
/// </summary>
public int Moves => _moves;
#region Enumeration
/// <summary>
/// Gets the enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator<Change<TObject, TKey>> GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return $"ChangeSet<{typeof (TObject).Name}.{typeof (TKey).Name}>. Count={Count}";
}
}
}