瀏覽代碼

Merge branch 'master' of https://github.com/ldqk/Masuit.Tools

懒得勤快 7 年之前
父節點
當前提交
ca08013328

+ 17 - 2
Masuit.Tools.Core/Extensions.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using Masuit.Tools.Security;
@@ -202,7 +203,7 @@ namespace Masuit.Tools
                 Parallel.ForEach(objs, action);
             });
         }
-        
+
         /// <summary>
         /// 遍历数组
         /// </summary>
@@ -1425,7 +1426,7 @@ namespace Masuit.Tools
         /// <returns>匹配对象</returns>
         public static Match MatchPhoneNumber(this string s, out bool isMatch)
         {
-            Match match = Regex.Match(s, @"^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8])|(199))\d{8}$");
+            Match match = Regex.Match(s, @"^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8])|(19[8,9]))\d{8}$");
             isMatch = match.Success;
             return isMatch ? match : null;
         }
@@ -1534,5 +1535,19 @@ namespace Masuit.Tools
             }
             return temp;
         }
+
+        /// <summary>
+        /// 按字段去重
+        /// </summary>
+        /// <typeparam name="TSource"></typeparam>
+        /// <typeparam name="TKey"></typeparam>
+        /// <param name="source"></param>
+        /// <param name="keySelector"></param>
+        /// <returns></returns>
+        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+        {
+            HashSet<TKey> hash = new HashSet<TKey>();
+            return source.Where(p => hash.Add(keySelector(p)));
+        }
     }
 }

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>netcoreapp2.0</TargetFramework>
-    <Version>1.9.1.4</Version>
+    <Version>1.9.3.1</Version>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

+ 35 - 0
Masuit.Tools.Core/Systems/ConcurrentLimitedQueue.cs

@@ -0,0 +1,35 @@
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Core.Systems
+{
+    /// <summary>
+    /// 定长队列
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class ConcurrentLimitedQueue<T> : ConcurrentQueue<T>
+    {
+        public int Limit { get; set; }
+
+        public ConcurrentLimitedQueue(int limit)
+        {
+            Limit = limit;
+        }
+
+        public ConcurrentLimitedQueue(IEnumerable<T> list) : base(list)
+        {
+            Limit = list.Count();
+        }
+
+        public new void Enqueue(T item)
+        {
+            if (Count >= Limit)
+            {
+                TryDequeue(out var _);
+            }
+
+            base.Enqueue(item);
+        }
+    }
+}

+ 28 - 0
Masuit.Tools.Core/Systems/LimitedQueue.cs

@@ -0,0 +1,28 @@
+using System.Collections.Generic;
+
+namespace Masuit.Tools.Core.Systems
+{
+    /// <summary>
+    /// 定长队列
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class LimitedQueue<T> : Queue<T>
+    {
+        public int Limit { get; set; }
+
+        public LimitedQueue(int limit) : base(limit)
+        {
+            Limit = limit;
+        }
+
+        public new void Enqueue(T item)
+        {
+            if (Count >= Limit)
+            {
+                Dequeue();
+            }
+
+            base.Enqueue(item);
+        }
+    }
+}

+ 17 - 2
Masuit.Tools/Extensions.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using Masuit.Tools.Security;
@@ -202,7 +203,7 @@ namespace Masuit.Tools
                 Parallel.ForEach(objs, action);
             });
         }
-        
+
         /// <summary>
         /// 遍历数组
         /// </summary>
@@ -1440,7 +1441,7 @@ namespace Masuit.Tools
         /// <returns>匹配对象</returns>
         public static Match MatchPhoneNumber(this string s, out bool isMatch)
         {
-            Match match = Regex.Match(s, @"^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8])|(199))\d{8}$");
+            Match match = Regex.Match(s, @"^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8])|(19[8,9]))\d{8}$");
             isMatch = match.Success;
             return isMatch ? match : null;
         }
@@ -1549,5 +1550,19 @@ namespace Masuit.Tools
             }
             return temp;
         }
+
+        /// <summary>
+        /// 按字段去重
+        /// </summary>
+        /// <typeparam name="TSource"></typeparam>
+        /// <typeparam name="TKey"></typeparam>
+        /// <param name="source"></param>
+        /// <param name="keySelector"></param>
+        /// <returns></returns>
+        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+        {
+            HashSet<TKey> hash = new HashSet<TKey>();
+            return source.Where(p => hash.Add(keySelector(p)));
+        }
     }
 }

+ 2 - 0
Masuit.Tools/Masuit.Tools.csproj

@@ -113,6 +113,8 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Security\HashEncode.cs" />
     <Compile Include="Security\RSACrypt.cs" />
+    <Compile Include="Systems\ConcurrentLimitedQueue.cs" />
+    <Compile Include="Systems\LimitedQueue.cs" />
     <Compile Include="Strings\ValidateCode.cs" />
     <Compile Include="Net\WebExtension.cs" />
     <Compile Include="Systems\EnumExt.cs" />

+ 2 - 2
Masuit.Tools/Properties/AssemblyInfo.cs

@@ -36,7 +36,7 @@ using System.Runtime.InteropServices;
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
 
-[assembly: AssemblyVersion("1.9.1")]
-[assembly: AssemblyFileVersion("1.9.1")]
+[assembly: AssemblyVersion("1.9.3.1")]
+[assembly: AssemblyFileVersion("1.9.3.1")]
 [assembly: NeutralResourcesLanguage("zh-CN")]
 

+ 35 - 0
Masuit.Tools/Systems/ConcurrentLimitedQueue.cs

@@ -0,0 +1,35 @@
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Systems
+{
+    /// <summary>
+    /// 定长队列
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class ConcurrentLimitedQueue<T> : ConcurrentQueue<T>
+    {
+        public int Limit { get; set; }
+
+        public ConcurrentLimitedQueue(int limit)
+        {
+            Limit = limit;
+        }
+
+        public ConcurrentLimitedQueue(IEnumerable<T> list) : base(list)
+        {
+            Limit = list.Count();
+        }
+
+        public new void Enqueue(T item)
+        {
+            if (Count >= Limit)
+            {
+                TryDequeue(out var _);
+            }
+
+            base.Enqueue(item);
+        }
+    }
+}

+ 28 - 0
Masuit.Tools/Systems/LimitedQueue.cs

@@ -0,0 +1,28 @@
+using System.Collections.Generic;
+
+namespace Masuit.Tools.Systems
+{
+    /// <summary>
+    /// 定长队列
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class LimitedQueue<T> : Queue<T>
+    {
+        public int Limit { get; set; }
+
+        public LimitedQueue(int limit) : base(limit)
+        {
+            Limit = limit;
+        }
+
+        public new void Enqueue(T item)
+        {
+            if (Count >= Limit)
+            {
+                Dequeue();
+            }
+
+            base.Enqueue(item);
+        }
+    }
+}

+ 7 - 0
Test/Program.cs

@@ -83,10 +83,17 @@ namespace Test
             //LogManager.Debug("bbbbbbbbbbbbbbbbb");
             //LogManager.Error(typeof(object), "bbbbbbbbbbbbbbbbb");
             //LogManager.Info("aaaaaaaaaaaaaaaaaaaaaaaaa");
+
+
             Console.ReadKey();
         }
     }
 
+    public class Banlance
+    {
+        public string Name { get; set; }
+        public int Amount { get; set; }
+    }
     public class MyClass
     {
         public string Name { get; set; }