namespace UIWidgets.Extensions { using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Text; /// /// For each extensions. /// public static class Extensions { /// /// For each with index. /// /// Enumerable. /// Handler. /// The 1st type parameter. [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0401:Possible allocation of reference type enumerator", Justification = "Required.")] public static void ForEach(this IEnumerable enumerable, Action handler) { int i = 0; foreach (T item in enumerable) { handler(item, i); i++; } } /// /// For each with index. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this T[] enumerable, Action handler) { int i = 0; foreach (T item in enumerable) { handler(item, i); i++; } } /// /// For each with index. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this List enumerable, Action handler) { int i = 0; foreach (T item in enumerable) { handler(item, i); i++; } } /// /// For each with index. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this ObservableList enumerable, Action handler) { int i = 0; foreach (T item in enumerable) { handler(item, i); i++; } } /// /// For each. /// /// Enumerable. /// Handler. /// The 1st type parameter. [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0401:Possible allocation of reference type enumerator", Justification = "Required.")] public static void ForEach(this IEnumerable enumerable, Action handler) { foreach (T item in enumerable) { handler(item); } } /// /// For each. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this T[] enumerable, Action handler) { foreach (T item in enumerable) { handler(item); } } /// /// For each. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this List enumerable, Action handler) { foreach (T item in enumerable) { handler(item); } } /// /// For each. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this ObservableList enumerable, Action handler) { foreach (T item in enumerable) { handler(item); } } /// /// Convert IEnumerable{T} to ObservableList{T}. /// /// The observable list. /// Enumerable. /// Is need to observe items? /// The 1st type parameter. public static ObservableList ToObservableList(this IEnumerable enumerable, bool observeItems = true) { return new ObservableList(enumerable, observeItems); } /// /// Convert IEnumerable{T} to ObservableList{T}. /// /// The observable list. /// Enumerable. /// Is need to observe items? /// The 1st type parameter. public static ObservableList ToObservableList(this T[] enumerable, bool observeItems = true) { return new ObservableList(enumerable, observeItems); } /// /// Convert IEnumerable{T} to ObservableList{T}. /// /// The observable list. /// Enumerable. /// Is need to observe items? /// The 1st type parameter. public static ObservableList ToObservableList(this List enumerable, bool observeItems = true) { return new ObservableList(enumerable, observeItems); } /// /// Convert IEnumerable{T} to ObservableList{T}. /// /// The observable list. /// Enumerable. /// Is need to observe items? /// The 1st type parameter. public static ObservableList ToObservableList(this ObservableList enumerable, bool observeItems = true) { return new ObservableList(enumerable, observeItems); } /// /// Sums the float. /// /// The float. /// List. [Obsolete("Will be removed in next releases.")] public static float SumFloat(this IList list) { var result = 0f; for (int i = 0; i < list.Count; i++) { result += list[i]; } return result; } /// /// Sums the float. /// /// The float. /// List. /// Calculate. /// The 1st type parameter. [Obsolete("Will be removed in next releases.")] public static float SumFloat(this IList list, Func calculate) { var result = 0f; for (int i = 0; i < list.Count; i++) { result += calculate(list[i]); } return result; } /// /// Convert the specified list with converter. /// /// Input. /// Converter. /// The 1st type parameter. /// The 2nd type parameter. /// List with converted items. public static List Convert(this List input, Converter converter) { #if NETFX_CORE var output = new List(input.Count); foreach (var item in input) { output.Add(converter(item)); } return output; #else return input.ConvertAll(converter); #endif } /// /// Gets all sub nodes. /// /// The all sub nodes. /// Nodes. /// The 1st type parameter. public static List> GetAllSubNodes(this ObservableList> nodes) { var all_subnodes = new List>(); foreach (var node in nodes) { GetSubNodes(node.Nodes, all_subnodes); } return all_subnodes; } /// /// Gets the sub nodes from specified nodes list. /// /// Nodes. /// Sub nodes. /// The 1st type parameter. public static void GetSubNodes(ObservableList> nodes, List> subnodes) { if (nodes == null) { return; } foreach (var subnode in nodes) { subnodes.Add(subnode); GetSubNodes(subnode.Nodes, subnodes); } } /// /// Convert list to the string. /// /// The string. /// List. /// Format. /// The 1st type parameter. public static string ToString(this IList list, string format) where T : IFormattable { return list.ToString(format, CultureInfo.CurrentCulture); } /// /// Convert list to the string. /// /// The string. /// List. /// Format. /// Format provider. /// The 1st type parameter. public static string ToString(this IList list, string format, IFormatProvider formatProvider) where T : IFormattable { if (list.Count == 0) { return string.Empty; } var result = new StringBuilder(list.Count); for (int i = 0; i < list.Count; i++) { result.Append(string.Format(format, list[i].ToString(), formatProvider)); } return result.ToString(); } /// /// Convert list to the string. /// /// The string. /// List. /// Format. /// Argument 1 object. /// Format provider. /// The 1st type parameter. [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0601:Value type to reference type conversion causing boxing allocation", Justification = "Required.")] public static string ToString(this IList list, string format, object arg1, IFormatProvider formatProvider) where T : IFormattable { if (list.Count == 0) { return string.Empty; } var result = new StringBuilder(list.Count); for (int i = 0; i < list.Count; i++) { result.Append(string.Format(format, list[i], arg1, formatProvider)); } return result.ToString(); } /// /// Remove and return the item in the end of the list. /// /// Type of the item. /// List. /// Last item. public static T Pop(this IList list) { var n = list.Count - 1; var result = list[n]; list.RemoveAt(n); return result; } #if NETFX_CORE /// /// Determines if is assignable from the specified source from. /// /// true if is assignable from the specified source from; otherwise, false. /// Source. /// From. static public bool IsAssignableFrom(this Type source, Type from) { return source.GetTypeInfo().IsAssignableFrom(from.GetTypeInfo()); } /// /// Apply action for each item in list. /// /// List. /// Action. /// The 1st type parameter. static public void ForEach(this List list, Action action) { for (int i = 0; i < list.Count; i++) { action(list[i]); } } #endif } }