From db36836eff7cce28d14a1a6400fb4286c74a39a9 Mon Sep 17 00:00:00 2001 From: eeichinger Date: Tue, 28 Jul 2009 15:21:16 +0000 Subject: [PATCH] fixed compiler error --- src/Spring/Spring.Core/Util/NumberUtils.cs | 923 ++++++++++-------- .../Util/NumberUtilsTests.cs | 20 + 2 files changed, 540 insertions(+), 403 deletions(-) diff --git a/src/Spring/Spring.Core/Util/NumberUtils.cs b/src/Spring/Spring.Core/Util/NumberUtils.cs index 3669030a..c52a0870 100644 --- a/src/Spring/Spring.Core/Util/NumberUtils.cs +++ b/src/Spring/Spring.Core/Util/NumberUtils.cs @@ -1,5 +1,5 @@ -#region License - +#region License + /* * Copyright © 2002-2005 the original author or authors. * @@ -14,191 +14,222 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ - -#endregion - -#region Imports - -using System; -using System.ComponentModel; -using Spring.Core.TypeConversion; - -#endregion - -namespace Spring.Util -{ - /// - /// Various utility methods relating to numbers. - /// - /// - ///

- /// Mainly for internal use within the framework. - ///

- ///
- /// Aleksandar Seovic - public sealed class NumberUtils - { - /// - /// Determines whether the supplied is an integer. - /// - /// The object to check. - /// - /// if the supplied is an integer. - /// - public static bool IsInteger(object number) - { - return (number is Int32 || number is Int16 || number is Int64 || number is UInt32 - || number is UInt16 || number is UInt64 || number is Byte || number is SByte); - } - - /// - /// Determines whether the supplied is a decimal number. - /// - /// The object to check. - /// - /// if the supplied is a decimal number. - /// - public static bool IsDecimal(object number) - { - - return (number is Single || number is Double || number is Decimal); - } - - /// - /// Determines whether the supplied is of numeric type. - /// - /// The object to check. - /// - /// true if the specified object is of numeric type; otherwise, false. - /// - public static bool IsNumber(object number) - { - return (IsInteger(number) || IsDecimal(number)); - } - - /// - /// Determines whether the supplied can be converted to an integer. - /// - /// The object to check. - /// - /// if the supplied can be converted to an integer. - /// - public static bool CanConvertToInteger(object number) - { - TypeConverter converter = TypeDescriptor.GetConverter(number); - return (converter.CanConvertTo(typeof(Int32)) - || converter.CanConvertTo(typeof(Int16)) - || converter.CanConvertTo(typeof(Int64)) - || converter.CanConvertTo(typeof(UInt16)) - || converter.CanConvertTo(typeof(UInt64)) - || converter.CanConvertTo(typeof(Byte)) - || converter.CanConvertTo(typeof(SByte)) - ); - } - - /// - /// Determines whether the supplied can be converted to an integer. - /// - /// The object to check. - /// - /// if the supplied can be converted to an integer. - /// - public static bool CanConvertToDecimal(object number) - { - TypeConverter converter = TypeDescriptor.GetConverter(number); - return (converter.CanConvertTo(typeof(Single)) - || converter.CanConvertTo(typeof(Double)) - || converter.CanConvertTo(typeof(Decimal)) - ); - } - - /// - /// Determines whether the supplied can be converted to a number. - /// - /// The object to check. - /// - /// true if the specified object is decimal number; otherwise, false. - /// - public static bool CanConvertToNumber(object number) - { - return (CanConvertToInteger(number) || CanConvertToDecimal(number)); - } - - /// - /// Is the supplied equal to zero (0)? - /// - /// The number to check. - /// - /// id the supplied is equal to zero (0). - /// - public static bool IsZero(object number) - { - if (number is Int32) return ((Int32) number) == 0; - else if (number is Int16) return ((Int16) number) == 0; - else if (number is Int64) return ((Int64) number) == 0; - else if (number is UInt16) return ((Int32) number) == 0; - else if (number is UInt32) return ((Int64) number) == 0; - else if (number is UInt64) return (Convert.ToDecimal(number) == 0); - else if (number is Byte) return ((Int16) number) == 0; - else if (number is SByte) return ((Int16) number) == 0; - else if (number is Single) return ((Single) number) == 0f; - else if (number is Double) return ((Double) number) == 0d; - else if (number is Decimal) return ((Decimal) number) == 0m; - return false; - } - - /// - /// Negates the supplied . - /// - /// The number to negate. - /// The supplied negated. - /// - /// If the supplied is not a supported numeric type. - /// - public static object Negate(object number) - { - if (number is Int32) return -((Int32) number); - else if (number is Int16) return -((Int16) number); - else if (number is Int64) return -((Int64) number); - else if (number is UInt16) return -((Int32) number); - else if (number is UInt32) return -((Int64) number); - else if (number is UInt64) return -(Convert.ToDecimal(number)); - else if (number is Byte) return -((Int16) number); - else if (number is SByte) return -((Int16) number); - else if (number is Single) return -((Single) number); - else if (number is Double) return -((Double) number); - else if (number is Decimal) return -((Decimal) number); - else - { - throw new ArgumentException(string.Format("'{0}' is not one of the supported numeric types.", number)); - } - } - - /// - /// Returns the bitwise not (~) of the supplied . - /// - /// The number. - /// The value of ~. - /// - /// If the supplied is not a supported numeric type. - /// - public static object BitwiseNot(object number) - { - if (number is bool) return !((bool) number); - else if (number is Int32) return ~((Int32) number); - else if (number is Int16) return ~((Int16) number); - else if (number is Int64) return ~((Int64) number); - else if (number is UInt16) return ~((UInt16) number); - else if (number is UInt32) return ~((UInt32) number); - else if (number is UInt64) return ~((UInt64)number); - else if (number is Byte) return ~((Byte) number); - else if (number is SByte) return ~((SByte) number); - else - { - throw new ArgumentException(string.Format("'{0}' is not one of the supported integer types.", number)); - } - } + */ + +#endregion + +#region Imports + +using System; +using System.ComponentModel; +using Spring.Core.TypeConversion; + +#endregion + +namespace Spring.Util +{ + /// + /// Various utility methods relating to numbers. + /// + /// + ///

+ /// Mainly for internal use within the framework. + ///

+ ///
+ /// Aleksandar Seovic + public sealed class NumberUtils + { + /// + /// Determines whether the supplied is an integer. + /// + /// The object to check. + /// + /// if the supplied is an integer. + /// + public static bool IsInteger(object number) + { + return (number is Int32 || number is Int16 || number is Int64 || number is UInt32 + || number is UInt16 || number is UInt64 || number is Byte || number is SByte); + } + + /// + /// Determines whether the supplied is a decimal number. + /// + /// The object to check. + /// + /// if the supplied is a decimal number. + /// + public static bool IsDecimal(object number) + { + + return (number is Single || number is Double || number is Decimal); + } + + /// + /// Determines whether the supplied is of numeric type. + /// + /// The object to check. + /// + /// true if the specified object is of numeric type; otherwise, false. + /// + public static bool IsNumber(object number) + { + return (IsInteger(number) || IsDecimal(number)); + } + + /// + /// Determines whether the supplied can be converted to an integer. + /// + /// The object to check. + /// + /// if the supplied can be converted to an integer. + /// + public static bool CanConvertToInteger(object number) + { + TypeConverter converter = TypeDescriptor.GetConverter(number); + return (converter.CanConvertTo(typeof(Int32)) + || converter.CanConvertTo(typeof(Int16)) + || converter.CanConvertTo(typeof(Int64)) + || converter.CanConvertTo(typeof(UInt16)) + || converter.CanConvertTo(typeof(UInt64)) + || converter.CanConvertTo(typeof(Byte)) + || converter.CanConvertTo(typeof(SByte)) + ); + } + + /// + /// Determines whether the supplied can be converted to an integer. + /// + /// The object to check. + /// + /// if the supplied can be converted to an integer. + /// + public static bool CanConvertToDecimal(object number) + { + TypeConverter converter = TypeDescriptor.GetConverter(number); + return (converter.CanConvertTo(typeof(Single)) + || converter.CanConvertTo(typeof(Double)) + || converter.CanConvertTo(typeof(Decimal)) + ); + } + + /// + /// Determines whether the supplied can be converted to a number. + /// + /// The object to check. + /// + /// true if the specified object is decimal number; otherwise, false. + /// + public static bool CanConvertToNumber(object number) + { + return (CanConvertToInteger(number) || CanConvertToDecimal(number)); + } + + /// + /// Is the supplied equal to zero (0)? + /// + /// The number to check. + /// + /// id the supplied is equal to zero (0). + /// + public static bool IsZero(object number) + { + if (number is Int32) + return ((Int32)number) == 0; + else if (number is Int16) + return ((Int16)number) == 0; + else if (number is Int64) + return ((Int64)number) == 0; + else if (number is UInt16) + return ((Int32)number) == 0; + else if (number is UInt32) + return ((Int64)number) == 0; + else if (number is UInt64) + return (Convert.ToDecimal(number) == 0); + else if (number is Byte) + return ((Int16)number) == 0; + else if (number is SByte) + return ((Int16)number) == 0; + else if (number is Single) + return ((Single)number) == 0f; + else if (number is Double) + return ((Double)number) == 0d; + else if (number is Decimal) + return ((Decimal)number) == 0m; + return false; + } + + /// + /// Negates the supplied . + /// + /// The number to negate. + /// The supplied negated. + /// + /// If the supplied is not a supported numeric type. + /// + public static object Negate(object number) + { + if (number is Int32) + return -((Int32)number); + else if (number is Int16) + return -((Int16)number); + else if (number is Int64) + return -((Int64)number); + else if (number is UInt16) + return -((Int32)number); + else if (number is UInt32) + return -((Int64)number); + else if (number is UInt64) + return -(Convert.ToDecimal(number)); + else if (number is Byte) + return -((Int16)number); + else if (number is SByte) + return -((Int16)number); + else if (number is Single) + return -((Single)number); + else if (number is Double) + return -((Double)number); + else if (number is Decimal) + return -((Decimal)number); + else + { + throw new ArgumentException(string.Format("'{0}' is not one of the supported numeric types.", number)); + } + } + + /// + /// Returns the bitwise not (~) of the supplied . + /// + /// The number. + /// The value of ~. + /// + /// If the supplied is not a supported numeric type. + /// + public static object BitwiseNot(object number) + { + if (number is bool) + return !((bool)number); + else if (number is Int32) + return ~((Int32)number); + else if (number is Int16) + return ~((Int16)number); + else if (number is Int64) + return ~((Int64)number); + else if (number is UInt16) + return ~((UInt16)number); + else if (number is UInt32) + return ~((UInt32)number); + else if (number is UInt64) + return ~((UInt64)number); + else if (number is Byte) + return ~((Byte)number); + else if (number is SByte) + return ~((SByte)number); + else + { + throw new ArgumentException(string.Format("'{0}' is not one of the supported integer types.", number)); + } + } /// /// Bitwise ANDs (&) the specified integral values. @@ -208,25 +239,34 @@ namespace Spring.Util /// /// If one of the supplied arguments is not a supported integral types. /// - public static object BitwiseAnd(object m, object n) - { + public static object BitwiseAnd(object m, object n) + { CoerceTypes(ref m, ref n); - if (n is bool) return (bool)m & (bool)n; - else if (n is Int32) return (Int32)m & (Int32)n; - else if (n is Int16) return (Int16)m & (Int16)n; - else if (n is Int64) return (Int64)m & (Int64)n; - else if (n is UInt16) return (UInt16)m & (UInt16)n; - else if (n is UInt32) return (UInt32)m & (UInt32)n; - else if (n is UInt64) return (UInt64)m & (UInt64)n; - else if (n is Byte) return (Byte)m & (Byte)n; - else if (n is SByte) return (SByte)m & (SByte)n; - else - { + if (n is bool) + return (bool)m & (bool)n; + else if (n is Int32) + return (Int32)m & (Int32)n; + else if (n is Int16) + return (Int16)m & (Int16)n; + else if (n is Int64) + return (Int64)m & (Int64)n; + else if (n is UInt16) + return (UInt16)m & (UInt16)n; + else if (n is UInt32) + return (UInt32)m & (UInt32)n; + else if (n is UInt64) + return (UInt64)m & (UInt64)n; + else if (n is Byte) + return (Byte)m & (Byte)n; + else if (n is SByte) + return (SByte)m & (SByte)n; + else + { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported integral types.", m, n)); - } - } - + } + } + /// /// Bitwise ORs (|) the specified integral values. /// @@ -235,25 +275,38 @@ namespace Spring.Util /// /// If one of the supplied arguments is not a supported integral types. /// - public static object BitwiseOr(object m, object n) - { + public static object BitwiseOr(object m, object n) + { CoerceTypes(ref m, ref n); - if (n is bool) return (bool)m | (bool)n; - else if (n is Int32) return (Int32)m | (Int32)n; - else if (n is Int16) return (UInt16)m | (UInt16)n; - else if (n is Int64) return (Int64)m | (Int64)n; - else if (n is UInt16) return (UInt16)m | (UInt16)n; - else if (n is UInt32) return (UInt32)m | (UInt32)n; - else if (n is UInt64) return (UInt64)m | (UInt64)n; - else if (n is Byte) return (Byte)m | (Byte)n; - else if (n is SByte) return (Byte)m | (Byte)n; - else - { + if (n is bool) + return (bool)m | (bool)n; + else if (n is Int32) + return (Int32)m | (Int32)n; +#if NET_2_0 + else if (n is Int16) + return (Int16)m | (Int16)n; +#endif + else if (n is Int64) + return (Int64)m | (Int64)n; + else if (n is UInt16) + return (UInt16)m | (UInt16)n; + else if (n is UInt32) + return (UInt32)m | (UInt32)n; + else if (n is UInt64) + return (UInt64)m | (UInt64)n; + else if (n is Byte) + return (Byte)m | (Byte)n; +#if NET_2_0 + else if (n is SByte) + return ((SByte) m | (SByte) n); +#endif + else + { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported integral types.", m, n)); - } - } - + } + } + /// /// Bitwise XORs (^) the specified integral values. /// @@ -262,203 +315,267 @@ namespace Spring.Util /// /// If one of the supplied arguments is not a supported integral types. /// - public static object BitwiseXor(object m, object n) - { + public static object BitwiseXor(object m, object n) + { CoerceTypes(ref m, ref n); - if (n is bool) return (bool)m ^ (bool)n; - else if (n is Int32) return (Int32)m ^ (Int32)n; - else if (n is Int16) return (Int16)m ^ (Int16)n; - else if (n is Int64) return (Int64)m ^ (Int64)n; - else if (n is UInt16) return (UInt16)m ^ (UInt16)n; - else if (n is UInt32) return (UInt32)m ^ (UInt32)n; - else if (n is UInt64) return (UInt64)m ^ (UInt64)n; - else if (n is Byte) return (Byte)m ^ (Byte)n; - else if (n is SByte) return (SByte)m ^ (SByte)n; - else - { + if (n is bool) + return (bool)m ^ (bool)n; + else if (n is Int32) + return (Int32)m ^ (Int32)n; + else if (n is Int16) + return (Int16)m ^ (Int16)n; + else if (n is Int64) + return (Int64)m ^ (Int64)n; + else if (n is UInt16) + return (UInt16)m ^ (UInt16)n; + else if (n is UInt32) + return (UInt32)m ^ (UInt32)n; + else if (n is UInt64) + return (UInt64)m ^ (UInt64)n; + else if (n is Byte) + return (Byte)m ^ (Byte)n; + else if (n is SByte) + return (SByte)m ^ (SByte)n; + else + { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported integral types.", m, n)); - } - } - - /// - /// Adds the specified numbers. - /// - /// The first number. - /// The second number. - public static object Add(object m, object n) - { - CoerceTypes(ref m, ref n); - - if (n is Int32) return (Int32) m + (Int32) n; - else if (n is Int16) return (Int16) m + (Int16) n; - else if (n is Int64) return (Int64) m + (Int64) n; - else if (n is UInt16) return (UInt16) m + (UInt16) n; - else if (n is UInt32) return (UInt32) m + (UInt32) n; - else if (n is UInt64) return (UInt64) m + (UInt64) n; - else if (n is Byte) return (Byte) m + (Byte) n; - else if (n is SByte) return (SByte) m + (SByte) n; - else if (n is Single) return (Single) m + (Single) n; - else if (n is Double) return (Double) m + (Double) n; - else if (n is Decimal) return (Decimal) m + (Decimal) n; - else - { - throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); - } - } - - /// - /// Subtracts the specified numbers. - /// - /// The first number. - /// The second number. - public static object Subtract(object m, object n) - { - CoerceTypes(ref m, ref n); - - if (n is Int32) return (Int32) m - (Int32) n; - else if (n is Int16) return (Int16) m - (Int16) n; - else if (n is Int64) return (Int64) m - (Int64) n; - else if (n is UInt16) return (UInt16) m - (UInt16) n; - else if (n is UInt32) return (UInt32) m - (UInt32) n; - else if (n is UInt64) return (UInt64) m - (UInt64) n; - else if (n is Byte) return (Byte) m - (Byte) n; - else if (n is SByte) return (SByte) m - (SByte) n; - else if (n is Single) return (Single) m - (Single) n; - else if (n is Double) return (Double) m - (Double) n; - else if (n is Decimal) return (Decimal) m - (Decimal) n; - else - { - throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); - } - } - - /// - /// Multiplies the specified numbers. - /// - /// The first number. - /// The second number. - public static object Multiply(object m, object n) - { - CoerceTypes(ref m, ref n); - - if (n is Int32) return (Int32) m*(Int32) n; - else if (n is Int16) return (Int16) m*(Int16) n; - else if (n is Int64) return (Int64) m*(Int64) n; - else if (n is UInt16) return (UInt16) m*(UInt16) n; - else if (n is UInt32) return (UInt32) m*(UInt32) n; - else if (n is UInt64) return (UInt64) m*(UInt64) n; - else if (n is Byte) return (Byte) m*(Byte) n; - else if (n is SByte) return (SByte) m*(SByte) n; - else if (n is Single) return (Single) m*(Single) n; - else if (n is Double) return (Double) m*(Double) n; - else if (n is Decimal) return (Decimal) m*(Decimal) n; + } + } + + /// + /// Adds the specified numbers. + /// + /// The first number. + /// The second number. + public static object Add(object m, object n) + { + CoerceTypes(ref m, ref n); + + if (n is Int32) + return (Int32)m + (Int32)n; + else if (n is Int16) + return (Int16)m + (Int16)n; + else if (n is Int64) + return (Int64)m + (Int64)n; + else if (n is UInt16) + return (UInt16)m + (UInt16)n; + else if (n is UInt32) + return (UInt32)m + (UInt32)n; + else if (n is UInt64) + return (UInt64)m + (UInt64)n; + else if (n is Byte) + return (Byte)m + (Byte)n; + else if (n is SByte) + return (SByte)m + (SByte)n; + else if (n is Single) + return (Single)m + (Single)n; + else if (n is Double) + return (Double)m + (Double)n; + else if (n is Decimal) + return (Decimal)m + (Decimal)n; else { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); } - } - - /// - /// Divides the specified numbers. - /// - /// The first number. - /// The second number. - public static object Divide(object m, object n) - { - CoerceTypes(ref m, ref n); - - if (n is Int32) return (Int32) m/(Int32) n; - else if (n is Int16) return (Int16) m/(Int16) n; - else if (n is Int64) return (Int64) m/(Int64) n; - else if (n is UInt16) return (UInt16) m/(UInt16) n; - else if (n is UInt32) return (UInt32) m/(UInt32) n; - else if (n is UInt64) return (UInt64) m/(UInt64) n; - else if (n is Byte) return (Byte) m/(Byte) n; - else if (n is SByte) return (SByte) m/(SByte) n; - else if (n is Single) return (Single) m/(Single) n; - else if (n is Double) return (Double) m/(Double) n; - else if (n is Decimal) return (Decimal) m/(Decimal) n; + } + + /// + /// Subtracts the specified numbers. + /// + /// The first number. + /// The second number. + public static object Subtract(object m, object n) + { + CoerceTypes(ref m, ref n); + + if (n is Int32) + return (Int32)m - (Int32)n; + else if (n is Int16) + return (Int16)m - (Int16)n; + else if (n is Int64) + return (Int64)m - (Int64)n; + else if (n is UInt16) + return (UInt16)m - (UInt16)n; + else if (n is UInt32) + return (UInt32)m - (UInt32)n; + else if (n is UInt64) + return (UInt64)m - (UInt64)n; + else if (n is Byte) + return (Byte)m - (Byte)n; + else if (n is SByte) + return (SByte)m - (SByte)n; + else if (n is Single) + return (Single)m - (Single)n; + else if (n is Double) + return (Double)m - (Double)n; + else if (n is Decimal) + return (Decimal)m - (Decimal)n; else { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); } - } - - /// - /// Calculates remainder for the specified numbers. - /// - /// The first number (dividend). - /// The second number (divisor). - public static object Modulus(object m, object n) - { - CoerceTypes(ref m, ref n); - - if (n is Int32) return (Int32) m%(Int32) n; - else if (n is Int16) return (Int16) m%(Int16) n; - else if (n is Int64) return (Int64) m%(Int64) n; - else if (n is UInt16) return (UInt16) m%(UInt16) n; - else if (n is UInt32) return (UInt32) m%(UInt32) n; - else if (n is UInt64) return (UInt64) m%(UInt64) n; - else if (n is Byte) return (Byte) m%(Byte) n; - else if (n is SByte) return (SByte) m%(SByte) n; - else if (n is Single) return (Single) m%(Single) n; - else if (n is Double) return (Double) m%(Double) n; - else if (n is Decimal) return (Decimal) m%(Decimal) n; + } + + /// + /// Multiplies the specified numbers. + /// + /// The first number. + /// The second number. + public static object Multiply(object m, object n) + { + CoerceTypes(ref m, ref n); + + if (n is Int32) + return (Int32)m * (Int32)n; + else if (n is Int16) + return (Int16)m * (Int16)n; + else if (n is Int64) + return (Int64)m * (Int64)n; + else if (n is UInt16) + return (UInt16)m * (UInt16)n; + else if (n is UInt32) + return (UInt32)m * (UInt32)n; + else if (n is UInt64) + return (UInt64)m * (UInt64)n; + else if (n is Byte) + return (Byte)m * (Byte)n; + else if (n is SByte) + return (SByte)m * (SByte)n; + else if (n is Single) + return (Single)m * (Single)n; + else if (n is Double) + return (Double)m * (Double)n; + else if (n is Decimal) + return (Decimal)m * (Decimal)n; else { throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); } - } - - /// - /// Raises first number to the power of the second one. - /// - /// The first number. - /// The second number. - public static object Power(object m, object n) - { - return Math.Pow(Convert.ToDouble(m), Convert.ToDouble(n)); - } - - /// - /// Coerces the types so they can be compared. - /// - /// The right. - /// The left. - public static void CoerceTypes(ref object m, ref object n) - { - TypeCode leftTypeCode = Convert.GetTypeCode(m); - TypeCode rightTypeCode = Convert.GetTypeCode(n); - - if (leftTypeCode > rightTypeCode) - { - n = Convert.ChangeType(n, leftTypeCode); - } - else - { - m = Convert.ChangeType(m, rightTypeCode); - } - } - - #region Constructor (s) / Destructor - - // CLOVER:OFF - - /// - /// Creates a new instance of the class. - /// - /// - ///

- /// This is a utility class, and as such exposes no public constructors. - ///

- ///
- private NumberUtils() - { - } - - // CLOVER:ON - - #endregion - } + } + + /// + /// Divides the specified numbers. + /// + /// The first number. + /// The second number. + public static object Divide(object m, object n) + { + CoerceTypes(ref m, ref n); + + if (n is Int32) + return (Int32)m / (Int32)n; + else if (n is Int16) + return (Int16)m / (Int16)n; + else if (n is Int64) + return (Int64)m / (Int64)n; + else if (n is UInt16) + return (UInt16)m / (UInt16)n; + else if (n is UInt32) + return (UInt32)m / (UInt32)n; + else if (n is UInt64) + return (UInt64)m / (UInt64)n; + else if (n is Byte) + return (Byte)m / (Byte)n; + else if (n is SByte) + return (SByte)m / (SByte)n; + else if (n is Single) + return (Single)m / (Single)n; + else if (n is Double) + return (Double)m / (Double)n; + else if (n is Decimal) + return (Decimal)m / (Decimal)n; + else + { + throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); + } + } + + /// + /// Calculates remainder for the specified numbers. + /// + /// The first number (dividend). + /// The second number (divisor). + public static object Modulus(object m, object n) + { + CoerceTypes(ref m, ref n); + + if (n is Int32) + return (Int32)m % (Int32)n; + else if (n is Int16) + return (Int16)m % (Int16)n; + else if (n is Int64) + return (Int64)m % (Int64)n; + else if (n is UInt16) + return (UInt16)m % (UInt16)n; + else if (n is UInt32) + return (UInt32)m % (UInt32)n; + else if (n is UInt64) + return (UInt64)m % (UInt64)n; + else if (n is Byte) + return (Byte)m % (Byte)n; + else if (n is SByte) + return (SByte)m % (SByte)n; + else if (n is Single) + return (Single)m % (Single)n; + else if (n is Double) + return (Double)m % (Double)n; + else if (n is Decimal) + return (Decimal)m % (Decimal)n; + else + { + throw new ArgumentException(string.Format("'{0}' and/or '{1}' are not one of the supported numeric types.", m, n)); + } + } + + /// + /// Raises first number to the power of the second one. + /// + /// The first number. + /// The second number. + public static object Power(object m, object n) + { + return Math.Pow(Convert.ToDouble(m), Convert.ToDouble(n)); + } + + /// + /// Coerces the types so they can be compared. + /// + /// The right. + /// The left. + public static void CoerceTypes(ref object m, ref object n) + { + TypeCode leftTypeCode = Convert.GetTypeCode(m); + TypeCode rightTypeCode = Convert.GetTypeCode(n); + + if (leftTypeCode > rightTypeCode) + { + n = Convert.ChangeType(n, leftTypeCode); + } + else + { + m = Convert.ChangeType(m, rightTypeCode); + } + } + + #region Constructor (s) / Destructor + + // CLOVER:OFF + + /// + /// Creates a new instance of the class. + /// + /// + ///

+ /// This is a utility class, and as such exposes no public constructors. + ///

+ ///
+ private NumberUtils() + { + } + + // CLOVER:ON + + #endregion + } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Util/NumberUtilsTests.cs b/test/Spring/Spring.Core.Tests/Util/NumberUtilsTests.cs index 81d3d8ac..3017e863 100644 --- a/test/Spring/Spring.Core.Tests/Util/NumberUtilsTests.cs +++ b/test/Spring/Spring.Core.Tests/Util/NumberUtilsTests.cs @@ -169,8 +169,28 @@ namespace Spring.Util public void BitwiseOr() { Assert.AreEqual( ((Byte)2) | ((Byte)3), NumberUtils.BitwiseOr((Byte)2, (Byte)3)); +#if NET_2_0 Assert.AreEqual(((SByte)2) | ((SByte)3), NumberUtils.BitwiseOr((SByte)2, (SByte)3)); +#else + try + { + NumberUtils.BitwiseOr((SByte)2, (SByte)3); + Assert.Fail(); + } + catch (ArgumentException) + { } +#endif +#if NET_2_0 Assert.AreEqual(((Int16)2) | ((Int16)3), NumberUtils.BitwiseOr((Int16)2, (Int16)3)); +#else + try + { + NumberUtils.BitwiseOr((Int16) 2, (Int16) 3); + Assert.Fail(); + } + catch(ArgumentException) + {} +#endif Assert.AreEqual(((UInt16)2) | ((UInt16)3), NumberUtils.BitwiseOr((UInt16)2, (UInt16)3)); Assert.AreEqual(((Int32)2) | ((Int32)3), NumberUtils.BitwiseOr((Int32)2, (Int32)3)); Assert.AreEqual(((UInt32)2) | ((UInt32)3), NumberUtils.BitwiseOr((UInt32)2, (UInt32)3));