[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / internal / XamlBinding / EnumerableExtensions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4
5 namespace Tizen.NUI.Binding
6 {
7     [EditorBrowsable(EditorBrowsableState.Never)]
8     internal static class EnumerableExtensions
9     {
10         public static IEnumerable<T> GetGesturesFor<T>(this IEnumerable<IGestureRecognizer> gestures, Func<T, bool> predicate = null) where T : GestureRecognizer
11         {
12             if (gestures == null)
13                 yield break;
14
15             if (predicate == null)
16                 predicate = x => true;
17
18             foreach (IGestureRecognizer item in gestures)
19             {
20                 var gesture = item as T;
21                 if (gesture != null && predicate(gesture))
22                 {
23                     yield return gesture;
24                 }
25             }
26         }
27
28         internal static IEnumerable<T> Append<T>(this IEnumerable<T> enumerable, T item)
29         {
30             foreach (T x in enumerable)
31                 yield return x;
32
33             yield return item;
34         }
35
36         public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
37         {
38             foreach (T item in enumeration)
39             {
40                 action(item);
41             }
42         }
43
44         public static int IndexOf<T>(this IEnumerable<T> enumerable, T item)
45         {
46             if (enumerable == null)
47                 throw new ArgumentNullException("enumerable");
48
49             var i = 0;
50             foreach (T element in enumerable)
51             {
52                 if (Equals(element, item))
53                     return i;
54
55                 i++;
56             }
57
58             return -1;
59         }
60
61         public static int IndexOf<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
62         {
63             var i = 0;
64             foreach (T element in enumerable)
65             {
66                 if (predicate(element))
67                     return i;
68
69                 i++;
70             }
71
72             return -1;
73         }
74
75         public static IEnumerable<T> Prepend<T>(this IEnumerable<T> enumerable, T item)
76         {
77             yield return item;
78
79             foreach (T x in enumerable)
80                 yield return x;
81         }
82     }
83 }