using System.Collections;
using System.Collections.Generic;
namespace GisSharpBlog.NetTopologySuite.Utilities{
/// <summary>
/// Executes a transformation function on each element of a collection
/// and returns the results in a new List.
/// </summary>
public class CollectionUtil
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public delegate T FunctionDelegate<T>(T obj);
/// <summary>
/// Executes a function on each item in a <see cref="ICollection" />
/// and returns the results in a new <see cref="IList" />.
/// </summary>
/// <param name="coll"></param>
/// <param name="func"></param>
/// <returns></returns>
public static IList Transform(ICollection coll, FunctionDelegate<object> func)
{
IList result = new List<object>();
IEnumerator i = coll.GetEnumerator();
foreach(object obj in coll)
result.Add(func(obj));
return result;
}
/// <summary>
/// Executes a function on each item in a <see cref="ICollection" />
/// but does not accumulate the result.
/// </summary>
/// <param name="coll"></param>
/// <param name="func"></param>
public static void Apply(ICollection coll, FunctionDelegate<object> func)
{
foreach(object obj in coll)
func(obj);
}
/// <summary>
/// Executes a function on each item in a <see cref="ICollection" />
/// and collects all the entries for which the result
/// of the function is equal to <c>true</c>.
/// </summary>
/// <param name="coll"></param>
/// <param name="func"></param>
/// <returns></returns>
public static IList Select(ICollection coll, FunctionDelegate<object> func)
{
IList result = new List<object>();
foreach (object obj in coll)
if (true.Equals(func(obj)))
result.Add(obj);
return result;
}
}
}
|