Repository navigation
Expand file tree
/
Copy pathChangeSetEx.cs
More file actions
106 lines (93 loc) · 4.1 KB
/
Copy pathChangeSetEx.cs
File metadata and controls
106 lines (93 loc) · 4.1 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
// 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;
using DynamicData.List.Internal;
using DynamicData.List.Linq;
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// Change set extensions.
/// </summary>
public static class ChangeSetEx
{
/// <summary>
/// Returns a flattened source with the index.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <returns>An enumerable of change sets.</returns>
/// <exception cref="ArgumentNullException">source.</exception>
public static IEnumerable<ItemChange<T>> Flatten<T>(this IChangeSet<T> source)
where T : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return new ItemChangeEnumerator<T>(source);
}
/// <summary>
/// Gets the type of the change i.e. whether it is an item or a range change.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>The change type.</returns>
public static ChangeType GetChangeType(this ListChangeReason source) => source switch
{
ListChangeReason.Add or ListChangeReason.Refresh or ListChangeReason.Replace or ListChangeReason.Moved or ListChangeReason.Remove => ChangeType.Item,
ListChangeReason.AddRange or ListChangeReason.RemoveRange or ListChangeReason.Clear => ChangeType.Range,
_ => throw new ArgumentOutOfRangeException(nameof(source)),
};
/// <summary>
/// Transforms the change set into a different type using the specified transform function.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="transformer">The transformer.</param>
/// <returns>The change set.</returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// transformer.
/// </exception>
public static IChangeSet<TDestination> Transform<TSource, TDestination>(this IChangeSet<TSource> source, Func<TSource, TDestination> transformer)
where TSource : notnull
where TDestination : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
transformer.ThrowArgumentNullExceptionIfNull(nameof(transformer));
var changes = source.Select(
change =>
{
if (change.Type == ChangeType.Item)
{
return new Change<TDestination>(change.Reason, transformer(change.Item.Current), change.Item.Previous.Convert(transformer), change.Item.CurrentIndex, change.Item.PreviousIndex);
}
return new Change<TDestination>(change.Reason, change.Range.Select(transformer), change.Range.Index);
});
return new ChangeSet<TDestination>(changes);
}
/// <summary>
/// Remove the index from the changes.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <returns>An enumerable of changes.</returns>
public static IEnumerable<Change<T>> YieldWithoutIndex<T>(this IEnumerable<Change<T>> source)
where T : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return new WithoutIndexEnumerator<T>(source);
}
/// <summary>
/// Returns a flattened source.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <returns>An enumerable of changes.</returns>
/// <exception cref="ArgumentNullException">source.</exception>
internal static IEnumerable<UnifiedChange<T>> Unified<T>(this IChangeSet<T> source)
where T : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return new UnifiedChangeEnumerator<T>(source);
}
}