develop
soul liu 2 years ago
parent a44cfba5ac
commit 610b5786e2
  1. 20
      General/utils/CollectionsUtils.cs
  2. 69
      General/utils/StringUtils.cs
  3. 98
      General/utils/TimerUtils.cs

@ -5,23 +5,19 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Soul2.General.utils namespace Soul2.General.utils {
{ public class CollectionsUtils {
public class CollectionsUtils
{ public static bool isNotEmpty(IEnumerable collection) {
public static bool isNotEmpty(IEnumerable collection)
{
return !isEmpty(collection); return !isEmpty(collection);
} }
public static bool isEmpty(IEnumerable collection)
{ public static bool isEmpty(IEnumerable collection) {
if (collection == null) if (collection == null) {
{
return true; return true;
} }
foreach (var item in collection) foreach (var item in collection) {
{
return false; return false;
} }
return true; return true;

@ -4,25 +4,21 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Soul2.General.utils namespace Soul2.General.utils {
{
/// <summary> /// <summary>
/// 字符串工具类 /// 字符串工具类
/// By Soul2 /// By Soul2
/// </summary> /// </summary>
public static class StringUtils public static class StringUtils {
{
/// <summary> /// <summary>
/// 判断空值(null或"") /// 判断空值(null或"")
/// </summary> /// </summary>
/// <param name="str"></param> /// <param name="str"></param>
/// <returns></returns> /// <returns></returns>
public static bool isEmpty(this string str) public static bool isEmpty(this string str) {
{
return string.IsNullOrEmpty(str); return string.IsNullOrEmpty(str);
} }
public static bool isNotEmpty(this string str) public static bool isNotEmpty(this string str) {
{
return !string.IsNullOrEmpty(str); return !string.IsNullOrEmpty(str);
} }
@ -41,12 +37,9 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="strs"></param> /// <param name="strs"></param>
/// <returns></returns> /// <returns></returns>
public static bool isAnyBlank(params string[] strs) public static bool isAnyBlank(params string[] strs) {
{ foreach (var str in strs) {
foreach (var str in strs) if (string.IsNullOrWhiteSpace(str)) {
{
if (string.IsNullOrWhiteSpace(str))
{
return true; return true;
} }
} }
@ -59,12 +52,9 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="strs"></param> /// <param name="strs"></param>
/// <returns></returns> /// <returns></returns>
public static bool isAnyEmpty(params string[] strs) public static bool isAnyEmpty(params string[] strs) {
{ foreach (var str in strs) {
foreach (var str in strs) if (string.IsNullOrEmpty(str)) {
{
if (string.IsNullOrEmpty(str))
{
return true; return true;
} }
} }
@ -76,12 +66,9 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="strs"></param> /// <param name="strs"></param>
/// <returns></returns> /// <returns></returns>
public static bool isNoneBlank(params string[] strs) public static bool isNoneBlank(params string[] strs) {
{ foreach (var str in strs) {
foreach (var str in strs) if (string.IsNullOrWhiteSpace(str)) {
{
if (string.IsNullOrWhiteSpace(str))
{
return false; return false;
} }
} }
@ -94,12 +81,9 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="strs"></param> /// <param name="strs"></param>
/// <returns></returns> /// <returns></returns>
public static bool isNoneEmpty(params string[] strs) public static bool isNoneEmpty(params string[] strs) {
{ foreach (var str in strs) {
foreach (var str in strs) if (string.IsNullOrEmpty(str)) {
{
if (string.IsNullOrEmpty(str))
{
return false; return false;
} }
} }
@ -111,10 +95,8 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="original"></param> /// <param name="original"></param>
/// <param name="str"></param> /// <param name="str"></param>
public static void appendIfMissing(this string original, string str) public static void appendIfMissing(this string original, string str) {
{ if (!original.EndsWith(str)) {
if (!original.EndsWith(str))
{
original += str; original += str;
} }
} }
@ -124,10 +106,8 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="original"></param> /// <param name="original"></param>
/// <param name="str"></param> /// <param name="str"></param>
public static void prependIfMissing(this string original, string str) public static void prependIfMissing(this string original, string str) {
{ if (!original.StartsWith(str)) {
if (!original.StartsWith(str))
{
original = str + original; original = str + original;
} }
} }
@ -138,17 +118,14 @@ namespace Soul2.General.utils
/// </summary> /// </summary>
/// <param name="original"></param> /// <param name="original"></param>
/// <returns></returns> /// <returns></returns>
public static string flip(this string original) public static string flip(this string original) {
{
char[] chars = original.ToCharArray(); char[] chars = original.ToCharArray();
Array.Reverse(chars); Array.Reverse(chars);
return new string(chars); return new string(chars);
} }
public static string subStrWith(this string original, int begin, int end) public static string subStrWith(this string original, int begin, int end) {
{ if (begin < 0 || end > original.Length || begin > end) {
if (begin < 0 || end > original.Length || begin > end)
{
throw new ArgumentException("Invalid input parameters"); throw new ArgumentException("Invalid input parameters");
} }
return original.Substring(begin, end - begin); return original.Substring(begin, end - begin);

@ -6,14 +6,12 @@ using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Timer = System.Timers.Timer; using Timer = System.Timers.Timer;
namespace Soul2.General.utils namespace Soul2.General.utils {
{
/// <summary> /// <summary>
/// 扩展System.Timers.Timer /// 扩展System.Timers.Timer
/// By Soul2 /// By Soul2
/// </summary> /// </summary>
public static class TimerUtils public static class TimerUtils {
{
private const int default_loop_times = 512; private const int default_loop_times = 512;
/// <summary> /// <summary>
@ -24,17 +22,14 @@ namespace Soul2.General.utils
/// <param name="time"></param> /// <param name="time"></param>
/// <param name="callback"></param> /// <param name="callback"></param>
/// <returns></returns> /// <returns></returns>
public static Timer startOnce(this Timer timer, double time, Action<Timer> callback) public static Timer startOnce(this Timer timer, double time, Action<Timer> callback) {
{ void onElapsed(object source, ElapsedEventArgs e) {
void onElapsed(object source, ElapsedEventArgs e)
{
callback(timer); callback(timer);
timer.destroy(); timer.destroy();
} }
return run(timer, (int)(time * 1000), false, onElapsed); return run(timer, (int)(time * 1000), false, onElapsed);
} }
public static Timer startOnce(this Timer timer, double time, Action callback) public static Timer startOnce(this Timer timer, double time, Action callback) {
{
return timer.startOnce(time, (t) => callback?.Invoke()); return timer.startOnce(time, (t) => callback?.Invoke());
} }
@ -48,36 +43,28 @@ namespace Soul2.General.utils
/// <param name="times">次数</param> /// <param name="times">次数</param>
/// <param name="callback"></param> /// <param name="callback"></param>
/// <returns></returns> /// <returns></returns>
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer, int> callback) public static Timer startLoop(this Timer timer, double time, int times, Action<Timer, int> callback) {
{
int run_times = 0; int run_times = 0;
void onElapsed(object source, ElapsedEventArgs e) void onElapsed(object source, ElapsedEventArgs e) {
{
run_times += 1; run_times += 1;
if (run_times <= times) if (run_times <= times) {
{
callback(timer, run_times); callback(timer, run_times);
} } else {
else
{
timer.destroy(); timer.destroy();
} }
} }
return run(timer, (int)(time * 1000), true, onElapsed); return run(timer, (int)(time * 1000), true, onElapsed);
} }
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer> callback) public static Timer startLoop(this Timer timer, double time, int times, Action<Timer> callback) {
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke(t)); return timer.startLoop(time, times, (t, i) => callback?.Invoke(t));
} }
public static Timer startLoop(this Timer timer, double time, int times, Action<int> callback) public static Timer startLoop(this Timer timer, double time, int times, Action<int> callback) {
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke(i)); return timer.startLoop(time, times, (t, i) => callback?.Invoke(i));
} }
public static Timer startLoop(this Timer timer, double time, int times, Action callback) public static Timer startLoop(this Timer timer, double time, int times, Action callback) {
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke()); return timer.startLoop(time, times, (t, i) => callback?.Invoke());
} }
@ -90,40 +77,30 @@ namespace Soul2.General.utils
/// <param name="times">次数</param> /// <param name="times">次数</param>
/// <param name="callback"></param> /// <param name="callback"></param>
/// <returns></returns> /// <returns></returns>
public static Timer startKeep(this Timer timer, double time, Action<Timer, int> callback, int times = default_loop_times) public static Timer startKeep(this Timer timer, double time, Action<Timer, int> callback, int times = default_loop_times) {
{
int run_times = 0; int run_times = 0;
void onElapsed(object source, ElapsedEventArgs e) void onElapsed(object source, ElapsedEventArgs e) {
{
run_times += 1; run_times += 1;
if (run_times <= times) if (run_times <= times) {
{
callback(timer, run_times); callback(timer, run_times);
} } else {
else
{
timer.destroy(); timer.destroy();
} }
} }
return run(timer, (int)(time * 1000), true, onElapsed); return run(timer, (int)(time * 1000), true, onElapsed);
} }
public static Timer startKeep(this Timer timer, double time, Action<int> callback, int times = default_loop_times) public static Timer startKeep(this Timer timer, double time, Action<int> callback, int times = default_loop_times) {
{
return timer.startKeep(time, (t, i) => callback?.Invoke(i)); return timer.startKeep(time, (t, i) => callback?.Invoke(i));
} }
public static Timer startKeep(this Timer timer, double time, Action<Timer> callback, int times = default_loop_times) public static Timer startKeep(this Timer timer, double time, Action<Timer> callback, int times = default_loop_times) {
{
return timer.startKeep(time, (t, i) => callback?.Invoke(t)); return timer.startKeep(time, (t, i) => callback?.Invoke(t));
} }
public static Timer startKeep(this Timer timer, double time, Action callback, int times = default_loop_times) public static Timer startKeep(this Timer timer, double time, Action callback, int times = default_loop_times) {
{
return timer.startKeep(time, (t, i) => callback?.Invoke()); return timer.startKeep(time, (t, i) => callback?.Invoke());
} }
public static void destroy(this Timer timer) public static void destroy(this Timer timer) {
{ if (timer.Enabled) {
if (timer.Enabled)
{
timer.Stop(); timer.Stop();
} }
timer.Dispose(); timer.Dispose();
@ -131,8 +108,7 @@ namespace Soul2.General.utils
// ----------------------------------------- // -----------------------------------------
private static Timer run(Timer timer, int interval, bool loop, ElapsedEventHandler elapsed) private static Timer run(Timer timer, int interval, bool loop, ElapsedEventHandler elapsed) {
{
timer.Elapsed += elapsed; timer.Elapsed += elapsed;
timer.Interval = interval; timer.Interval = interval;
timer.AutoReset = loop; timer.AutoReset = loop;
@ -140,50 +116,40 @@ namespace Soul2.General.utils
return timer; return timer;
} }
public static Timer startOnce(double time, Action<Timer> callback) public static Timer startOnce(double time, Action<Timer> callback) {
{
return new Timer().startOnce(time, callback); return new Timer().startOnce(time, callback);
} }
public static Timer startOnce(double time, Action callback) public static Timer startOnce(double time, Action callback) {
{
return new Timer().startOnce(time, callback); return new Timer().startOnce(time, callback);
} }
// ----------------------------------------- // -----------------------------------------
public static Timer startLoop(double time, int times, Action<Timer, int> callback) public static Timer startLoop(double time, int times, Action<Timer, int> callback) {
{
return new Timer().startLoop(time, times, callback); return new Timer().startLoop(time, times, callback);
} }
public static Timer startLoop(double time, int times, Action<Timer> callback) public static Timer startLoop(double time, int times, Action<Timer> callback) {
{
return new Timer().startLoop(time, times, callback); return new Timer().startLoop(time, times, callback);
} }
public static Timer startLoop(double time, int times, Action<int> callback) public static Timer startLoop(double time, int times, Action<int> callback) {
{
return new Timer().startLoop(time, times, callback); return new Timer().startLoop(time, times, callback);
} }
public static Timer startLoop(double time, int times, Action callback) public static Timer startLoop(double time, int times, Action callback) {
{
return new Timer().startLoop(time, times, callback); return new Timer().startLoop(time, times, callback);
} }
// ----------------------------------------- // -----------------------------------------
public static Timer startKeep(double time, Action<Timer, int> callback, int times = default_loop_times) public static Timer startKeep(double time, Action<Timer, int> callback, int times = default_loop_times) {
{
return new Timer().startKeep(time, callback, times); return new Timer().startKeep(time, callback, times);
} }
public static Timer startKeep(double time, Action<int> callback, int times = default_loop_times) public static Timer startKeep(double time, Action<int> callback, int times = default_loop_times) {
{
return new Timer().startKeep(time, callback, times); return new Timer().startKeep(time, callback, times);
} }
public static Timer startKeep(double time, Action<Timer> callback, int times = default_loop_times) public static Timer startKeep(double time, Action<Timer> callback, int times = default_loop_times) {
{
return new Timer().startKeep(time, callback, times); return new Timer().startKeep(time, callback, times);
} }
public static Timer startKeep(double time, Action callback, int times = default_loop_times) public static Timer startKeep(double time, Action callback, int times = default_loop_times) {
{
return new Timer().startKeep(time, callback, times); return new Timer().startKeep(time, callback, times);
} }

Loading…
Cancel
Save