Skip to content

Commit

Permalink
Fixes issue #13; Added right-centric versions of TakeLeftThenRight*Me…
Browse files Browse the repository at this point in the history
…rgeConflictResolver types
  • Loading branch information
lassevk committed May 1, 2017
1 parent 196c85d commit bcb8c81
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
46 changes: 46 additions & 0 deletions DiffLib/TakeRightThenLeftIfLeftDiffersFromRight.cs
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;
}
}
}
27 changes: 27 additions & 0 deletions DiffLib/TakeRightThenLeftMergeConflictResolver.cs
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;
}
}
}

0 comments on commit bcb8c81

Please sign in to comment.