public void When_creating_a_new_keyedCollectionEx_with_non_default_comparer() { var personA = new Person { Name = "A", Age = 1 }; var personB = new Person { Name = "B", Age = 2 }; var personC = new Person { Name = "C", Age = 3 }; var collection = new KeyedCollectionEx <string, Person>(p => p.Name, StringComparer.OrdinalIgnoreCase) { personA, personB }; collection.ShouldNotBeNull(); collection.Count.ShouldBe(2); collection.Contains("A").ShouldBeTrue(); collection.Contains("a").ShouldBeTrue(); collection.Contains("B").ShouldBeTrue(); collection.Contains("b").ShouldBeTrue(); collection.Contains("C").ShouldBeFalse(); collection.Contains("c").ShouldBeFalse(); collection.Contains(personA).ShouldBeTrue(); collection.Contains(personB).ShouldBeTrue(); collection.Contains(personC).ShouldBeFalse(); collection.Add(personC); collection.Contains("C").ShouldBeTrue(); collection.Contains("c").ShouldBeTrue(); collection.Contains(personC).ShouldBeTrue(); var dodgyPerson = new Person { Name = "A", Age = 666 }; Should.Throw <ArgumentException>(() => collection.Add(dodgyPerson)).Message .ShouldBe("An item with the same key has already been added."); collection["A"].Age.ShouldBe(1); collection["B"].Age.ShouldBe(2); collection["C"].Age.ShouldBe(3); Person somePerson; collection.TryGet("A", out somePerson).ShouldBeTrue(); somePerson.ShouldNotBeNull(); somePerson.Age.ShouldBe(1); collection.TryGet("D", out somePerson).ShouldBeFalse(); somePerson.ShouldBeNull(); }
/// <summary> /// Adds the <paramref name="key"/> and <paramref name="value"/> to the <paramref name="keyedCollection"/> /// if the <paramref name="key"/> does not already exists and returns the inserted value. /// </summary> public static TValue GetOrAdd <TKey, TValue>(this KeyedCollectionEx <TKey, TValue> keyedCollection, TKey key, TValue value) { if (!keyedCollection.TryGet(key, out TValue result)) { keyedCollection.Add(value); return(value); } return(result); }
/// <summary> /// Adds the <paramref name="key"/> and the value created by <paramref name="valueCreator"/> to /// the <paramref name="keyedCollection"/> if the <paramref name="key"/> does not already exists /// and returns the inserted value. /// </summary> public static TValue GetOrAdd <TKey, TValue>(this KeyedCollectionEx <TKey, TValue> keyedCollection, TKey key, Func <TValue> valueCreator) { if (!keyedCollection.TryGet(key, out TValue result)) { var value = valueCreator(); keyedCollection.Add(value); result = value; } return(result); }
/// <summary> /// Creates a <see cref="KeyedCollection{TKey, TItem}"/> from an <see cref="IEnumerable{T}"/> /// according to a specified key selector function. /// </summary> /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <param name="source"> /// An <see cref="IEnumerable{T}"/> to create a <see cref="KeyedCollection{TKey, TItem}"/> from. /// </param> /// <param name="keySelector">A function to extract a key from each element.</param> /// <returns> /// A <see cref="KeyedCollection{TKey, TItem}"/> that contains values of <paramref name="source"/>. /// </returns> public static KeyedCollection <TKey, TSource> ToKeyedCollection <TKey, TSource>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector) { var collection = new KeyedCollectionEx <TKey, TSource>(keySelector); foreach (var item in source) { collection.Add(item); } return(collection); }
public static KeyedCollectionEx <TKey, TItem> ToKeyedCollectionEx <TKey, TItem>(this IEnumerable <TItem> sequence, Func <TItem, TKey> selector, IEqualityComparer <TKey> comparer) { var result = new KeyedCollectionEx <TKey, TItem>(selector, comparer); foreach (var item in sequence) { result.Add(item); } return(result); }
/// <summary> /// Compares the values of all the properties for the given /// <paramref name="left"/> and <paramref name="right"/> and returns the variance. /// </summary> public bool Compare <T>(T left, T right, bool inherit, bool includePrivate, out KeyedCollectionEx <PropertyInfo, Variance> variances) { var type = typeof(T); var key = new CacheKey(type, inherit, includePrivate); var cache = _cache.GetOrAdd(key, () => { return(type.GetInstanceProperties(inherit, includePrivate) .Select(p => new KeyValuePair <PropertyInfo, object>(p, AccessorBuilder.BuildGetter <T>(p, includePrivate)))); }); var bothMatch = true; var result = new KeyedCollectionEx <PropertyInfo, Variance>(variance => variance.Property); foreach (var pair in cache) { var p = pair.Key; var getter = (Func <T, object>)pair.Value; var leftVal = getter(left); var rightVal = getter(right); var variance = new Variance(p, leftVal, rightVal); result.Add(variance); if (!bothMatch) { continue; } if (variance.Varies) { bothMatch = false; } } variances = result; return(bothMatch); }