/src/Samples/EventStore/Shared/Util/Range.cs

https://github.com/JeetKunDoug/Lokad-Cqrs-Extensions
C# | 144 lines | 68 code | 16 blank | 60 comment | 7 complexity | 914eead14cf62843f340448d6e628a0f MD5 | raw file
  1. #region (c)2009 Lokad - New BSD license
  2. // Copyright (c) Lokad 2009
  3. // Company: http://www.lokad.com
  4. // This code is released under the terms of the new BSD licence
  5. #endregion
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace Shared.Util
  10. {
  11. /// <summary>
  12. /// Helper class with shortcut methods for managing enumerations.
  13. /// Useful for inlining object generation in tests
  14. /// </summary>
  15. public static class Range
  16. {
  17. /// <summary> Returns empty enumerator </summary>
  18. /// <typeparam name="T">type of the item to enumerate</typeparam>
  19. /// <returns>singleton instance of the empty enumerator</returns>
  20. public static IEnumerable<T> Empty<T>()
  21. {
  22. return Enumerable.Empty<T>();
  23. }
  24. /// <summary>
  25. /// returns enumeration from 0 with <paramref name="count"/> numbers
  26. /// </summary>
  27. /// <param name="count">Number of items to create</param>
  28. /// <returns>enumerable</returns>
  29. public static IEnumerable<int> Create(int count)
  30. {
  31. return Enumerable.Range(0, count);
  32. }
  33. /// <summary>
  34. /// Creates sequence of the integral numbers within the specified range
  35. /// </summary>
  36. /// <param name="start">The value of the first integer in sequence.</param>
  37. /// <param name="count">The number of values in the sequence.</param>
  38. /// <returns>sequence of the integral numbers within the specified range</returns>
  39. public static IEnumerable<int> Create(int start, int count)
  40. {
  41. return Enumerable.Range(start, count);
  42. }
  43. /// <summary>
  44. /// Creates sequence that consists of a repeated value.
  45. /// </summary>
  46. /// <typeparam name="TResult">The type of the value to repeat.</typeparam>
  47. /// <param name="item">The value to repeat.</param>
  48. /// <param name="count">The number of times to repeat.</param>
  49. /// <returns>sequence that consists of a repeated value</returns>
  50. public static IEnumerable<TResult> Repeat<TResult>(TResult item, int count)
  51. {
  52. return Enumerable.Repeat(item, count);
  53. }
  54. /// <summary>
  55. /// Creates the generator to iterate from 1 to <see cref="int.MaxValue"/>.
  56. /// </summary>
  57. /// <typeparam name="T">type of the item to generate</typeparam>
  58. /// <param name="generator">The generator.</param>
  59. /// <returns>new enumerator</returns>
  60. public static IEnumerable<T> Create<T>(Func<int, T> generator)
  61. {
  62. for (int i = 0; i < int.MaxValue; i++)
  63. {
  64. yield return generator(i);
  65. }
  66. throw new InvalidOperationException("Generator has reached the end");
  67. }
  68. /// <summary>
  69. /// Creates the enumerable using the provided generator.
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <param name="count">The count.</param>
  73. /// <param name="generator">The generator.</param>
  74. /// <returns>enumerable instance</returns>
  75. public static IEnumerable<T> Create<T>(int count, Func<int, T> generator)
  76. {
  77. for (int i = 0; i < count; i++)
  78. {
  79. yield return generator(i);
  80. }
  81. }
  82. /// <summary>
  83. /// Creates the enumerable using the provided generator.
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="count">The count.</param>
  87. /// <param name="generator">The generator.</param>
  88. /// <returns>enumerable instance</returns>
  89. public static IEnumerable<T> Create<T>(int count, Func<T> generator)
  90. {
  91. for (int i = 0; i < count; i++)
  92. {
  93. yield return generator();
  94. }
  95. }
  96. /// <summary>
  97. /// Creates the array populated with the provided generator
  98. /// </summary>
  99. /// <typeparam name="TValue">The type of the value.</typeparam>
  100. /// <param name="count">The count.</param>
  101. /// <param name="generator">The generator.</param>
  102. /// <returns>array</returns>
  103. public static TValue[] Array<TValue>(int count, Func<int, TValue> generator)
  104. {
  105. if (generator == null) throw new ArgumentNullException("generator");
  106. var array = new TValue[count];
  107. for (int i = 0; i < array.Length; i++)
  108. {
  109. array[i] = generator(i);
  110. }
  111. return array;
  112. }
  113. /// <summary>
  114. /// Creates the array of integers
  115. /// </summary>
  116. /// <param name="count">The count.</param>
  117. /// <returns></returns>
  118. public static int[] Array(int count)
  119. {
  120. var array = new int[count];
  121. for (int i = 0; i < array.Length; i++)
  122. {
  123. array[i] = i;
  124. }
  125. return array;
  126. }
  127. }
  128. }