MultiValueDictionary.cs :  » Testing » xUnit.net » Xunit » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » xUnit.net 
xUnit.net » Xunit » MultiValueDictionary.cs
using System.Collections.Generic;

namespace Xunit{
    /// <summary>
    /// A dictionary which contains multiple unique values for each key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    public class MultiValueDictionary<TKey, TValue>
    {
        Dictionary<TKey, List<TValue>> dictionary = new Dictionary<TKey, List<TValue>>();

        /// <summary>
        /// Gets the values for the given key.
        /// </summary>
        public IEnumerable<TValue> this[TKey key]
        {
            get { return dictionary[key]; }
        }

        /// <summary>
        /// Gets the count of the keys in the dictionary.
        /// </summary>
        public int Count
        {
            get { return dictionary.Keys.Count; }
        }

        /// <summary>
        /// Gets the keys.
        /// </summary>
        public IEnumerable<TKey> Keys
        {
            get { return dictionary.Keys; }
        }

        /// <summary>
        /// Adds the value for the given key. If the key does not exist in the
        /// dictionary yet, it will add it.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void AddValue(TKey key, TValue value)
        {
            List<TValue> items;

            if (!dictionary.TryGetValue(key, out items))
            {
                items = new List<TValue>();
                dictionary[key] = items;
            }

            if (!items.Contains(value))
                items.Add(value);
        }

        /// <summary>
        /// Removes all keys and values from the dictionary.
        /// </summary>
        public void Clear()
        {
            dictionary.Clear();
        }

        /// <summary>
        /// Determines whether the dictionary contains to specified key and value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public bool Contains(TKey key, TValue value)
        {
            List<TValue> items;

            if (!dictionary.TryGetValue(key, out items))
                return false;

            return items.Contains(value);
        }

        /// <summary/>
        public delegate void ForEachDelegate(TKey key, TValue value);

        /// <summary>
        /// Calls the delegate once for each key/value pair in the dictionary.
        /// </summary>
        public void ForEach(ForEachDelegate code)
        {
            foreach (TKey key in Keys)
                foreach (TValue value in this[key])
                    code(key, value);
        }

        /// <summary>
        /// Removes the given key and all of its values.
        /// </summary>
        public void Remove(TKey key)
        {
            dictionary.Remove(key);
        }

        /// <summary>
        /// Removes the given value from the given key. If this was the
        /// last value for the key, then the key is removed as well.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void RemoveValue(TKey key, TValue value)
        {
            List<TValue> items;

            if (!dictionary.TryGetValue(key, out items))
                return;

            items.Remove(value);

            if (items.Count == 0)
                dictionary.Remove(key);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.