diff MetroWpf/MetroWpf.Framework/Extensions/ComparableExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Framework/Extensions/ComparableExtensions.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,69 @@
+using System;
+using System.Collections.Generic;
+
+namespace MetroWpf
+{
+    /// <summary>
+    /// Extension methods for all comparable objects eg. string, DateTime, numeric values ...
+    /// </summary>
+    public static class ComparableExtensions
+    {
+
+        /// <summary>
+        /// Determines whether the specified value is between the the defined minimum and maximum range (including those values).
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="value">The value.</param>
+        /// <param name="minValue">The minimum value.</param>
+        /// <param name="maxValue">The maximum value.</param>
+        /// <returns>
+        /// 	<c>true</c> if the specified value is between min and max; otherwise, <c>false</c>.
+        /// </returns>
+        /// <example>
+        /// var value = 5;
+        /// if(value.IsBetween(1, 10)) { 
+        ///     // ... 
+        /// }
+        /// </example>
+        public static bool IsBetween<T>(this T value, T minValue, T maxValue) where T : IComparable<T>
+        {
+            return IsBetween(value, minValue, maxValue, null);
+        }
+
+        /// <summary>
+        /// Determines whether the specified value is between the the defined minimum and maximum range (including those values).
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="value">The value.</param>
+        /// <param name="minValue">The minimum value.</param>
+        /// <param name="maxValue">The maximum value.</param>
+        /// <param name="comparer">An optional comparer to be used instead of the types default comparer.</param>
+        /// <returns>
+        /// 	<c>true</c> if the specified value is between min and max; otherwise, <c>false</c>.
+        /// </returns>
+        /// <example>
+        /// var value = 5;
+        /// if(value.IsBetween(1, 10)) {
+        /// // ...
+        /// }
+        /// </example>
+        public static bool IsBetween<T>(this T value, T minValue, T maxValue, IComparer<T> comparer) where T : IComparable<T>
+        {
+            comparer = comparer ?? Comparer<T>.Default;
+
+            var minMaxCompare = comparer.Compare(minValue, maxValue);
+            if (minMaxCompare < 0)
+            {
+                return ((comparer.Compare(value, minValue) >= 0) && (comparer.Compare(value, maxValue) <= 0));
+            }
+            else if (minMaxCompare == 0)
+            {
+                return (comparer.Compare(value, minValue) == 0);
+            }
+            else
+            {
+                return ((comparer.Compare(value, maxValue) >= 0) && (comparer.Compare(value, minValue) <= 0));
+            }
+        }
+    }
+}
\ No newline at end of file