/src/LinFu.IoC/Configuration/TypeCounter.cs

http://github.com/philiplaureano/LinFu · C# · 113 lines · 70 code · 17 blank · 26 comment · 6 complexity · 160c3805951e4a74e6d617cbf857a4f1 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace LinFu.IoC.Configuration
  5. {
  6. /// <summary>
  7. /// Counts the number of occurrences of a specific type.
  8. /// </summary>
  9. internal class TypeCounter
  10. {
  11. private readonly Dictionary<int, Dictionary<Type, int>> _counts = new Dictionary<int, Dictionary<Type, int>>();
  12. /// <summary>
  13. /// Gets the value indicating the types that are
  14. /// currently being counted.
  15. /// </summary>
  16. public IEnumerable<Type> AvailableTypes
  17. {
  18. get
  19. {
  20. var threadId = Thread.CurrentThread.ManagedThreadId;
  21. if (!_counts.ContainsKey(threadId))
  22. return new Type[0];
  23. var results = new List<Type>();
  24. foreach (var type in _counts[threadId].Keys) results.Add(type);
  25. return results;
  26. }
  27. }
  28. /// <summary>
  29. /// Increments the count for the current <paramref name="type" />.
  30. /// </summary>
  31. /// <param name="type">The type being counted.</param>
  32. public void Increment(Type type)
  33. {
  34. var threadId = Thread.CurrentThread.ManagedThreadId;
  35. // Create a new counter, if necessary
  36. if (!_counts.ContainsKey(threadId))
  37. lock (_counts)
  38. {
  39. _counts[threadId] = new Dictionary<Type, int>();
  40. }
  41. var currentCounts = _counts[threadId];
  42. if (!currentCounts.ContainsKey(type))
  43. lock (currentCounts)
  44. {
  45. currentCounts[type] = 0;
  46. }
  47. lock (currentCounts)
  48. {
  49. currentCounts[type]++;
  50. }
  51. }
  52. /// <summary>
  53. /// Returns the number of occurrences of a specific <paramref name="type" />.
  54. /// </summary>
  55. /// <param name="type">The type being counted.</param>
  56. /// <returns>The number of occurrences for the given type.</returns>
  57. public int CountOf(Type type)
  58. {
  59. var threadId = Thread.CurrentThread.ManagedThreadId;
  60. if (!_counts.ContainsKey(threadId))
  61. return 0;
  62. var currentCounts = _counts[threadId];
  63. return currentCounts[type];
  64. }
  65. /// <summary>
  66. /// Decrements the count for the current <paramref name="type" />.
  67. /// </summary>
  68. /// <param name="type">The type being counted.</param>
  69. public void Decrement(Type type)
  70. {
  71. var currentCount = CountOf(type);
  72. if (currentCount > 0)
  73. currentCount--;
  74. var threadId = Thread.CurrentThread.ManagedThreadId;
  75. // Create a new counter, if necessary
  76. if (!_counts.ContainsKey(threadId))
  77. lock (_counts)
  78. {
  79. _counts[threadId] = new Dictionary<Type, int>();
  80. }
  81. // Split the counts by thread
  82. var currentCounts = _counts[threadId];
  83. lock (currentCounts)
  84. {
  85. currentCounts[type] = currentCount;
  86. }
  87. }
  88. /// <summary>
  89. /// Resets the counts back to zero.
  90. /// </summary>
  91. public void Reset()
  92. {
  93. _counts.Clear();
  94. }
  95. }
  96. }