PageRenderTime 43ms CodeModel.GetById 34ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

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