using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Data.SqlTypes;
namespace EasyDevCore.Common
{
///
/// Common Helper class
///
public static class ConditionHelper
{
///
/// Tests the value in.
///
///
/// The input.
/// The arguments.
/// The comparer.
///
private static bool TestValueIn(T input, IEnumerable args, IComparer comparer = null)
{
if (input == null) return false;
foreach (var value in args)
{
if (value is IEnumerable list)
{
if(TestValueIn(input, list, comparer))
{
return true;
}
}
if (comparer is not null)
{
return comparer.Compare(input, value) == 0;
}
else if (input.Equals(value))
{
return true;
}
}
return false;
}
///
/// Check value in list.
///
/// The input.
/// The args.
///
public static bool In(this T input, params object[] args)
{
return TestValueIn(input, args);
}
///
/// Ins the specified comparer.
///
///
/// The input.
/// The comparer.
/// The arguments.
///
public static bool In(this T input, IComparer comparer, params object[] args)
{
return TestValueIn(input, args);
}
///
/// Betweens the specified value from.
///
///
/// The value.
/// The value from.
/// The value to.
/// The comparer.
///
public static bool Between(this T value, T valueFrom, T valueTo, IComparer comparer = null)
{
if (value == null || valueFrom == null || valueTo == null) return false;
if(comparer is not null)
{
return comparer.Compare(value, valueFrom) >= 0 && comparer.Compare(value, valueTo) <= 0;
}
else if ((valueFrom is IComparable) && (valueTo is IComparable))
{
return Comparer.Default.Compare(value, valueFrom) >= 0 && Comparer.Default.Compare(value, valueTo) <= 0;
}
else
{
return value.CompareValue(valueFrom) >= 0 && value.CompareValue(valueTo) <= 0;
}
}
///
/// Values if exception.
///
///
/// The function.
/// The value if exception.
///
public static T ValueIfException(this Func func, T valueIfException)
{
try
{
return func();
}
catch
{
return valueIfException;
}
}
///
/// Return the index of answer value in the question values.
/// (-1 means no correct)
///
///
/// The answer.
/// The quest values.
///
public static int ChoiceIndex(this T answer, params T[] questValues)
{
if (answer == null) return -1;
for (int i = 0; i < questValues.Length; i++)
{
if (answer.Equals(questValues[i])) return i;
}
return -1;
}
///
/// Switches the specified value.
///
///
/// The type of the 1.
/// The value.
/// The compare values.
/// The return values.
/// The failed value.
/// The comparer.
///
/// Compare values and the same length with return values
public static T Switch(this T1 value, T1[] compareValues, T[] returnValues, T failedValue, IComparer comparer = null)
{
if (compareValues.Length != returnValues.Length)
{
throw new ArgumentOutOfRangeException("Compare values must be the same length with return values");
}
for (int i = 0; i < compareValues.Length; i++)
{
T1 compareValue = compareValues[i];
if (value != null && compareValue != null)
{
if (comparer is null)
{
if (value.Equals(compareValues[i]))
return returnValues[i];
}
else
{
if(comparer.Compare(value, compareValue) == 0)
{
return returnValues[i];
}
}
}
}
return failedValue;
}
///
/// Switches the specified value.
///
///
/// The type of the 1.
/// The value.
/// The compare values.
/// The return values.
/// The comparer.
///
public static T Switch(this T1 value, T1[] compareValues, T[] returnValues, IComparer comparer = null)
{
return Switch(value, compareValues, returnValues, default, comparer);
}
///
/// Switches all the specified value.
///
///
/// The type of the 1.
/// The value.
/// The compare values.
/// The return values.
/// The comparer.
///
/// All the value where value equal compare value
///
/// Compare values and the same length with return values
public static T[] SwitchAll(this T1 value, T1[] compareValues, T[] returnValues, IComparer comparer = null)
{
if (compareValues.Length != returnValues.Length)
{
throw new ArgumentOutOfRangeException("Compare values must be the same length with return values");
}
List list = new List();
if (value != null)
{
for (int i = 0; i < compareValues.Length; i++)
{
T1 compareValue = compareValues[i];
if (value != null && compareValue != null)
{
if (comparer is null)
{
if (value.Equals(compareValues[i]))
list.Add(returnValues[i]);
}
else
{
if (comparer.Compare(value, compareValue) == 0)
{
list.Add(returnValues[i]);
}
}
}
}
}
return list.ToArray();
}
///
/// Mupliple If statement (if not any conditions meet, it will return last returnValues)
///
///
/// Multiple conditions.
/// The return values.
///
/// Conditions must be equals return values length - 1
public static T IIFs(this bool[] conditions, T[] returnValues)
{
if (conditions.Length != returnValues.Length)
{
throw new ArgumentOutOfRangeException("Conditions must be equals return values length - 1");
}
for (int i = 0; i < conditions.Length; i++)
{
if (conditions[i])
{
return returnValues[i];
}
}
return returnValues[returnValues.Length - 1];
}
///
/// Iifs all condition true.
///
///
/// The conditions.
/// The return values.
///
/// Conditions must be equals return values length - 1
public static T[] IIFAll(this bool[] conditions, T[] returnValues)
{
if (conditions.Length != returnValues.Length)
{
throw new ArgumentOutOfRangeException("Conditions must be equals return values length - 1");
}
var list = new List();
for (int i = 0; i < conditions.Length; i++)
{
if (conditions[i])
{
list.Add(returnValues[i]);
}
}
return list.ToArray();
}
///
/// Compares any value type (0: equal, 1: lower, 2: greater, 3: not equal)
///
/// The value 1.
/// The value 2.
///
public static byte CompareValue(this object value1, object value2)
{
if (value1.Equals(value2)) return 0;
TypeCode value1TypeCode = Type.GetTypeCode(value1.GetType());
TypeCode value2TypeCode = Type.GetTypeCode(value2.GetType());
if ((value1TypeCode == value2TypeCode) && ((value1TypeCode == TypeCode.Empty) || (value1TypeCode == TypeCode.Object)) && !value1.Equals(value2)) return 3;
if (((int)value1TypeCode >= 3) && ((int)value1TypeCode <= 15) && ((int)value2TypeCode >= 3) && ((int)value2TypeCode <= 15))
{
return (Convert.ToDecimal(value1) > Convert.ToDecimal(value2) ? (byte)2 : (byte)1);
}
if ((value1TypeCode == TypeCode.String) && (value2TypeCode == TypeCode.String))
{
return (string.Compare((string)value1, (string)value2) > 0 ? (byte)2 : (byte)1);
}
if ((value1TypeCode == TypeCode.DateTime) && (value2TypeCode == TypeCode.DateTime))
{
return (DateTime)value1 > (DateTime)value2 ? (byte)2 : (byte)1;
}
return 3;
}
}
///
///
///
public static class ComparerExtensions
{
///
///
///
///
private class FuncComparer : IComparer
{
private readonly Comparison comparison;
///
/// Initializes a new instance of the class.
///
/// The comparison.
public FuncComparer(Comparison comparison)
{
this.comparison = comparison;
}
///
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
///
/// The first object to compare.
/// The second object to compare.
///
/// A signed integer that indicates the relative values of and , as shown in the following table.
/// Value Meaning- Less than zero is less than .
- Zero equals .
- Greater than zero is greater than .
///
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
public int Compare(T x, T y)
#pragma warning restore CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
{
return comparison(x, y);
}
}
///
/// Functions the compare.
///
///
/// The comparison.
///
public static IComparer FuncCompare(Comparison comparison)
{
return new FuncComparer(comparison);
}
}
}