示例#1
0
        /// <summary>
        /// Adds the items to the collection.
        /// </summary>
        /// <typeparam name="T">The type of T.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="items">The new items.</param>
        /// <param name="insureUnique">Set to true if items added to list are unique.</param>
        /// <exception cref="ArgumentNullException">items</exception>
        /// <exception cref="ArgumentException">items</exception>
        /// <exception cref="System.ArgumentNullException">items</exception>
        public static void AddRange <T>(this ICollection <T> list, IEnumerable <T> items, bool insureUnique = false)
        {
            if (list is null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            if (list.IsReadOnly)
            {
                throw new ArgumentException("List cannot be read-only.", nameof(list));
            }

            if (items.HasItems() == false)
            {
                throw new ArgumentNullException(nameof(items), $"{nameof(items)} is null.");
            }

            if (items.HasItems())
            {
                items.ToList().ForEach(item =>
                {
                    if (insureUnique)
                    {
                        list.AddIfNotExists(item);
                    }
                    else
                    {
                        list.Add(item);
                    }
                });
            }
        }
 /// <summary>
 /// Adds if not exists.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="list">The list.</param>
 /// <param name="values">The values.</param>
 /// <exception cref="ArgumentNullException">list - List cannot be null.
 /// or
 /// values - Values cannot be null.</exception>
 /// <exception cref="ArgumentException">list - List cannot be read-only.</exception>
 public static void AddIfNotExists <T>(this ICollection <T> list, params T[] values)
 {
     foreach (var value in values)
     {
         list.AddIfNotExists(value);
     }
 }
        public static bool AddRange <T>(this ICollection <T> collection, IEnumerable <T> items, Tristate ensureUnique = Tristate.False)
        {
            if (items.HasItems() == false)
            {
                return(false);
            }

            Trace.WriteLine(CreateCollectionIfNull(ref collection));

            Validate.TryValidateParam <ArgumentReadOnlyException>(collection.IsReadOnly == false, nameof(collection));

            var returnValue = false;

            if (items.HasItems())
            {
                items.ToList()
                .ForEach(item =>
                {
                    if (ensureUnique is Tristate.True or Tristate.UseDefault)
                    {
                        _           = collection.AddIfNotExists(item);
                        returnValue = true;
                    }
                    else
                    {
                        collection.Add(item);
                        returnValue = true;
                    }
                });
示例#4
0
        /// <summary>
        /// Adds if not exists.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <param name="values">The values.</param>
        /// <exception cref="ArgumentNullException">list - List cannot be null.
        /// or
        /// values - Values cannot be null.</exception>
        /// <exception cref="ArgumentException">list - List cannot be read-only.</exception>
        public static void AddIfNotExists <T>(this ICollection <T> list, params T[] values)
        {
            Encapsulation.TryValidateParam(values, nameof(values));

            foreach (var value in values)
            {
                list.AddIfNotExists(value);
            }
        }
        /// <summary>
        /// Adds if not exists.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The list.</param>
        /// <param name="values">The values.</param>
        public static void AddIfNotExists <T>(this ICollection <T> source, params T[] values)
        {
            // Encapsulation.TryValidateParam<ArgumentNullException>(values != null);

            foreach (var value in values.AsParallel())
            {
                source.AddIfNotExists(value);
            }
        }
        /// <summary>
        /// Adds if not exists.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <param name="values">The values.</param>
        /// <exception cref="ArgumentNullException">
        /// list - List cannot be null.
        /// or
        /// values - Values cannot be null.
        /// </exception>
        /// <exception cref="ArgumentException">list - List cannot be read-only.</exception>
        public static void AddIfNotExists <T>(this ICollection <T> list, params T[] values)
        {
            Encapsulation.TryValidateParam(values, nameof(values));
            Encapsulation.TryValidateParam <ArgumentNullException>(list.IsReadOnly, "List cannot be read-only.");

            foreach (var value in values)
            {
                list.AddIfNotExists(value);
            }
        }
示例#7
0
 /// <summary>
 /// Adds if not exists.
 /// </summary>
 /// <typeparam name="T">The type of T.</typeparam>
 /// <param name="list">The list.</param>
 /// <param name="items">The values.</param>
 /// <exception cref="ArgumentNullException">list - List cannot be null.
 /// or
 /// values - Values cannot be null.</exception>
 /// <exception cref="ArgumentException">list - List cannot be read-only.</exception>
 public static void AddIfNotExists <T>(this ICollection <T> list, params T[] items)
 {
     if (items.HasItems())
     {
         for (int i = 0; i < items.Length; i++)
         {
             list.AddIfNotExists(items[i]);
         }
     }
 }
 /// <summary>
 /// Adds if not exists.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The list.</param>
 /// <param name="values">The values.</param>
 public static void AddIfNotExists <T>(this ICollection <T> source, params T[] values)
 {
     if (values.IsValid())
     {
         foreach (var value in values)
         {
             source.AddIfNotExists(value);
         }
     }
 }
示例#9
0
        public void AddIfNotExists()
        {
            // Arrange
            _testCollection.Contains(Arg.Any <int>()).Returns(c => (int)c[0] > 0);

            // Act
            foreach (var i in _Int32TestData)
            {
                _testCollection.AddIfNotExists(i);
            }

            // Assert
            _testCollection.Received(5).Contains(Arg.Any <int>());
            _testCollection.Received(3).Add(Arg.Any <int>());
        }
示例#10
0
        /// <summary>
        /// Adds if not exists.
        /// </summary>
        /// <typeparam name="T">The type of T.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="items">The values.</param>
        /// <exception cref="ArgumentNullException">list - List cannot be null.
        /// or
        /// values - Values cannot be null.</exception>
        /// <exception cref="ArgumentException">list - List cannot be read-only.</exception>
        public static void AddIfNotExists <T>(this ICollection <T> list, params T[] items)
        {
            if (list is null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            if (list.IsReadOnly)
            {
                throw new ArgumentException("List cannot be read-only.", nameof(list));
            }

            if (items.HasItems())
            {
                for (int index = 0; index < items.Length; index++)
                {
                    list.AddIfNotExists(items[index]);
                }
            }
        }
        public static bool AddIfNotExists <T>([NotNull] this ICollection <T> collection, [NotNull] params T[] items)
        {
            items      = items.ArgumentNotNull();
            collection = collection.ArgumentNotNull().ArgumentNotReadOnly();

            var returnValue = false;

            if (items.HasItems())
            {
                for (var index = 0; index < items.Length; index++)
                {
                    if (items[index].IsNotNull())
                    {
                        _           = collection.AddIfNotExists(items[index]);
                        returnValue = true;
                    }
                }
            }

            return(returnValue);
        }
        public static bool AddIfNotExists <T>([NotNull] this ICollection <T> collection, [NotNull] params T[] items)
        {
            Trace.WriteLine(CreateCollectionIfNull(ref collection));

            Validate.TryValidateParam <ArgumentReadOnlyException>(collection.IsReadOnly == false, nameof(collection));

            var returnValue = false;

            if (items.HasItems())
            {
                for (var index = 0; index < items.Length; index++)
                {
                    if (items[index].IsNotNull())
                    {
                        _           = collection.AddIfNotExists(items[index]);
                        returnValue = true;
                    }
                }
            }

            return(returnValue);
        }
        public static bool AddRange <T>([NotNull] this ICollection <T> collection, [NotNull] IEnumerable <T> items, Tristate ensureUnique = Tristate.False)
        {
            items      = items.ArgumentNotNull();
            collection = collection.ArgumentNotNull().ArgumentNotReadOnly();

            var returnValue = false;

            if (items.HasItems())
            {
                items.ToList()
                .ForEach(item =>
                {
                    if (ensureUnique is Tristate.True or Tristate.UseDefault)
                    {
                        _           = collection.AddIfNotExists(item);
                        returnValue = true;
                    }
                    else
                    {
                        collection.Add(item);
                        returnValue = true;
                    }
                });