~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/DictionaryExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using ServiceStack.Common.Extensions;
 
4
 
 
5
namespace ServiceStack.Common
 
6
{
 
7
    public static class DictionaryExtensions
 
8
    {
 
9
        public static TValue GetValueOrDefault<TValue, TKey>(this Dictionary<TKey, TValue> dictionary, TKey key)
 
10
        {
 
11
            return dictionary.ContainsKey(key) ? dictionary[key] : default(TValue);
 
12
        }
 
13
 
 
14
        public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Action<TKey, TValue> onEachFn)
 
15
        {
 
16
            foreach (var entry in dictionary)
 
17
            {
 
18
                onEachFn(entry.Key, entry.Value);
 
19
            }
 
20
        }
 
21
 
 
22
        public static bool EquivalentTo<K, V>(this IDictionary<K, V> thisMap, IDictionary<K, V> otherMap)
 
23
        {
 
24
            if (thisMap == null || otherMap == null) return thisMap == otherMap;
 
25
            if (thisMap.Count != otherMap.Count) return false;
 
26
 
 
27
            foreach (var entry in thisMap)
 
28
            {
 
29
                V otherValue;
 
30
                if (!otherMap.TryGetValue(entry.Key, out otherValue)) return false;
 
31
                if (!Equals(entry.Value, otherValue)) return false;
 
32
            }
 
33
 
 
34
            return true;
 
35
        }
 
36
 
 
37
        public static List<T> ConvertAll<T, K, V>(IDictionary<K, V> map, Func<K, V, T> createFn)
 
38
        {
 
39
            var list = new List<T>();
 
40
            map.ForEach((kvp) => list.Add(createFn(kvp.Key, kvp.Value)));
 
41
            return list;
 
42
        }
 
43
        
 
44
        public static V GetOrAdd<K, V>(this Dictionary<K, V> map, K key, Func<K,V> createFn)
 
45
        {
 
46
            //simulate ConcurrentDictionary.GetOrAdd
 
47
            lock (map)
 
48
            {
 
49
                V val;
 
50
                if (!map.TryGetValue(key, out val))
 
51
                    map[key] = val = createFn(key);
 
52
 
 
53
                return val;
 
54
            }
 
55
        }
 
56
 
 
57
    }
 
58
}
 
 
b'\\ No newline at end of file'