PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/WebGrease/WebGrease/Css/Extensions/ListExtensions.cs

http://webgrease.codeplex.com
C# | 101 lines | 62 code | 10 blank | 29 comment | 16 complexity | 096e6cbefc90507293f11c334d4c5582 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ListExtensions.cs" company="Microsoft">
  3. // Copyright Microsoft Corporation, all rights reserved
  4. // </copyright>
  5. // <summary>
  6. // ListExtensions Class - Provides the extension on List
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace WebGrease.Css.Extensions
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. /// <summary>ListExtensions Class - Provides the extension on List</summary>
  16. public static class ListExtensions
  17. {
  18. /// <summary>The generic version of AsReadOnly with null check</summary>
  19. /// <typeparam name="T">The type of items in collection</typeparam>
  20. /// <param name="list">The list of items</param>
  21. /// <returns>The safe readonly collection with null check</returns>
  22. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "This is by design.")]
  23. public static ReadOnlyCollection<T> AsSafeReadOnly<T>(this List<T> list)
  24. {
  25. return list != null ? list.AsReadOnly() : null;
  26. }
  27. /// <summary>For each extension method for IEnumerable</summary>
  28. /// <typeparam name="T">The type of items in collection</typeparam>
  29. /// <param name="list">The list of items</param>
  30. /// <param name="action">The action to perform on items</param>
  31. public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
  32. {
  33. if (list == null || action == null)
  34. {
  35. return;
  36. }
  37. foreach (var item in list)
  38. {
  39. action(item);
  40. }
  41. }
  42. /// <summary>For each extension which has an index</summary>
  43. /// <typeparam name="T">The type of <paramref name="action"/> elements.</typeparam>
  44. /// <param name="list">The enumerable</param>
  45. /// <param name="action">The action with type and index</param>
  46. public static void ForEach<T>(this IEnumerable<T> list, Action<T, int> action)
  47. {
  48. if (list == null || action == null)
  49. {
  50. return;
  51. }
  52. var count = 0;
  53. foreach (var item in list)
  54. {
  55. action(item, count);
  56. count++;
  57. }
  58. }
  59. /// <summary>For each extension which has a boolean which indicates the last index</summary>
  60. /// <typeparam name="T">The type of <paramref name="action"/> elements.</typeparam>
  61. /// <param name="list">The list of items</param>
  62. /// <param name="action">The action with type and bool</param>
  63. public static void ForEach<T>(this IList<T> list, Action<T, bool> action)
  64. {
  65. if (list == null || action == null)
  66. {
  67. return;
  68. }
  69. var count = 0;
  70. foreach (var item in list)
  71. {
  72. action(item, count < list.Count - 1 ? false : true);
  73. count++;
  74. }
  75. }
  76. /// <summary>Walks the enumerable and converts to read only collection.</summary>
  77. /// <param name="enumerable">The enumerable.</param>
  78. /// <typeparam name="T">The type of items in list.</typeparam>
  79. /// <returns>The read only collection.</returns>
  80. public static ReadOnlyCollection<T> ToSafeReadOnlyCollection<T>(this IEnumerable<T> enumerable)
  81. where T : class
  82. {
  83. if (enumerable == null)
  84. {
  85. return null;
  86. }
  87. var collection = new List<T>(enumerable.Where(_ => _ != null));
  88. return collection.AsReadOnly();
  89. }
  90. }
  91. }