diff --git a/Utilities/Comparers/ReverseComparer.cs b/Utilities/Comparers/ReverseComparer.cs new file mode 100644 index 0000000..54132a1 --- /dev/null +++ b/Utilities/Comparers/ReverseComparer.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Utilities +{ + public class ReverseComparer : IComparer + { + private IComparer m_comparer; + + public ReverseComparer(IComparer comparer) + { + m_comparer = comparer; + } + + public int Compare(T x, T y) + { + return m_comparer.Compare(y, x); + } + } +} diff --git a/Utilities/Conversion/Conversion.SimpleTypes.cs b/Utilities/Conversion/Conversion.SimpleTypes.cs index 72fec08..042c16e 100644 --- a/Utilities/Conversion/Conversion.SimpleTypes.cs +++ b/Utilities/Conversion/Conversion.SimpleTypes.cs @@ -1,14 +1,29 @@ using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Web; namespace Utilities { public partial class Conversion { + public static short ToInt16(object obj) + { + return ToInt16(obj, 0); + } + + public static short ToInt16(object obj, short defaultValue) + { + short result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToInt16(obj); + } + catch + { } + } + return result; + } + public static int ToInt32(object obj) { return ToInt32(obj, 0); @@ -48,5 +63,200 @@ namespace Utilities } return result; } + + public static ushort ToUInt16(object obj) + { + return ToUInt16(obj, 0); + } + + public static ushort ToUInt16(object obj, ushort defaultValue) + { + ushort result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToUInt16(obj); + } + catch + { } + } + return result; + } + + public static uint ToUInt32(object obj) + { + return ToUInt32(obj, 0); + } + + public static uint ToUInt32(object obj, uint defaultValue) + { + uint result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToUInt32(obj); + } + catch + { } + } + return result; + } + + public static ulong ToUInt64(object obj) + { + return ToUInt64(obj, 0); + } + + public static ulong ToUInt64(object obj, ulong defaultValue) + { + ulong result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToUInt64(obj); + } + catch + { } + } + return result; + } + + public static float ToFloat(object obj) + { + return ToFloat(obj, 0); + } + + public static float ToFloat(object obj, float defaultValue) + { + float result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToSingle(obj); + } + catch + {} + } + return result; + } + + public static double ToDouble(object obj) + { + return ToDouble(obj, 0); + } + + public static double ToDouble(object obj, double defaultValue) + { + double result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToDouble(obj); + } + catch + { } + } + return result; + } + + public static decimal ToDecimal(object obj) + { + return ToDecimal(obj, 0); + } + + public static decimal ToDecimal(object obj, decimal defaultValue) + { + decimal result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToDecimal(obj); + } + catch + { } + } + return result; + } + + public static bool ToBoolean(object obj) + { + return ToBoolean(obj, false); + } + + public static bool ToBoolean(object obj, bool defaultValue) + { + bool result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToBoolean(obj); + } + catch + { } + } + return result; + } + + public static string ToString(object obj) + { + string result = String.Empty; + if (obj != null) + { + try + { + result = Convert.ToString(obj); + } + catch + {} + } + return result; + } + + public static char ToChar(object obj) + { + return ToChar(obj, new char()); + } + + public static char ToChar(object obj, char defaultValue) + { + char result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToChar(obj); + } + catch + { } + } + return result; + } + + public static DateTime ToDateTime(object obj) + { + return ToDateTime(obj, DateTime.MinValue); + } + + public static DateTime ToDateTime(object obj, DateTime defaultValue) + { + DateTime result = defaultValue; + if (obj != null) + { + try + { + result = Convert.ToDateTime(obj); + } + catch + { } + } + return result; + } } } diff --git a/Utilities/Generics/KeyValuePairList.cs b/Utilities/Generics/KeyValuePairList.cs index b91be25..093c6e1 100644 --- a/Utilities/Generics/KeyValuePairList.cs +++ b/Utilities/Generics/KeyValuePairList.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; namespace Utilities { @@ -66,5 +67,35 @@ namespace Utilities return result; } } + + new public void Sort() + { + this.Sort(Comparer.Default); + } + + public void Sort(ListSortDirection sortDirection) + { + Sort(Comparer.Default, sortDirection); + } + + public void Sort(IComparer comparer, ListSortDirection sortDirection) + { + if (sortDirection == ListSortDirection.Ascending) + { + Sort(comparer); + } + else + { + Sort(new ReverseComparer(comparer)); + } + } + + public void Sort(IComparer comparer) + { + this.Sort(delegate(KeyValuePair a, KeyValuePair b) + { + return comparer.Compare(a.Key, b.Key); + }); + } } } diff --git a/Utilities/Generics/SortedList.cs b/Utilities/Generics/SortedList.cs index ffc2160..b8d5f2d 100644 --- a/Utilities/Generics/SortedList.cs +++ b/Utilities/Generics/SortedList.cs @@ -35,21 +35,7 @@ namespace Utilities /// public int IndexOf(T item) { - int insertIndex = FindIndexForSortedInsert(m_innerList, m_comparer, item); - if (insertIndex == m_innerList.Count) - { - return -1; - } - if (m_comparer.Compare(item, m_innerList[insertIndex]) == 0) - { - int index = insertIndex; - while (index > 0 && m_comparer.Compare(item, m_innerList[index - 1]) == 0) - { - index--; - } - return index; - } - return -1; + return FirstIndexOf(m_innerList, m_comparer, item); } public bool Remove(T item) @@ -117,6 +103,25 @@ namespace Utilities } } + public static int FirstIndexOf(List list, Comparer comparer, T item) + { + int insertIndex = FindIndexForSortedInsert(list, comparer, item); + if (insertIndex == list.Count) + { + return -1; + } + if (comparer.Compare(item, list[insertIndex]) == 0) + { + int index = insertIndex; + while (index > 0 && comparer.Compare(item, list[index - 1]) == 0) + { + index--; + } + return index; + } + return -1; + } + public static int FindIndexForSortedInsert(List list, Comparer comparer, T item) { if (list.Count == 0) diff --git a/Utilities/Properties/AssemblyInfo.cs b/Utilities/Properties/AssemblyInfo.cs index 56878dc..a0d0338 100644 --- a/Utilities/Properties/AssemblyInfo.cs +++ b/Utilities/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Tal Aloni")] [assembly: AssemblyProduct("Utilities")] -[assembly: AssemblyCopyright("Copyright © Tal Aloni 2014")] +[assembly: AssemblyCopyright("Copyright © Tal Aloni 2005-2016")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Utilities/Utilities.csproj b/Utilities/Utilities.csproj index cbd997a..90d8236 100644 --- a/Utilities/Utilities.csproj +++ b/Utilities/Utilities.csproj @@ -38,6 +38,7 @@ +