Showing the changes to Source/Libraries/GSF.Core/NumericExtensions.cs (1 of 628 files). Show all
Unified · Split
| @@ -0,0 +1,163 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // NumericExtensions.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 09/18/2008 - James R. Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 02/16/2009 - Josh L. Patterson | ||
| 22 | // Edited Code Comments. | ||
| 23 | // 09/14/2009 - Stephen C. Wills | ||
| 24 | // Added new header and license agreement. | ||
| 25 | // 10/8/2012 - Danyelle Gilliam | ||
| 26 | // Modified Header | ||
| 27 | // | ||
| 28 | //****************************************************************************************************** | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | using System; | ||
| 33 | |||
| 34 | namespace GSF | ||
| 35 | { | ||
| 36 | /// <summary>Defines extension functions related to numbers.</summary> | ||
| 37 | public static class NumericExtensions | ||
| 38 | { | ||
| 39 | /// <summary>Ensures parameter passed to function is not zero. Returns -1 | ||
| 40 | /// if <paramref name="source">source</paramref> is zero.</summary> | ||
| 41 | /// <param name="source">Value to test for zero.</param> | ||
| 42 | /// <typeparam name="T">Return type used for immediate expression</typeparam> | ||
| 43 | /// <returns>A non-zero value.</returns> | ||
| 44 | public static T NotZero<T>(this T source) where T : struct, IEquatable<T> | ||
| 45 | { | ||
| 46 | return NotZero<T>(source, (T)Convert.ChangeType(-1, typeof(T))); | ||
| 47 | } | ||
| 48 | |||
| 49 | /// <summary>Ensures parameter passed to function is not zero.</summary> | ||
| 50 | /// <param name="source">Value to test for zero.</param> | ||
| 51 | /// <param name="nonZeroReturnValue">Value to return if <paramref name="source">source</paramref> is | ||
| 52 | /// zero.</param> | ||
| 53 | /// <typeparam name="T">Return type used for immediate expression</typeparam> | ||
| 54 | /// <returns>A non-zero value.</returns> | ||
| 55 | /// <remarks>To optimize performance, this function does not validate that the notZeroReturnValue is not | ||
| 56 | /// zero.</remarks> | ||
| 57 | public static T NotZero<T>(this T source, T nonZeroReturnValue) where T : struct, IEquatable<T> | ||
| 58 | { | ||
| 59 | return (((IEquatable<T>)source).Equals(default(T)) ? nonZeroReturnValue : source); | ||
| 60 | } | ||
| 61 | |||
| 62 | /// <summary>Ensures test parameter passed to function is not equal to the specified value.</summary> | ||
| 63 | /// <param name="source">Value to test.</param> | ||
| 64 | /// <param name="notEqualToValue">Value that represents the undesired value (e.g., zero).</param> | ||
| 65 | /// <param name="alternateValue">Value to return if <paramref name="source">source</paramref> is equal | ||
| 66 | /// to the undesired value.</param> | ||
| 67 | /// <typeparam name="T">Structure or class that implements IEquatable(Of T) (e.g., Double, Single, | ||
| 68 | /// Integer, etc.).</typeparam> | ||
| 69 | /// <returns>A value not equal to notEqualToValue.</returns> | ||
| 70 | /// <remarks>To optimize performance, this function does not validate that the notEqualToValue is not equal | ||
| 71 | /// to the alternateValue.</remarks> | ||
| 72 | public static T NotEqualTo<T>(this T source, T notEqualToValue, T alternateValue) where T : IEquatable<T> | ||
| 73 | { | ||
| 74 | return (((IEquatable<T>)source).Equals(notEqualToValue) ? alternateValue : source); | ||
| 75 | } | ||
| 76 | |||
| 77 | /// <summary>Ensures test parameter passed to function is not less than the specified value.</summary> | ||
| 78 | /// <param name="source">Value to test.</param> | ||
| 79 | /// <param name="notLessThanValue">Value that represents the lower limit for the source. This value | ||
| 80 | /// is returned if source is less than notLessThanValue.</param> | ||
| 81 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 82 | /// Integer, etc.).</typeparam> | ||
| 83 | /// <returns>A value not less than notLessThanValue.</returns> | ||
| 84 | /// <remarks>If source is less than notLessThanValue, then notLessThanValue is returned.</remarks> | ||
| 85 | public static T NotLessThan<T>(this T source, T notLessThanValue) where T : IComparable<T> | ||
| 86 | { | ||
| 87 | return (((IComparable<T>)source).CompareTo(notLessThanValue) < 0 ? notLessThanValue : source); | ||
| 88 | } | ||
| 89 | |||
| 90 | /// <summary>Ensures test parameter passed to function is not less than the specified value.</summary> | ||
| 91 | /// <param name="source">Value to test.</param> | ||
| 92 | /// <param name="notLessThanValue">Value that represents the lower limit for the source.</param> | ||
| 93 | /// <param name="alternateValue">Value to return if <paramref name="source">source</paramref> is | ||
| 94 | /// less than <paramref name="notLessThanValue">notLessThanValue</paramref>.</param> | ||
| 95 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 96 | /// Integer, etc.).</typeparam> | ||
| 97 | /// <returns>A value not less than notLessThanValue.</returns> | ||
| 98 | /// <remarks>To optimize performance, this function does not validate that the notLessThanValue is not | ||
| 99 | /// less than the alternateValue.</remarks> | ||
| 100 | public static T NotLessThan<T>(this T source, T notLessThanValue, T alternateValue) where T : IComparable<T> | ||
| 101 | { | ||
| 102 | return (((IComparable<T>)source).CompareTo(notLessThanValue) < 0 ? alternateValue : source); | ||
| 103 | } | ||
| 104 | |||
| 105 | /// <summary>Ensures test parameter passed to function is not less than or equal to the specified value.</summary> | ||
| 106 | /// <param name="source">Value to test.</param> | ||
| 107 | /// <param name="notLessThanOrEqualToValue">Value that represents the lower limit for the source.</param> | ||
| 108 | /// <param name="alternateValue">Value to return if <paramref name="source">source</paramref> is | ||
| 109 | /// less than or equal to <paramref name="notLessThanOrEqualToValue">notLessThanOrEqualToValue</paramref>.</param> | ||
| 110 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 111 | /// Integer, etc.).</typeparam> | ||
| 112 | /// <returns>A value not less than or equal to notLessThanOrEqualToValue.</returns> | ||
| 113 | /// <remarks>To optimize performance, this function does not validate that the notLessThanOrEqualToValue is | ||
| 114 | /// not less than or equal to the alternateValue.</remarks> | ||
| 115 | public static T NotLessThanOrEqualTo<T>(this T source, T notLessThanOrEqualToValue, T alternateValue) where T : IComparable<T> | ||
| 116 | { | ||
| 117 | return (((IComparable<T>)source).CompareTo(notLessThanOrEqualToValue) <= 0 ? alternateValue : source); | ||
| 118 | } | ||
| 119 | |||
| 120 | /// <summary>Ensures test parameter passed to function is not greater than the specified value.</summary> | ||
| 121 | /// <param name="source">Value to test.</param> | ||
| 122 | /// <param name="notGreaterThanValue">Value that represents the upper limit for the source. This | ||
| 123 | /// value is returned if source is greater than notGreaterThanValue.</param> | ||
| 124 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 125 | /// Integer, etc.).</typeparam> | ||
| 126 | /// <returns>A value not greater than notGreaterThanValue.</returns> | ||
| 127 | /// <remarks>If source is greater than notGreaterThanValue, then notGreaterThanValue is returned.</remarks> | ||
| 128 | public static T NotGreaterThan<T>(this T source, T notGreaterThanValue) where T : IComparable<T> | ||
| 129 | { | ||
| 130 | return (((IComparable<T>)source).CompareTo(notGreaterThanValue) > 0 ? notGreaterThanValue : source); | ||
| 131 | } | ||
| 132 | |||
| 133 | /// <summary>Ensures test parameter passed to function is not greater than the specified value.</summary> | ||
| 134 | /// <param name="source">Value to test.</param> | ||
| 135 | /// <param name="notGreaterThanValue">Value that represents the upper limit for the source.</param> | ||
| 136 | /// <param name="alternateValue">Value to return if <paramref name="source">source</paramref> is | ||
| 137 | /// greater than <paramref name="notGreaterThanValue">notGreaterThanValue</paramref>.</param> | ||
| 138 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 139 | /// Integer, etc.).</typeparam> | ||
| 140 | /// <returns>A value not greater than notGreaterThanValue.</returns> | ||
| 141 | /// <remarks>To optimize performance, this function does not validate that the notGreaterThanValue is | ||
| 142 | /// not greater than the alternateValue</remarks> | ||
| 143 | public static T NotGreaterThan<T>(this T source, T notGreaterThanValue, T alternateValue) where T : IComparable<T> | ||
| 144 | { | ||
| 145 | return (((IComparable<T>)source).CompareTo(notGreaterThanValue) > 0 ? alternateValue : source); | ||
| 146 | } | ||
| 147 | |||
| 148 | /// <summary>Ensures test parameter passed to function is not greater than or equal to the specified value.</summary> | ||
| 149 | /// <param name="source">Value to test.</param> | ||
| 150 | /// <param name="notGreaterThanOrEqualToValue">Value that represents the upper limit for the source.</param> | ||
| 151 | /// <param name="alternateValue">Value to return if <paramref name="source">source</paramref> is | ||
| 152 | /// greater than or equal to <paramref name="notGreaterThanOrEqualToValue">notGreaterThanOrEqualToValue</paramref>.</param> | ||
| 153 | /// <typeparam name="T">Structure or class that implements IComparable(Of T) (e.g., Double, Single, | ||
| 154 | /// Integer, etc.).</typeparam> | ||
| 155 | /// <returns>A value not greater than or equal to notGreaterThanOrEqualToValue.</returns> | ||
| 156 | /// <remarks>To optimize performance, this function does not validate that the notGreaterThanOrEqualToValue | ||
| 157 | /// is not greater than or equal to the alternateValue.</remarks> | ||
| 158 | public static T NotGreaterThanOrEqualTo<T>(this T source, T notGreaterThanOrEqualToValue, T alternateValue) where T : IComparable<T> | ||
| 159 | { | ||
| 160 | return (((IComparable<T>)source).CompareTo(notGreaterThanOrEqualToValue) >= 0 ? alternateValue : source); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||