Пример #1
0
        /// <summary>
        /// Returns an equality comparer that uses a key comparer if the source comparer determines the objects are equal.
        /// </summary>
        /// <typeparam name="T">The type of objects being compared.</typeparam>
        /// <typeparam name="TKey">The type of key objects being compared.</typeparam>
        /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param>
        /// <param name="selector">The key selector. May not be <c>null</c>.</param>
        /// <param name="comparerFactory">The definition of the key comparer. May not be <c>null</c>.</param>
        /// <param name="specialNullHandling">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</param>
        /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns>
        public static IFullEqualityComparer <T> ThenEquateBy <T, TKey>(this IEqualityComparer <T>?source, Func <T, TKey> selector, Func <EqualityComparerBuilderFor <TKey>, IEqualityComparer <TKey> > comparerFactory, bool specialNullHandling = false)
        {
            _ = comparerFactory ?? throw new ArgumentNullException(nameof(comparerFactory));
            var comparer = comparerFactory(EqualityComparerBuilder.For <TKey>());

            return(source.ThenEquateBy(selector, comparer, specialNullHandling));
        }
        public void SubstitutesCompareDefaultForNull()
        {
            IEqualityComparer <Person> thenByComparer = EqualityComparerBuilder.For <Person>().EquateBy(p => p.FirstName);
            IEqualityComparer <Person> source         = null;
            var comparer = source.ThenEquateBy(thenByComparer);

            Assert.Equal(EqualityComparerBuilder.For <Person>().Default().ThenEquateBy(thenByComparer).ToString(), comparer.ToString());
        }
Пример #3
0
        public void SubstitutesCompareDefaultForNull()
        {
            IEqualityComparer <Person> thenByComparer = EqualityCompare <string> .Default().SelectEquateFrom((Person p) => p.FirstName);

            IEqualityComparer <Person> source = null;
            var comparer = source.ThenEquateBy(thenByComparer);

            Assert.AreSame(EqualityCompare <Person> .Default(), (comparer as CompoundEqualityComparer <Person>).Source);
        }
        public void ThenByIsAppliedAsTieBreaker()
        {
            IEqualityComparer <Person> thenByComparer  = EqualityComparerBuilder.For <Person>().EquateBy(p => p.FirstName);
            IEqualityComparer <Person> defaultComparer = EqualityComparerBuilder.For <Person>().Default();
            IEqualityComparer <Person> fullComparer    = defaultComparer.ThenEquateBy(thenByComparer);

            Assert.True(defaultComparer.Equals(AbeAbrams, WilliamAbrams));
            Assert.Equal(defaultComparer.GetHashCode(AbeAbrams), defaultComparer.GetHashCode(WilliamAbrams));
            Assert.False(thenByComparer.Equals(AbeAbrams, WilliamAbrams));
            Assert.False(fullComparer.Equals(AbeAbrams, WilliamAbrams));
        }
Пример #5
0
        public void ThenByIsAppliedAsTieBreaker()
        {
            IEqualityComparer <Person> thenByComparer = EqualityCompare <string> .Default().SelectEquateFrom((Person p) => p.FirstName);

            IEqualityComparer <Person> defaultComparer = EqualityCompare <Person> .Default();

            IEqualityComparer <Person> fullComparer = defaultComparer.ThenEquateBy(thenByComparer);

            Assert.IsTrue(defaultComparer.Equals(AbeAbrams, WilliamAbrams));
            Assert.AreEqual(defaultComparer.GetHashCode(AbeAbrams), defaultComparer.GetHashCode(WilliamAbrams));
            Assert.IsFalse(thenByComparer.Equals(AbeAbrams, WilliamAbrams));
            Assert.IsFalse(fullComparer.Equals(AbeAbrams, WilliamAbrams));
        }
Пример #6
0
 /// <summary>
 /// Returns an equality comparer that uses a key comparer if the source comparer determines the objects are equal.
 /// </summary>
 /// <typeparam name="T">The type of objects being compared.</typeparam>
 /// <typeparam name="TKey">The type of key objects being compared.</typeparam>
 /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param>
 /// <param name="selector">The key selector. May not be <c>null</c>.</param>
 /// <param name="keyComparer">The key comparer. Defaults to <c>null</c>. If this is <c>null</c>, the default comparer is used.</param>
 /// <param name="allowNulls">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>.</param>
 /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns>
 public static IEqualityComparer <T> ThenEquateBy <T, TKey>(this IEqualityComparer <T> source, Func <T, TKey> selector, IEqualityComparer <TKey> keyComparer = null, bool allowNulls = false)
 {
     Contract.Requires(selector != null);
     Contract.Ensures(Contract.Result <IEqualityComparer <T> >() != null);
     return(source.ThenEquateBy(keyComparer.SelectEquateFrom(selector, allowNulls)));
 }
Пример #7
0
        /// <summary>
        /// Returns an equality comparer that uses a key comparer if the source comparer determines the objects are equal.
        /// </summary>
        /// <typeparam name="T">The type of objects being compared.</typeparam>
        /// <typeparam name="TKey">The type of key objects being compared.</typeparam>
        /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param>
        /// <param name="selector">The key selector. May not be <c>null</c>.</param>
        /// <param name="keyComparer">The key comparer. Defaults to <c>null</c>. If this is <c>null</c>, the default comparer is used.</param>
        /// <param name="specialNullHandling">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</param>
        /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns>
        public static IFullEqualityComparer <T> ThenEquateBy <T, TKey>(this IEqualityComparer <T>?source, Func <T, TKey> selector, IEqualityComparer <TKey>?keyComparer = null, bool specialNullHandling = false)
        {
            var selectComparer = new SelectEqualityComparer <T, TKey>(selector, keyComparer, specialNullHandling);

            return(source.ThenEquateBy(selectComparer));
        }
Пример #8
0
        /// <summary>
        /// Returns an equality comparer that uses a key comparer if the source comparer determines the objects are equal.
        /// </summary>
        /// <typeparam name="T">The type of objects being compared.</typeparam>
        /// <typeparam name="TKey">The type of key objects being compared.</typeparam>
        /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param>
        /// <param name="selector">The key selector. May not be <c>null</c>.</param>
        /// <param name="comparerFactory">The definition of the key comparer. May not be <c>null</c>.</param>
        /// <param name="specialNullHandling">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>. This value is ignored if <typeparamref name="T"/> is a non-nullable type.</param>
        /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns>
        public static IFullEqualityComparer <T> ThenEquateBy <T, TKey>(this IEqualityComparer <T> source, Func <T, TKey> selector, Func <EqualityComparerBuilderFor <TKey>, IEqualityComparer <TKey> > comparerFactory, bool specialNullHandling = false)
        {
            var comparer = comparerFactory(EqualityComparerBuilder.For <TKey>());

            return(source.ThenEquateBy(selector, comparer, specialNullHandling));
        }
 /// <summary>
 /// Returns an equality comparer that uses a key comparer if the source comparer determines the objects are equal.
 /// </summary>
 /// <typeparam name="T">The type of objects being compared.</typeparam>
 /// <typeparam name="TKey">The type of key objects being compared.</typeparam>
 /// <param name="source">The source comparer. If this is <c>null</c>, the default comparer is used.</param>
 /// <param name="selector">The key selector. May not be <c>null</c>.</param>
 /// <param name="keyComparer">The key comparer. Defaults to <c>null</c>. If this is <c>null</c>, the default comparer is used.</param>
 /// <param name="specialNullHandling">A value indicating whether <c>null</c> values are passed to <paramref name="selector"/>. If <c>false</c>, then <c>null</c> values are considered less than any non-<c>null</c> values and are not passed to <paramref name="selector"/>.</param>
 /// <returns>A comparer that uses a key comparer if the source comparer determines the objects are equal.</returns>
 public static IFullEqualityComparer <T> ThenEquateBy <T, TKey>(this IEqualityComparer <T> source, Func <T, TKey> selector, IEqualityComparer <TKey> keyComparer = null, bool specialNullHandling = false)
 {
     return(source.ThenEquateBy(keyComparer.SelectEquateFrom(selector, specialNullHandling)));
 }