using System;
using System.Collections;
using System.Collections.Generic;
using Proxy = ServiceStack.Common.EnumerableExtensions;
namespace ServiceStack.Common.Extensions
{
///
/// These extensions have a potential to conflict with the LINQ extensions methods so
/// leaving the implmentation in the 'Extensions' sub-namespace to force explicit opt-in
///
public static class EnumerableExtensions
{
public static void ForEach(this IEnumerable values, Action action)
{
foreach (var value in values)
{
action(value);
}
}
public static List ConvertAll(this IEnumerable items, Func converter)
{
var list = new List();
foreach (var item in items)
{
list.Add(converter(item));
}
return list;
}
public static object First(this IEnumerable items)
{
foreach (var item in items)
{
return item;
}
return null;
}
public static List ToList(this IEnumerable items)
{
var list = new List();
foreach (var item in items)
{
list.Add((To)item);
}
return list;
}
public static List ConvertAll(this IEnumerable items, Func converter)
{
var list = new List();
foreach (var item in items)
{
list.Add(converter(item));
}
return list;
}
public static HashSet ToHashSet(this IEnumerable items)
{
return Proxy.ToHashSet(items);
}
public static List SafeConvertAll(this IEnumerable items, Func converter)
{
return Proxy.SafeConvertAll(items, converter);
}
public static List ToObjects(this IEnumerable items)
{
var to = new List();
foreach (var item in items)
{
to.Add(item);
}
return to;
}
public static string FirstNonDefaultOrEmpty(this IEnumerable values)
{
foreach (var value in values)
{
if (!string.IsNullOrEmpty(value)) return value;
}
return null;
}
public static T FirstNonDefault(this IEnumerable values)
{
foreach (var value in values)
{
if (!Equals(value, default(T))) return value;
}
return default(T);
}
public static bool EquivalentTo(this IEnumerable thisList, IEnumerable otherList)
{
if (thisList == null || otherList == null) return thisList == otherList;
var otherEnum = otherList.GetEnumerator();
foreach (var item in thisList)
{
if (!otherEnum.MoveNext()) return false;
var thisIsDefault = Equals(item, default(T));
var otherIsDefault = Equals(otherEnum.Current, default(T));
if (thisIsDefault || otherIsDefault)
{
return thisIsDefault && otherIsDefault;
}
if (!item.Equals(otherEnum.Current)) return false;
}
var hasNoMoreLeftAsWell = !otherEnum.MoveNext();
return hasNoMoreLeftAsWell;
}
}
}