using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
namespace Umbraco.Core
{
///
/// Extension methods for dictionary & concurrentdictionary
///
internal static class DictionaryExtensions
{
///
/// Updates an item with the specified key with the specified value
///
///
///
///
///
///
///
///
/// Taken from: http://stackoverflow.com/questions/12240219/is-there-a-way-to-use-concurrentdictionary-tryupdate-with-a-lambda-expression
///
/// If there is an item in the dictionary with the key, it will keep trying to update it until it can
///
public static bool TryUpdate(this ConcurrentDictionary dict, TKey key, Func updateFactory)
{
TValue curValue;
while (dict.TryGetValue(key, out curValue))
{
if (dict.TryUpdate(key, updateFactory(curValue), curValue))
return true;
//if we're looping either the key was removed by another thread, or another thread
//changed the value, so we start again.
}
return false;
}
///
/// Updates an item with the specified key with the specified value
///
///
///
///
///
///
///
///
/// Taken from: http://stackoverflow.com/questions/12240219/is-there-a-way-to-use-concurrentdictionary-tryupdate-with-a-lambda-expression
///
/// WARNING: If the value changes after we've retreived it, then the item will not be updated
///
public static bool TryUpdateOptimisitic(this ConcurrentDictionary dict, TKey key, Func updateFactory)
{
TValue curValue;
if (!dict.TryGetValue(key, out curValue))
return false;
dict.TryUpdate(key, updateFactory(curValue), curValue);
return true;//note we return true whether we succeed or not, see explanation below.
}
///
/// Converts a dictionary to another type by only using direct casting
///
///
///
///
///
public static IDictionary ConvertTo(this IDictionary d)
{
var result = new Dictionary();
foreach (DictionaryEntry v in d)
{
result.Add((TKeyOut)v.Key, (TValOut)v.Value);
}
return result;
}
///
/// Converts a dictionary to another type using the specified converters
///
///
///
///
///
///
///
public static IDictionary ConvertTo(this IDictionary d, Func