//****************************************************************************************************** // NumericExtensions.cs - Gbtc // // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/eclipse-1.0.php // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 09/18/2008 - James R. Carroll // Generated original version of source code. // 02/16/2009 - Josh L. Patterson // Edited Code Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 10/8/2012 - Danyelle Gilliam // Modified Header // //****************************************************************************************************** using System; namespace GSF { /// Defines extension functions related to numbers. public static class NumericExtensions { /// Ensures parameter passed to function is not zero. Returns -1 /// if source is zero. /// Value to test for zero. /// Return type used for immediate expression /// A non-zero value. public static T NotZero(this T source) where T : struct, IEquatable { return NotZero(source, (T)Convert.ChangeType(-1, typeof(T))); } /// Ensures parameter passed to function is not zero. /// Value to test for zero. /// Value to return if source is /// zero. /// Return type used for immediate expression /// A non-zero value. /// To optimize performance, this function does not validate that the notZeroReturnValue is not /// zero. public static T NotZero(this T source, T nonZeroReturnValue) where T : struct, IEquatable { return (((IEquatable)source).Equals(default(T)) ? nonZeroReturnValue : source); } /// Ensures test parameter passed to function is not equal to the specified value. /// Value to test. /// Value that represents the undesired value (e.g., zero). /// Value to return if source is equal /// to the undesired value. /// Structure or class that implements IEquatable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not equal to notEqualToValue. /// To optimize performance, this function does not validate that the notEqualToValue is not equal /// to the alternateValue. public static T NotEqualTo(this T source, T notEqualToValue, T alternateValue) where T : IEquatable { return (((IEquatable)source).Equals(notEqualToValue) ? alternateValue : source); } /// Ensures test parameter passed to function is not less than the specified value. /// Value to test. /// Value that represents the lower limit for the source. This value /// is returned if source is less than notLessThanValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not less than notLessThanValue. /// If source is less than notLessThanValue, then notLessThanValue is returned. public static T NotLessThan(this T source, T notLessThanValue) where T : IComparable { return (((IComparable)source).CompareTo(notLessThanValue) < 0 ? notLessThanValue : source); } /// Ensures test parameter passed to function is not less than the specified value. /// Value to test. /// Value that represents the lower limit for the source. /// Value to return if source is /// less than notLessThanValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not less than notLessThanValue. /// To optimize performance, this function does not validate that the notLessThanValue is not /// less than the alternateValue. public static T NotLessThan(this T source, T notLessThanValue, T alternateValue) where T : IComparable { return (((IComparable)source).CompareTo(notLessThanValue) < 0 ? alternateValue : source); } /// Ensures test parameter passed to function is not less than or equal to the specified value. /// Value to test. /// Value that represents the lower limit for the source. /// Value to return if source is /// less than or equal to notLessThanOrEqualToValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not less than or equal to notLessThanOrEqualToValue. /// To optimize performance, this function does not validate that the notLessThanOrEqualToValue is /// not less than or equal to the alternateValue. public static T NotLessThanOrEqualTo(this T source, T notLessThanOrEqualToValue, T alternateValue) where T : IComparable { return (((IComparable)source).CompareTo(notLessThanOrEqualToValue) <= 0 ? alternateValue : source); } /// Ensures test parameter passed to function is not greater than the specified value. /// Value to test. /// Value that represents the upper limit for the source. This /// value is returned if source is greater than notGreaterThanValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not greater than notGreaterThanValue. /// If source is greater than notGreaterThanValue, then notGreaterThanValue is returned. public static T NotGreaterThan(this T source, T notGreaterThanValue) where T : IComparable { return (((IComparable)source).CompareTo(notGreaterThanValue) > 0 ? notGreaterThanValue : source); } /// Ensures test parameter passed to function is not greater than the specified value. /// Value to test. /// Value that represents the upper limit for the source. /// Value to return if source is /// greater than notGreaterThanValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not greater than notGreaterThanValue. /// To optimize performance, this function does not validate that the notGreaterThanValue is /// not greater than the alternateValue public static T NotGreaterThan(this T source, T notGreaterThanValue, T alternateValue) where T : IComparable { return (((IComparable)source).CompareTo(notGreaterThanValue) > 0 ? alternateValue : source); } /// Ensures test parameter passed to function is not greater than or equal to the specified value. /// Value to test. /// Value that represents the upper limit for the source. /// Value to return if source is /// greater than or equal to notGreaterThanOrEqualToValue. /// Structure or class that implements IComparable(Of T) (e.g., Double, Single, /// Integer, etc.). /// A value not greater than or equal to notGreaterThanOrEqualToValue. /// To optimize performance, this function does not validate that the notGreaterThanOrEqualToValue /// is not greater than or equal to the alternateValue. public static T NotGreaterThanOrEqualTo(this T source, T notGreaterThanOrEqualToValue, T alternateValue) where T : IComparable { return (((IComparable)source).CompareTo(notGreaterThanOrEqualToValue) >= 0 ? alternateValue : source); } } }