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

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/EnumerableExtensions.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
 
 
4
namespace ServiceStack.Common
 
5
{
 
6
    public static class EnumerableExtensions
 
7
    {
 
8
        public static bool IsEmpty<T>(this ICollection<T> collection)
 
9
        {
 
10
            return collection == null || collection.Count == 0;
 
11
        }
 
12
 
 
13
        public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items)
 
14
        {
 
15
            return new HashSet<T>(items);
 
16
        }
 
17
 
 
18
        public static List<To> SafeConvertAll<To, From>(this IEnumerable<From> items, Func<From, To> converter)
 
19
        {
 
20
            return items == null ? new List<To>() : Extensions.EnumerableExtensions.ConvertAll(items, converter);
 
21
        }
 
22
 
 
23
        public static List<object> ToObjects<T>(this IEnumerable<T> items)
 
24
        {
 
25
            var to = new List<object>();
 
26
            foreach (var item in items)
 
27
            {
 
28
                to.Add(item);
 
29
            }
 
30
            return to;
 
31
        }
 
32
 
 
33
        public static string FirstNonDefaultOrEmpty(this IEnumerable<string> values)
 
34
        {
 
35
            foreach (var value in values)
 
36
            {
 
37
                if (!string.IsNullOrEmpty(value)) return value;
 
38
            }
 
39
            return null;
 
40
        }
 
41
 
 
42
        public static T FirstNonDefault<T>(this IEnumerable<T> values)
 
43
        {
 
44
            foreach (var value in values)
 
45
            {
 
46
                if (!Equals(value, default(T))) return value;
 
47
            }
 
48
            return default(T);
 
49
        }
 
50
 
 
51
        public static bool EquivalentTo<T>(this IEnumerable<T> thisList, IEnumerable<T> otherList)
 
52
        {
 
53
            if (thisList == null || otherList == null) return thisList == otherList;
 
54
 
 
55
            var otherEnum = otherList.GetEnumerator();
 
56
            foreach (var item in thisList)
 
57
            {
 
58
                if (!otherEnum.MoveNext()) return false;
 
59
 
 
60
                var thisIsDefault = Equals(item, default(T));
 
61
                var otherIsDefault = Equals(otherEnum.Current, default(T));
 
62
                if (thisIsDefault || otherIsDefault)
 
63
                {
 
64
                    return thisIsDefault && otherIsDefault;
 
65
                }
 
66
 
 
67
                if (!item.Equals(otherEnum.Current)) return false;
 
68
            }
 
69
            var hasNoMoreLeftAsWell = !otherEnum.MoveNext();
 
70
            return hasNoMoreLeftAsWell;
 
71
        }
 
72
 
 
73
        public static IEnumerable<T[]> BatchesOf<T>(this IEnumerable<T> sequence, int batchSize)
 
74
        {
 
75
            var batch = new List<T>(batchSize);
 
76
            foreach (var item in sequence)
 
77
            {
 
78
                batch.Add(item);
 
79
                if (batch.Count >= batchSize)
 
80
                {
 
81
                    yield return batch.ToArray();
 
82
                    batch.Clear();
 
83
                }
 
84
            }
 
85
 
 
86
            if (batch.Count > 0)
 
87
            {
 
88
                yield return batch.ToArray();
 
89
                batch.Clear();
 
90
            }
 
91
        }
 
92
 
 
93
        public static Dictionary<TKey, T> ToSafeDictionary<T, TKey>(this IEnumerable<T> list, Func<T, TKey> expr)
 
94
        {
 
95
            var map = new Dictionary<TKey, T>();
 
96
            if (list != null)
 
97
            {
 
98
                foreach (var item in list)
 
99
                {
 
100
                    map[expr(item)] = item;
 
101
                }
 
102
            }
 
103
            return map;
 
104
        }
 
105
        
 
106
    }
 
107
}
 
 
b'\\ No newline at end of file'