-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes issue #13; Added right-centric versions of TakeLeftThenRight*Me…
…rgeConflictResolver types
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace DiffLib | ||
{ | ||
/// <summary> | ||
/// This implementation of <see cref="IMergeConflictResolver{T}"/> resolves a conflict by taking the left side and then taking the right side. In the case | ||
/// where both cases are identical, only the left side is taken. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The type of elements in the collections being merged. | ||
/// </typeparam> | ||
public class TakeRightThenLeftIfLeftDiffersFromRight<T> : IMergeConflictResolver<T> | ||
{ | ||
private readonly IEqualityComparer<T> _EqualityComparer; | ||
|
||
/// <summary> | ||
/// Constructs a new instance of <see cref="DiffLib.TakeLeftThenRightIfRightDiffersFromLeft{T}"/> using the specified <paramref name="equalityComparer"/>. | ||
/// </summary> | ||
/// <param name="equalityComparer"> | ||
/// The <see cref="IEqualityComparer{T}"/> to use when determining if elements of the left side of a conflict matches those on the right side. If | ||
/// <c>null</c> then <see cref="EqualityComparer{T}.Default"/> is used. | ||
/// </param> | ||
public TakeRightThenLeftIfLeftDiffersFromRight([CanBeNull] IEqualityComparer<T> equalityComparer = null) | ||
{ | ||
_EqualityComparer = equalityComparer ?? EqualityComparer<T>.Default; | ||
} | ||
|
||
/// <inheritdoc /> | ||
[NotNull, ItemCanBeNull] | ||
public IEnumerable<T> Resolve([NotNull] IList<T> commonBase, [NotNull] IList<T> left, [NotNull] IList<T> right) | ||
{ | ||
foreach (var item in right) | ||
yield return item; | ||
|
||
if (left.SequenceEqual(right, _EqualityComparer)) | ||
yield break; | ||
|
||
foreach (var item in left) | ||
yield return item; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace DiffLib | ||
{ | ||
/// <summary> | ||
/// This implementation of <see cref="IMergeConflictResolver{T}"/> takes the left side then takes the right side. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The type of elements in the collections being merged. | ||
/// </typeparam> | ||
public class TakeRightThenLeftMergeConflictResolver<T> : IMergeConflictResolver<T> | ||
{ | ||
/// <inheritdoc /> | ||
[NotNull, ItemCanBeNull] | ||
public IEnumerable<T> Resolve([NotNull] IList<T> commonBase, [NotNull] IList<T> left, [NotNull] IList<T> right) | ||
{ | ||
foreach (var item in right) | ||
yield return item; | ||
|
||
foreach (var item in left) | ||
yield return item; | ||
} | ||
} | ||
} |