namespace Extensions
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
/// <summary>Extension methods for the <see cref="ObservableCollection{T}"/> class.</summary>
public static class ObservableCollectionExtensions
{
/// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
/// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
/// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
/// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items)
{
Throw.IfNull("observableCollection", observableCollection);
Throw.IfNull("items", items);
foreach (T item in items)
observableCollection.Add(item);
}
/// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
/// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
/// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
/// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
/// <param name="filter">A filter to use to select specific values from the <paramref name="items"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items, Func<T, bool> filter)
{
Throw.IfNull("observableCollection", observableCollection);
Throw.IfNull("items", items);
foreach (T item in items.Where(filter))
observableCollection.Add(item);
}
/// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
/// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
/// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
/// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
/// <param name="filter">A filter to use to select specific values from the <paramref name="items"/>.</param>
/// <param name="transform">A transform to apply to each of the <paramref name="items"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items, Func<T, bool> filter, Func<T, T> transform)
{
Throw.IfNull("observableCollection", observableCollection);
Throw.IfNull("items", items);
foreach (T item in items.Where(filter))
observableCollection.Add(transform(item));
}
}
}