-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMergeableCollection.cs
628 lines (539 loc) · 20.9 KB
/
MergeableCollection.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
namespace Standard
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
internal interface IMergeable<TKey, TItem> : IEquatable<TItem> where TKey : IEquatable<TKey>
{
/// <summary>Immutable foreign key for the object </summary>
TKey FKID { get; }
/// <summary>Merge the properties of another object of like type with this one.</summary>
/// <param name="other">The object to merge</param>
void Merge(TItem other);
}
internal class MergeableCollection<TKey, TItem> : IList<TItem>, INotifyCollectionChanged where TItem : class where TKey : IEquatable<TKey>
{
private class _Comparer : IComparer<TItem>
{
private bool _dontCompare = false;
private Comparison<TItem> _defaultComparison;
private Comparison<TItem> _customComparison;
private Comparison<TItem> _trueComparison;
public _Comparer()
{
if (Utility.IsInterfaceImplemented(typeof(TItem), typeof(IComparable<TItem>)))
{
_defaultComparison = (left, right) =>
{
if (object.ReferenceEquals(left, right))
{
return 0;
}
var comparableLeft = (IComparable<TItem>)left;
if (comparableLeft == null)
{
return -1;
}
return comparableLeft.CompareTo(right);
};
}
_trueComparison = _defaultComparison;
}
public Comparison<TItem> Comparison
{
get { return _trueComparison; }
set
{
Assert.IsFalse(_dontCompare);
if (!_dontCompare)
{
_customComparison = value;
_trueComparison = _customComparison ?? _defaultComparison;
}
}
}
public bool CanCompare
{
get { return _trueComparison != null; }
}
public void StopComparisons()
{
_trueComparison = null;
_dontCompare = true;
}
public int Compare(TItem x, TItem y)
{
Assert.IsTrue(CanCompare);
return _trueComparison(x, y);
}
public IEnumerable<TItem> OrderedList(IEnumerable<TItem> original)
{
Assert.IsNotNull(original);
if (!CanCompare)
{
return original;
}
return original.OrderBy(x => x, this);
}
}
private readonly bool _areItemsMergable;
private readonly bool _areItemsNotifiable;
private readonly ObservableCollection<TItem> _items;
private readonly Dictionary<TKey, TItem> _fkidLookup;
private _Comparer _itemComparer = new _Comparer();
// Block reentrancy that would cause the collection to be reordered.
// If while we're doing a merge we get change notifications that the item has changed, ignore it.
private IMergeable<TKey, TItem> _suspendNotificationsForMergeableObject;
public object SyncRoot { get; private set; }
public MergeableCollection()
: this(null, true)
{}
public MergeableCollection(bool sort)
: this(null, sort)
{}
public MergeableCollection(IEnumerable<TItem> dataObjects)
: this(dataObjects, true)
{}
public MergeableCollection(IEnumerable<TItem> dataObjects, bool sort)
{
SyncRoot = new object();
if (!sort)
{
_itemComparer.StopComparisons();
}
// We don't really want to constrain based on the type being IMergeable or comparable
// This is a very specific check. We want to ensure that this type supports IMergeable<T>, not IMergeable<SomethingElse>
_areItemsMergable = Utility.IsInterfaceImplemented(typeof(TItem), typeof(IMergeable<TKey, TItem>));
_areItemsNotifiable = Utility.IsInterfaceImplemented(typeof(TItem), typeof(INotifyPropertyChanged));
if (dataObjects == null)
{
_items = new ObservableCollection<TItem>();
}
else
{
_items = new ObservableCollection<TItem>(_itemComparer.OrderedList(dataObjects));
if (_areItemsNotifiable)
{
lock (SyncRoot)
{
foreach (INotifyPropertyChanged item in _items)
{
item.PropertyChanged += _OnItemChanged;
}
}
}
}
if (_areItemsMergable)
{
_fkidLookup = new Dictionary<TKey, TItem>();
foreach (IMergeable<TKey, TItem> item in _items)
{
//Assert.IsNotNull(item.FKID);
_fkidLookup.Add(item.FKID, (TItem)item);
}
}
}
public Comparison<TItem> CustomComparison
{
get { return _itemComparer.Comparison; }
set
{
if (_itemComparer.Comparison != value)
{
_itemComparer.Comparison = value;
RefreshSort();
}
}
}
public void RefreshSort()
{
if (_itemComparer.CanCompare)
{
lock (SyncRoot)
{
if (!_AreItemsSortedUpToIndex(_items.Count))
{
var copyList = new List<TItem>(_items);
// Clear the list first so we don't bubble-sort just to reorder.
_Merge(null, false, null);
_Merge(copyList, false, null);
}
}
}
}
/// <summary>
/// Merges data from another collection.
/// </summary>
/// <param name="newCollection">The data object collection that contains new data.</param>
/// <param name="add">
/// If true, combine the new collection with existing content, otherwise replace the list.
/// </param>
public void Merge(IEnumerable<TItem> newCollection, bool add)
{
_Merge(newCollection, add, null);
}
public void Merge(IEnumerable<TItem> newCollection, bool add, int? maxCount)
{
_Merge(newCollection, add, maxCount);
}
private void _Merge(IEnumerable<TItem> newCollection, bool add, int? maxCount)
{
// These should never get out of sync.
Assert.Implies(_areItemsMergable, () => _items.Count == _fkidLookup.Count);
lock (SyncRoot)
{
// Go-go partial template specialization!
if (_areItemsMergable)
{
_RichMerge(newCollection, add, maxCount);
}
else
{
_SimpleMerge(newCollection, add, maxCount);
}
}
}
public TItem FindFKID(TKey id)
{
lock (SyncRoot)
{
if (!_areItemsMergable)
{
throw new InvalidOperationException("This can only be used on collections with mergable items.");
}
TItem ret;
if (_fkidLookup.TryGetValue(id, out ret))
{
return ret;
}
return null;
}
}
private int _FindIndex(int startIndex, Predicate<IMergeable<TKey, TItem>> match)
{
int count = Count - startIndex;
for (int i = startIndex; i < startIndex + count; ++i)
{
if (match((IMergeable<TKey, TItem>)this[i]))
{
return i;
}
}
return -1;
}
private bool _VerifyInsertionPoint(int index)
{
lock (SyncRoot)
{
if (_itemComparer.Comparison == null)
{
// We don't have any way of determining a correct order. Whatever we have is fine.
return true;
}
// Make sure that the item at index is not less than the one before it
// and not greater than the one after it.
// If this fails, we need to update the list.
if (index != 0)
{
if (_itemComparer.Compare(_items[index - 1], _items[index]) > 0)
{
return false;
}
}
if (index < _items.Count - 1)
{
if (_itemComparer.Compare(_items[index], _items[index+1]) > 0)
{
return false;
}
}
return true;
}
}
private int _FindInsertionPoint(TItem item)
{
Assert.IsNotNull(item);
if (_itemComparer.Comparison != null)
{
for (int i = 0; i < _items.Count; ++i)
{
if (_itemComparer.Compare(item, _items[i]) <= 0)
{
return i;
}
}
}
return -1;
}
/// <summary>
/// Safe version of Clear that removes references to this from the items being removed.
/// </summary>
private void _MergeClear()
{
if (_areItemsNotifiable)
{
foreach (var item in _items)
{
_SafeRemoveNotifyAndLookup(item);
}
}
_items.Clear();
if (_fkidLookup != null)
{
_fkidLookup.Clear();
}
}
private void _RichMerge(IEnumerable<TItem> newCollection, bool additive, int? maxCount)
{
lock (SyncRoot)
{
if (newCollection == null)
{
_MergeClear();
return;
}
if (!additive)
{
int index = -1;
foreach (TItem newItem in _itemComparer.OrderedList(newCollection).Sublist(0, maxCount))
{
var mergeableItem = newItem as IMergeable<TKey, TItem>;
++index;
//Assert.IsNotNull(mergeableItem.FKID);
int oldIndex = _FindIndex(index, p => mergeableItem.FKID.Equals(p.FKID));
if (oldIndex == -1)
{
_items.Insert(index, newItem);
_SafeAddNotifyAndLookup(newItem);
continue;
}
else if (oldIndex != index)
{
_items.Move(oldIndex, index);
}
_suspendNotificationsForMergeableObject = (IMergeable<TKey, TItem>)this[index];
_suspendNotificationsForMergeableObject.Merge(newItem);
_suspendNotificationsForMergeableObject = null;
Assert.IsTrue<object>(unused => _AreItemsSortedUpToIndex(index), null);
}
if (index != -1)
{
++index;
_RemoveRange(index);
}
else
{
_MergeClear();
}
}
else
{
foreach (var item in newCollection)
{
var mergableItem = (IMergeable<TKey, TItem>)item;
int index = _FindIndex(0, p => p.FKID.Equals(mergableItem.FKID));
if (index == -1)
{
index = _FindInsertionPoint(item);
if (-1 == index)
{
_items.Add(item);
}
else
{
_items.Insert(index, item);
}
_SafeAddNotifyAndLookup(item);
}
else
{
_suspendNotificationsForMergeableObject = (IMergeable<TKey, TItem>)this[index];
_suspendNotificationsForMergeableObject.Merge(item);
_suspendNotificationsForMergeableObject = null;
}
}
}
if (maxCount != null && _items.Count > maxCount.Value)
{
_RemoveRange(maxCount.Value);
}
//Assert.Implies(_areItemsComparable || _customComparison != null, () => _items.AreSorted(_customComparison));
}
}
private bool _AreItemsSortedUpToIndex(int index)
{
if (_itemComparer.CanCompare)
{
for (int i = 1; i < index; ++i)
{
if (_itemComparer.Comparison(_items[i - 1], _items[i]) > 0)
{
return false;
}
}
}
return true;
}
private void _RemoveRange(int index)
{
while (index < _items.Count)
{
_SafeRemoveNotifyAndLookup(_items[index]);
_items.RemoveAt(index);
}
}
private void _SimpleMerge(IEnumerable<TItem> newCollection, bool add, int? maxCount)
{
lock (SyncRoot)
{
if (!add)
{
// This just replaces the entire collection.
_MergeClear();
if (newCollection == null)
{
return;
}
foreach (TItem item in _itemComparer.OrderedList(newCollection).Sublist(0, maxCount))
{
_items.Add(item);
_SafeAddNotifyAndLookup(item);
}
}
else
{
foreach (var item in newCollection)
{
if (!_items.Contains(item))
{
int index = _FindInsertionPoint(item);
if (-1 == index)
{
_items.Add(item);
}
else
{
_items.Insert(index, item);
}
_SafeAddNotifyAndLookup(item);
}
}
if (maxCount != null && _items.Count > maxCount.Value)
{
_RemoveRange(maxCount.Value);
}
}
//Assert.Implies(_areItemsComparable || _customComparison != null, () => _items.AreSorted(_customComparison));
}
}
private void _SafeAddNotifyAndLookup(TItem item)
{
Assert.IsNotNull(item);
if (_areItemsNotifiable)
{
((INotifyPropertyChanged)item).PropertyChanged += _OnItemChanged;
}
if (_areItemsMergable)
{
//Assert.IsNotNull(((IMergeable<TKey, TItem>)item).FKID);
_fkidLookup.Add(((IMergeable<TKey, TItem>)item).FKID, item);
}
}
private void _SafeRemoveNotifyAndLookup(TItem item)
{
Assert.IsNotNull(item);
if (_areItemsNotifiable)
{
((INotifyPropertyChanged)item).PropertyChanged -= _OnItemChanged;
}
if (_areItemsMergable)
{
//Assert.IsNotNull(((IMergeable<TKey, TItem>)item).FKID);
_fkidLookup.Remove(((IMergeable<TKey, TItem>)item).FKID);
}
}
private void _OnItemChanged(object sender, PropertyChangedEventArgs e)
{
var item = sender as TItem;
// If we're doing a merge of this item then we expect that properties may change.
// Don't reorder because of this. We expect the merge is putting the item in the right place.
if (item != null && item != _suspendNotificationsForMergeableObject)
{
lock (SyncRoot)
{
int currentIndex = _items.IndexOf(item);
if (-1 != currentIndex)
{
if (!_VerifyInsertionPoint(currentIndex))
{
_items.RemoveAt(currentIndex);
int newIndex = _FindInsertionPoint(item);
if (newIndex == -1 || newIndex == _items.Count)
{
_items.Add(item);
}
else
{
_items.Insert(newIndex, item);
}
}
}
//Assert.IsTrue(_items.AreSorted(_customComparison));
}
}
}
#region IList<T> Members
public int IndexOf(TItem item) { return _items.IndexOf(item); }
public TItem this[int index]
{
get { return _items[index]; }
set { throw new NotSupportedException(); }
}
#region Unsupported Mutable IList<T> Members
void IList<TItem>.Insert(int index, TItem item) { throw new NotSupportedException(); }
void IList<TItem>.RemoveAt(int index) { throw new NotSupportedException(); }
#endregion
#endregion
#region ICollection<T> Members
public bool Contains(TItem item) { return _items.Contains(item); }
public void CopyTo(TItem[] array, int arrayIndex) { _items.CopyTo(array, arrayIndex); }
public int Count { get { return _items.Count; } }
public bool IsReadOnly { get { return true; } }
public void Clear() { Merge(null, false); }
public void Add(TItem item)
{
Verify.IsNotNull(item, "item");
Merge(new [] { item }, true);
}
public bool Remove(TItem item)
{
Verify.IsNotNull(item, "item");
lock (SyncRoot)
{
if (_items.Remove(item))
{
_SafeRemoveNotifyAndLookup(item);
return true;
}
return false;
}
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<TItem> GetEnumerator() { return _items.GetEnumerator(); }
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
#endregion
#region INotifyCollectionChanged Members
public event NotifyCollectionChangedEventHandler CollectionChanged
{
add { _items.CollectionChanged += value; }
remove { _items.CollectionChanged -= value; }
}
#endregion
}
}