public void AddIf() { // Arrange // Act _testCollection.AddIf(101, true); _testCollection.AddIf(-101, false); _testCollection.AddIf(1001, i => i > 0); _testCollection.AddIf(-1001, i => i > 0); // Assert _testCollection.Received(1).Add(101); _testCollection.Received(1).Add(1001); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="collection">Collection to add to</param> /// <param name="predicate"> /// Predicate used to determine if two values are equal. Return true if they are the same, /// false otherwise /// </param> /// <param name="items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> collection, Func <T, T, bool> predicate, IEnumerable <T> items) { return(!(collection is null) && !(predicate is null) && (items is null || collection.AddIf(x => !collection.Any(y => predicate(x, y)), items))); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="collection">Collection to add to</param> /// <param name="predicate">Predicate that an item needs to satisfy in order to be added</param> /// <param name="items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIf <T>(this ICollection <T> collection, Predicate <T> predicate, IEnumerable <T> items) { return(!(collection is null) && !(predicate is null) && (items?.Any() != true || collection.AddIf(predicate, items.ToArray()))); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> Collection, IEnumerable <T> Items) { Contract.Requires <ArgumentNullException>(Collection != null, "Collection"); if (Items == null) { return(true); } return(Collection.AddIf(x => !Collection.Contains(x), Items)); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <param name="Predicate"> /// Predicate used to determine if two values are equal. Return true if they are the same, /// false otherwise /// </param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> Collection, Func <T, T, bool> Predicate, IEnumerable <T> Items) { Contract.Requires <ArgumentNullException>(Collection != null, "Collection"); Contract.Requires <ArgumentNullException>(Predicate != null, "Predicate"); if (Items == null) { return(true); } return(Collection.AddIf(x => !Collection.Any(y => Predicate(x, y)), Items)); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <param name="Predicate">Predicate that an item needs to satisfy in order to be added</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIf <T>(this ICollection <T> Collection, Predicate <T> Predicate, IEnumerable <T> Items) { Contract.Requires <ArgumentNullException>(Collection != null, "Collection"); Contract.Requires <ArgumentNullException>(Predicate != null, "Predicate"); if (Items == null) { return(true); } return(Collection.AddIf(Predicate, Items.ToArray())); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <param name="Predicate">Predicate that an item needs to satisfy in order to be added</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIf <T>(this ICollection <T> Collection, IEnumerable <T> Items, Predicate <T> Predicate) { Collection.ThrowIfNull("Collection"); Predicate.ThrowIfNull("Predicate"); bool ReturnValue = false; foreach (T Item in Items) { ReturnValue |= Collection.AddIf(Item, Predicate); } return(ReturnValue); }
/// <summary> /// Match a similarity with a group, if applicable /// </summary> /// <param name="index">The index letter to change a word</param> /// <param name="group">The group where similarities will be added to</param> /// <param name="target">The target word to be found</param> /// <param name="source">The allowed contents that the search can be done at</param> /// <param name="similarityGroups">The similarity groups that were already found</param> /// <param name="alreadyKnown">The already found words</param> /// <param name="pathsContent">List of valid paths</param> /// <returns>True if target is found</returns> public virtual bool FindAndMatchSimilarities(int index, string group, string target, ConcurrentHashSet <string> source, ConcurrentDictionary <string, ICollection <string> > similarityGroups, ConcurrentHashSet <string> alreadyKnown, ICollection <string> pathsContent) { var targetFound = false; for (char k = 'a'; k <= 'z'; k++) { string similarity = SubstituteLetter(group, index, k); if (!source.Contains(similarity)) { _log.LogTrace("Similarity {similarity} does not exist in source, skipping.", similarity); continue; } _log.LogDebug("Adding similarity {similarity} to the list of known elements.", similarity); AddSimilarityToGroup(group, similarity, similarityGroups); pathsContent.AddIf(similarity, !alreadyKnown.Contains(similarity)); alreadyKnown.Add(similarity); if (similarity.Equals(target)) { _log.LogInformation("Target {target} was found from similarity of {group}!", target, group); targetFound = true; } } return(targetFound); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> Collection, IEnumerable <T> Items) { Collection.ThrowIfNull("Collection"); return(Collection.AddIf(x => !Collection.Contains(x), Items)); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="collection">Collection to add to</param> /// <param name="items">Items to add to the collection</param> /// <param name="predicate">Predicate that an item needs to satisfy in order to be added</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIf <T>(this ICollection <T> collection, Predicate <T> predicate, IEnumerable <T> items) { Guard.NotNull(collection, "collection"); Guard.NotNull(predicate, "predicate"); return(collection.AddIf(predicate, items.ToArray())); }
/// <summary> /// Adds an item to the collection if it is not already present. /// </summary> /// <typeparam name="T">The type of the collection's elements and the item to add to it.</typeparam> /// <param name="collection">The collection to which the item will be added.</param> /// <param name="item">The item to add to the collection.</param> public static void AddIfNew <T>(this ICollection <T> collection, T item) { collection.AddIf(item, queriedItem => !collection.Contains(queriedItem)); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="collection">Collection to add to</param> /// <param name="items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> collection, IEnumerable <T> items) { Guard.NotNull(collection, "collection"); return(collection.AddIf(x => !collection.Contains(x), items)); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="collection">Collection to add to</param> /// <param name="items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> collection, IEnumerable <T> items) { return(!(collection is null) && (items is null || collection.AddIf(x => !collection.Contains(x), items))); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> Collection, params T[] Items) { Contract.Requires <ArgumentNullException>(Collection != null, "Collection"); return(Collection.AddIf(x => !Collection.Contains(x), Items)); }
public static ICollection <T> AddIf <T>(this ICollection <T> collection, Func <T, bool> predicate, T item) => collection.AddIf <T>(predicate.Invoke(item), item);
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Items">Items to add to the collection</param> /// <param name="Predicate">Predicate that an item needs to satisfy in order to be added</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIf <T>(this ICollection <T> Collection, Predicate <T> Predicate, IEnumerable <T> Items) { Collection.ThrowIfNull("Collection"); Predicate.ThrowIfNull("Predicate"); return(Collection.AddIf(Predicate, Items.ToArray())); }
/// <summary> /// Adds an item to the collection if it isn't already in the collection /// </summary> /// <typeparam name="T">Collection type</typeparam> /// <param name="Collection">Collection to add to</param> /// <param name="Item">Item to add to the collection</param> /// <returns>True if it is added, false otherwise</returns> public static bool AddIfUnique <T>(this ICollection <T> Collection, T Item) { Collection.ThrowIfNull("Collection"); return(Collection.AddIf(Item, x => !Collection.Contains(x))); }