ConditionHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.IO;
  9. using System.Diagnostics.CodeAnalysis;
  10. using System.Data.SqlTypes;
  11. namespace EasyDevCore.Common
  12. {
  13. /// <summary>
  14. /// Common Helper class
  15. /// </summary>
  16. public static class ConditionHelper
  17. {
  18. /// <summary>
  19. /// Tests the value in.
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="input">The input.</param>
  23. /// <param name="args">The arguments.</param>
  24. /// <param name="comparer">The comparer.</param>
  25. /// <returns></returns>
  26. private static bool TestValueIn<T>(T input, IEnumerable args, IComparer comparer = null)
  27. {
  28. if (input == null) return false;
  29. foreach (var value in args)
  30. {
  31. if (value is IEnumerable list)
  32. {
  33. if(TestValueIn(input, list, comparer))
  34. {
  35. return true;
  36. }
  37. }
  38. if (comparer is not null)
  39. {
  40. return comparer.Compare(input, value) == 0;
  41. }
  42. else if (input.Equals(value))
  43. {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /// <summary>
  50. /// Check value in list.
  51. /// </summary>
  52. /// <param name="input">The input.</param>
  53. /// <param name="args">The args.</param>
  54. /// <returns></returns>
  55. public static bool In<T>(this T input, params object[] args)
  56. {
  57. return TestValueIn(input, args);
  58. }
  59. /// <summary>
  60. /// Ins the specified comparer.
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <param name="input">The input.</param>
  64. /// <param name="comparer">The comparer.</param>
  65. /// <param name="args">The arguments.</param>
  66. /// <returns></returns>
  67. public static bool In<T>(this T input, IComparer comparer, params object[] args)
  68. {
  69. return TestValueIn(input, args);
  70. }
  71. /// <summary>
  72. /// Betweens the specified value from.
  73. /// </summary>
  74. /// <typeparam name="T"></typeparam>
  75. /// <param name="value">The value.</param>
  76. /// <param name="valueFrom">The value from.</param>
  77. /// <param name="valueTo">The value to.</param>
  78. /// <param name="comparer">The comparer.</param>
  79. /// <returns></returns>
  80. public static bool Between<T>(this T value, T valueFrom, T valueTo, IComparer<T> comparer = null)
  81. {
  82. if (value == null || valueFrom == null || valueTo == null) return false;
  83. if(comparer is not null)
  84. {
  85. return comparer.Compare(value, valueFrom) >= 0 && comparer.Compare(value, valueTo) <= 0;
  86. }
  87. else if ((valueFrom is IComparable) && (valueTo is IComparable))
  88. {
  89. return Comparer<T>.Default.Compare(value, valueFrom) >= 0 && Comparer<T>.Default.Compare(value, valueTo) <= 0;
  90. }
  91. else
  92. {
  93. return value.CompareValue(valueFrom) >= 0 && value.CompareValue(valueTo) <= 0;
  94. }
  95. }
  96. /// <summary>
  97. /// Values if exception.
  98. /// </summary>
  99. /// <typeparam name="T"></typeparam>
  100. /// <param name="func">The function.</param>
  101. /// <param name="valueIfException">The value if exception.</param>
  102. /// <returns></returns>
  103. public static T ValueIfException<T>(this Func<T> func, T valueIfException)
  104. {
  105. try
  106. {
  107. return func();
  108. }
  109. catch
  110. {
  111. return valueIfException;
  112. }
  113. }
  114. /// <summary>
  115. /// Return the index of answer value in the question values.
  116. /// (-1 means no correct)
  117. /// </summary>
  118. /// <typeparam name="T"></typeparam>
  119. /// <param name="answer">The answer.</param>
  120. /// <param name="questValues">The quest values.</param>
  121. /// <returns></returns>
  122. public static int ChoiceIndex<T>(this T answer, params T[] questValues)
  123. {
  124. if (answer == null) return -1;
  125. for (int i = 0; i < questValues.Length; i++)
  126. {
  127. if (answer.Equals(questValues[i])) return i;
  128. }
  129. return -1;
  130. }
  131. /// <summary>
  132. /// Switches the specified value.
  133. /// </summary>
  134. /// <typeparam name="T"></typeparam>
  135. /// <typeparam name="T1">The type of the 1.</typeparam>
  136. /// <param name="value">The value.</param>
  137. /// <param name="compareValues">The compare values.</param>
  138. /// <param name="returnValues">The return values.</param>
  139. /// <param name="failedValue">The failed value.</param>
  140. /// <param name="comparer">The comparer.</param>
  141. /// <returns></returns>
  142. /// <exception cref="System.ArgumentOutOfRangeException">Compare values and the same length with return values</exception>
  143. public static T Switch<T, T1>(this T1 value, T1[] compareValues, T[] returnValues, T failedValue, IComparer<T1> comparer = null)
  144. {
  145. if (compareValues.Length != returnValues.Length)
  146. {
  147. throw new ArgumentOutOfRangeException("Compare values must be the same length with return values");
  148. }
  149. for (int i = 0; i < compareValues.Length; i++)
  150. {
  151. T1 compareValue = compareValues[i];
  152. if (value != null && compareValue != null)
  153. {
  154. if (comparer is null)
  155. {
  156. if (value.Equals(compareValues[i]))
  157. return returnValues[i];
  158. }
  159. else
  160. {
  161. if(comparer.Compare(value, compareValue) == 0)
  162. {
  163. return returnValues[i];
  164. }
  165. }
  166. }
  167. }
  168. return failedValue;
  169. }
  170. /// <summary>
  171. /// Switches the specified value.
  172. /// </summary>
  173. /// <typeparam name="T"></typeparam>
  174. /// <typeparam name="T1">The type of the 1.</typeparam>
  175. /// <param name="value">The value.</param>
  176. /// <param name="compareValues">The compare values.</param>
  177. /// <param name="returnValues">The return values.</param>
  178. /// <param name="comparer">The comparer.</param>
  179. /// <returns></returns>
  180. public static T Switch<T, T1>(this T1 value, T1[] compareValues, T[] returnValues, IComparer<T1> comparer = null)
  181. {
  182. return Switch(value, compareValues, returnValues, default, comparer);
  183. }
  184. /// <summary>
  185. /// Switches all the specified value.
  186. /// </summary>
  187. /// <typeparam name="T"></typeparam>
  188. /// <typeparam name="T1">The type of the 1.</typeparam>
  189. /// <param name="value">The value.</param>
  190. /// <param name="compareValues">The compare values.</param>
  191. /// <param name="returnValues">The return values.</param>
  192. /// <param name="comparer">The comparer.</param>
  193. /// <returns>
  194. /// All the value where value equal compare value
  195. /// </returns>
  196. /// <exception cref="System.ArgumentOutOfRangeException">Compare values and the same length with return values</exception>
  197. public static T[] SwitchAll<T, T1>(this T1 value, T1[] compareValues, T[] returnValues, IComparer<T1> comparer = null)
  198. {
  199. if (compareValues.Length != returnValues.Length)
  200. {
  201. throw new ArgumentOutOfRangeException("Compare values must be the same length with return values");
  202. }
  203. List<T> list = new List<T>();
  204. if (value != null)
  205. {
  206. for (int i = 0; i < compareValues.Length; i++)
  207. {
  208. T1 compareValue = compareValues[i];
  209. if (value != null && compareValue != null)
  210. {
  211. if (comparer is null)
  212. {
  213. if (value.Equals(compareValues[i]))
  214. list.Add(returnValues[i]);
  215. }
  216. else
  217. {
  218. if (comparer.Compare(value, compareValue) == 0)
  219. {
  220. list.Add(returnValues[i]);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. return list.ToArray();
  227. }
  228. /// <summary>
  229. /// Mupliple If statement (if not any conditions meet, it will return last returnValues)
  230. /// </summary>
  231. /// <typeparam name="T"></typeparam>
  232. /// <param name="conditions">Multiple conditions.</param>
  233. /// <param name="returnValues">The return values.</param>
  234. /// <returns></returns>
  235. /// <exception cref="System.ArgumentOutOfRangeException">Conditions must be equals return values length - 1</exception>
  236. public static T IIFs<T>(this bool[] conditions, T[] returnValues)
  237. {
  238. if (conditions.Length != returnValues.Length)
  239. {
  240. throw new ArgumentOutOfRangeException("Conditions must be equals return values length - 1");
  241. }
  242. for (int i = 0; i < conditions.Length; i++)
  243. {
  244. if (conditions[i])
  245. {
  246. return returnValues[i];
  247. }
  248. }
  249. return returnValues[returnValues.Length - 1];
  250. }
  251. /// <summary>
  252. /// Iifs all condition true.
  253. /// </summary>
  254. /// <typeparam name="T"></typeparam>
  255. /// <param name="conditions">The conditions.</param>
  256. /// <param name="returnValues">The return values.</param>
  257. /// <returns></returns>
  258. /// <exception cref="System.ArgumentOutOfRangeException">Conditions must be equals return values length - 1</exception>
  259. public static T[] IIFAll<T>(this bool[] conditions, T[] returnValues)
  260. {
  261. if (conditions.Length != returnValues.Length)
  262. {
  263. throw new ArgumentOutOfRangeException("Conditions must be equals return values length - 1");
  264. }
  265. var list = new List<T>();
  266. for (int i = 0; i < conditions.Length; i++)
  267. {
  268. if (conditions[i])
  269. {
  270. list.Add(returnValues[i]);
  271. }
  272. }
  273. return list.ToArray();
  274. }
  275. /// <summary>
  276. /// Compares any value type (0: equal, 1: lower, 2: greater, 3: not equal)
  277. /// </summary>
  278. /// <param name="value1">The value 1.</param>
  279. /// <param name="value2">The value 2.</param>
  280. /// <returns></returns>
  281. public static byte CompareValue(this object value1, object value2)
  282. {
  283. if (value1.Equals(value2)) return 0;
  284. TypeCode value1TypeCode = Type.GetTypeCode(value1.GetType());
  285. TypeCode value2TypeCode = Type.GetTypeCode(value2.GetType());
  286. if ((value1TypeCode == value2TypeCode) && ((value1TypeCode == TypeCode.Empty) || (value1TypeCode == TypeCode.Object)) && !value1.Equals(value2)) return 3;
  287. if (((int)value1TypeCode >= 3) && ((int)value1TypeCode <= 15) && ((int)value2TypeCode >= 3) && ((int)value2TypeCode <= 15))
  288. {
  289. return (Convert.ToDecimal(value1) > Convert.ToDecimal(value2) ? (byte)2 : (byte)1);
  290. }
  291. if ((value1TypeCode == TypeCode.String) && (value2TypeCode == TypeCode.String))
  292. {
  293. return (string.Compare((string)value1, (string)value2) > 0 ? (byte)2 : (byte)1);
  294. }
  295. if ((value1TypeCode == TypeCode.DateTime) && (value2TypeCode == TypeCode.DateTime))
  296. {
  297. return (DateTime)value1 > (DateTime)value2 ? (byte)2 : (byte)1;
  298. }
  299. return 3;
  300. }
  301. }
  302. /// <summary>
  303. ///
  304. /// </summary>
  305. public static class ComparerExtensions
  306. {
  307. /// <summary>
  308. ///
  309. /// </summary>
  310. /// <typeparam name="T"></typeparam>
  311. private class FuncComparer<T> : IComparer<T>
  312. {
  313. private readonly Comparison<T> comparison;
  314. /// <summary>
  315. /// Initializes a new instance of the <see cref="FuncComparer{T}"/> class.
  316. /// </summary>
  317. /// <param name="comparison">The comparison.</param>
  318. public FuncComparer(Comparison<T> comparison)
  319. {
  320. this.comparison = comparison;
  321. }
  322. /// <summary>
  323. /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
  324. /// </summary>
  325. /// <param name="x">The first object to compare.</param>
  326. /// <param name="y">The second object to compare.</param>
  327. /// <returns>
  328. /// A signed integer that indicates the relative values of <paramref name="x" /> and <paramref name="y" />, as shown in the following table.
  329. /// <list type="table"><listheader><term> Value</term><description> Meaning</description></listheader><item><term> Less than zero</term><description><paramref name="x" /> is less than <paramref name="y" />.</description></item><item><term> Zero</term><description><paramref name="x" /> equals <paramref name="y" />.</description></item><item><term> Greater than zero</term><description><paramref name="x" /> is greater than <paramref name="y" />.</description></item></list>
  330. /// </returns>
  331. #pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
  332. public int Compare(T x, T y)
  333. #pragma warning restore CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
  334. {
  335. return comparison(x, y);
  336. }
  337. }
  338. /// <summary>
  339. /// Functions the compare.
  340. /// </summary>
  341. /// <typeparam name="T"></typeparam>
  342. /// <param name="comparison">The comparison.</param>
  343. /// <returns></returns>
  344. public static IComparer<T> FuncCompare<T>(Comparison<T> comparison)
  345. {
  346. return new FuncComparer<T>(comparison);
  347. }
  348. }
  349. }