Blame view

netcore/src/Modularity/Common/NCC.Common/Collections/CollectionExtensions.cs 2.64 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  using NCC.Dependency;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace NCC.Common.Collections
  {
      /// <summary>
      /// 集合扩展方法
      /// </summary>
      [SuppressSniffer]
      public static class CollectionExtensions
      {
          /// <summary>
          /// 如果条件成立,添加项
          /// </summary>
          public static void AddIf<T>(this ICollection<T> collection, T value, bool flag)
          {
              if (flag)
              {
                  collection.Add(value);
              }
          }
  
          /// <summary>
          /// 如果条件成立,添加项
          /// </summary>
          public static void AddIf<T>(this ICollection<T> collection, T value, Func<bool> func)
          {
              if (func())
              {
                  collection.Add(value);
              }
          }
  
          /// <summary>
          /// 如果不存在,添加项
          /// </summary>
          public static void AddIfNotExist<T>(this ICollection<T> collection, T value, Func<T, bool> existFunc = null)
          {
              bool exists = existFunc == null ? collection.Contains(value) : collection.Any(existFunc);
              if (!exists)
              {
                  collection.Add(value);
              }
          }
  
          /// <summary>
          /// 如果不为空,添加项
          /// </summary>
          public static void AddIfNotNull<T>(this ICollection<T> collection, T value) where T : class
          {
              if (value != null)
              {
                  collection.Add(value);
              }
          }
  
          /// <summary>
          /// 获取对象,不存在对使用委托添加对象
          /// </summary>
          public static T GetOrAdd<T>(this ICollection<T> collection, Func<T, bool> selector, Func<T> factory)
          {
              T item = collection.FirstOrDefault(selector);
              if (item == null)
              {
                  item = factory();
                  collection.Add(item);
              }
  
              return item;
          }
  
          /// <summary>
          /// 判断集合是否为null或空集合
          /// </summary>
          public static bool IsNullOrEmpty<T>(this ICollection<T> collection)
          {
              return collection == null || collection.Count == 0;
          }
  
          /// <summary>
          /// 交换两项的位置
          /// </summary>
          public static void Swap<T>(this List<T> list, int index1, int index2)
          {
              if (index1 == index2)
              {
                  return;
              }
  
              T tmp = list[index1];
              list[index1] = list[index2];
              list[index2] = tmp;
          }
      }
  }