Permalink
Browse files

Added DictionaryUtility and tests.

  • Loading branch information...
1 parent b12300a commit a156a28afa352c91512d2761d6a6060d7a894e7f @bgrainger bgrainger committed Feb 2, 2010
View
94 src/Logos.Utility/DictionaryUtility.cs
@@ -0,0 +1,94 @@
+
+using System;
+using System.Collections.Generic;
+
+namespace Logos.Utility
+{
+ /// <summary>
+ /// Provides methods for manipulating dictionaries.
+ /// </summary>
+ /// <remarks>See <a href="http://code.logos.com/blog/2008/02/getoraddvalue.html">GetOrAddValue</a>.</remarks>
+ public static class DictionaryUtility
+ {
+ /// <summary>
+ /// Gets a value from the dictionary, adding and returning a new instance if it is missing.
+ /// </summary>
+ /// <param name="dict">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <returns>The new or existing value.</returns>
+ public static TValue GetOrAddValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) where TValue : new()
+ {
+ TValue value;
+ if (dict.TryGetValue(key, out value))
+ return value;
+ value = new TValue();
+ dict.Add(key, value);
+ return value;
+ }
+
+ /// <summary>
+ /// Gets a value from the dictionary, adding and returning a new instance if it is missing.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="dict">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <param name="creator">Used to create a new value if necessary</param>
+ /// <returns>The new or existing value.</returns>
+ public static TValue GetOrAddValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TValue> creator)
+ {
+ TValue value;
+ if (dict.TryGetValue(key, out value))
+ return value;
+ value = creator();
+ dict.Add(key, value);
+ return value;
+ }
+
+ /// <summary>
+ /// Gets a value from the dictionary, returning a default value if it is missing.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="dict">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <returns>The value, or a default value.</returns>
+ public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
+ {
+ // specification for IDictionary<> requires that the returned value be the default if it fails
+ TValue value;
+ dict.TryGetValue(key, out value);
+ return value;
+ }
+
+ /// <summary>
+ /// Gets a value from the dictionary, returning the specified default value if it is missing.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="dict">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <param name="defaultValue">The default value.</param>
+ /// <returns>The value, or a default value.</returns>
+ public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
+ {
+ TValue value;
+ return dict.TryGetValue(key, out value) ? value : defaultValue;
+ }
+
+ /// <summary>
+ /// Gets a value from the dictionary, returning the generated default value if it is missing.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="dict">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <param name="defaultCreator">The default value generator.</param>
+ /// <returns>The value, or a default value.</returns>
+ public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TValue> defaultCreator)
+ {
+ TValue value;
+ return dict.TryGetValue(key, out value) ? value : defaultCreator();
+ }
+ }
+}
View
1 src/Logos.Utility/Logos.Utility.csproj
@@ -44,6 +44,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DictionaryUtility.cs" />
<Compile Include="ObjectImpl.cs" />
<Compile Include="ObjectUtility.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
View
52 tests/Logos.Utility.Tests/DictionaryUtilityTests.cs
@@ -0,0 +1,52 @@
+
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class DictionaryUtilityTests
+ {
+ [Test]
+ public void GetOrAddValueNew()
+ {
+ Dictionary<string, int> dict = new Dictionary<string, int>();
+ int i = dict.GetOrAddValue("test");
+ Assert.That(i, Is.EqualTo(0));
+ Assert.That(dict["test"], Is.EqualTo(0));
+ }
+
+ [Test]
+ public void GetOrAddValueCreator()
+ {
+ Dictionary<string, int> dict = new Dictionary<string, int>();
+ int i = dict.GetOrAddValue("test", () => 1);
+ Assert.That(i, Is.EqualTo(1));
+ Assert.That(dict["test"], Is.EqualTo(1));
+ }
+
+ [Test]
+ public void GetValueOrDefault()
+ {
+ Dictionary<string, int> dict = new Dictionary<string, int>();
+ int i = dict.GetValueOrDefault("test");
+ Assert.That(i, Is.EqualTo(0));
+ }
+
+ [Test]
+ public void GetValueOrDefaultValue()
+ {
+ Dictionary<string, int> dict = new Dictionary<string, int>();
+ int i = dict.GetValueOrDefault("test", 1);
+ Assert.That(i, Is.EqualTo(1));
+ }
+
+ [Test]
+ public void GetValueOrDefaultValueCreator()
+ {
+ Dictionary<string, int> dict = new Dictionary<string, int>();
+ int i = dict.GetValueOrDefault("test", () => 1);
+ Assert.That(i, Is.EqualTo(1));
+ }
+ }
+}
View
1 tests/Logos.Utility.Tests/Logos.Utility.Tests.csproj
@@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DictionaryUtilityTests.cs" />
<Compile Include="ObjectUtilityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScopeTests.cs" />

0 comments on commit a156a28

Please sign in to comment.