821 lines · 33.9 KB · C#
RawDownload
//******************************************************************************************************//  Current.cs - Gbtc////  Copyright © 2012, 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 MIT License (MIT), 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/MIT////  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://  ----------------------------------------------------------------------------------------------------//  01/25/2008 - J. Ritchie Carroll//       Initial version of source generated.//  09/11/2008 - J. Ritchie Carroll//       Converted to C#.//  08/10/2009 - Josh L. Patterson//       Edited Comments.//  09/14/2009 - Stephen C. Wills//       Added new header and license agreement.//  12/14/2012 - Starlynn Danyelle Gilliam//       Modified Header.//  10/03/2017 - J. Ritchie Carroll//       Added units enumeration with associated Convert method.////****************************************************************************************************** #region [ Contributor License Agreements ] /**************************************************************************\   Copyright © 2009 - J. Ritchie Carroll   All rights reserved.     Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions   are met:        * Redistributions of source code must retain the above copyright        notice, this list of conditions and the following disclaimer.             * Redistributions in binary form must reproduce the above        copyright notice, this list of conditions and the following        disclaimer in the documentation and/or other materials provided        with the distribution.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \**************************************************************************/ #endregion using System;using System.ComponentModel;using System.Globalization;using System.Runtime.CompilerServices; namespace GSF.Units{    #region [ Enumerations ]     /// <summary>    /// Represents the units available for a <see cref="Current"/> value.    /// </summary>    public enum CurrentUnit    {        /// <summary>        /// Ampere current units.        /// </summary>        Amperes,        /// <summary>        /// Abampere current units, a.k.a., an electromagnetic unit.        /// </summary>        Abamperes,        /// <summary>        /// Statampere current units, a.k.a., esu per second.        /// </summary>        Statamperes    }     #endregion     /// <summary>    /// Represents an electric current measurement, in amperes, as a double-precision floating-point number.    /// </summary>    /// <remarks>    /// This class behaves just like a <see cref="double"/> representing a current in amperes; it is implicitly    /// castable to and from a <see cref="double"/> and therefore can be generally used "as" a double, but it    /// has the advantage of handling conversions to and from other current representations, specifically    /// abampere (a.k.a., an electromagnetic unit) and statampere (a.k.a., esu per second). Metric conversions are handled    /// simply by applying the needed <see cref="SI"/> conversion factor, for example:    /// <example>    /// Convert current, in amperes, to kiloamperes:    /// <code>    /// public double GetKiloamperes(Current amperes)    /// {    ///     return amperes / SI.Kilo;    /// }    /// </code>    /// </example>    /// </remarks>    [Serializable]    public struct Current : IComparable, IFormattable, IConvertible, IComparable<Current>, IComparable<double>, IEquatable<Current>, IEquatable<double>    {        #region [ Members ]         // Constants        private const double AbamperesFactor = 10.0D;         private const double StatamperesFactor = 3.335641e-10D;         // Fields        private readonly double m_value; // Current value stored in amperes         #endregion         #region [ Constructors ]         /// <summary>        /// Creates a new <see cref="Current"/>.        /// </summary>        /// <param name="value">New current value in amperes.</param>        public Current(double value)        {            m_value = value;        }         #endregion         #region [ Methods ]         /// <summary>        /// Gets the <see cref="Current"/> value in abamperes.        /// </summary>        /// <returns>Value of <see cref="Current"/> in abamperes.</returns>        public double ToAbamperes()        {            return m_value / AbamperesFactor;        }         /// <summary>        /// Gets the <see cref="Current"/> value in statamperes.        /// </summary>        /// <returns>Value of <see cref="Current"/> in statamperes.</returns>        public double ToStatamperes()        {            return m_value / StatamperesFactor;        }         /// <summary>        /// Converts the <see cref="Current"/> to the specified <paramref name="targetUnit"/>.        /// </summary>        /// <param name="targetUnit">Target units.</param>        /// <returns><see cref="Current"/> converted to <paramref name="targetUnit"/>.</returns>        public double ConvertTo(CurrentUnit targetUnit)        {            switch (targetUnit)            {                case CurrentUnit.Amperes:                    return m_value;                case CurrentUnit.Abamperes:                    return ToAbamperes();                case CurrentUnit.Statamperes:                    return ToStatamperes();                default:                    throw new ArgumentOutOfRangeException(nameof(targetUnit), targetUnit, null);            }        }         #region [ Numeric Interface Implementations ]         /// <summary>        /// Compares this instance to a specified object and returns an indication of their relative values.        /// </summary>        /// <param name="value">An object to compare, or null.</param>        /// <returns>        /// A signed number indicating the relative values of this instance and value. Returns less than zero        /// if this instance is less than value, zero if this instance is equal to value, or greater than zero        /// if this instance is greater than value.        /// </returns>        /// <exception cref="ArgumentException">value is not a <see cref="Double"/> or <see cref="Current"/>.</exception>        public int CompareTo(object value)        {            if (value == null)                return 1;             double num;             if (value is double)                num = (double)value;             else if (value is Current)                num = (Current)value;             else                throw new ArgumentException("Argument must be a Double or a Current");             return m_value < num ? -1 : (m_value > num ? 1 : 0);        }         /// <summary>        /// Compares this instance to a specified <see cref="Current"/> and returns an indication of their        /// relative values.        /// </summary>        /// <param name="value">A <see cref="Current"/> to compare.</param>        /// <returns>        /// A signed number indicating the relative values of this instance and value. Returns less than zero        /// if this instance is less than value, zero if this instance is equal to value, or greater than zero        /// if this instance is greater than value.        /// </returns>        public int CompareTo(Current value)        {            return CompareTo((double)value);        }         /// <summary>        /// Compares this instance to a specified <see cref="Double"/> and returns an indication of their        /// relative values.        /// </summary>        /// <param name="value">A <see cref="Double"/> to compare.</param>        /// <returns>        /// A signed number indicating the relative values of this instance and value. Returns less than zero        /// if this instance is less than value, zero if this instance is equal to value, or greater than zero        /// if this instance is greater than value.        /// </returns>        public int CompareTo(double value)        {            return m_value < value ? -1 : (m_value > value ? 1 : 0);        }         /// <summary>        /// Returns a value indicating whether this instance is equal to a specified object.        /// </summary>        /// <param name="obj">An object to compare, or null.</param>        /// <returns>        /// True if obj is an instance of <see cref="Double"/> or <see cref="Current"/> and equals the value of this instance;        /// otherwise, False.        /// </returns>        public override bool Equals(object obj)        {            if (obj is double)                return Equals((double)obj);             if (obj is Current)                return Equals((Current)obj);             return false;        }         /// <summary>        /// Returns a value indicating whether this instance is equal to a specified <see cref="Current"/> value.        /// </summary>        /// <param name="obj">A <see cref="Current"/> value to compare to this instance.</param>        /// <returns>        /// True if obj has the same value as this instance; otherwise, False.        /// </returns>        public bool Equals(Current obj)        {            return Equals((double)obj);        }         /// <summary>        /// Returns a value indicating whether this instance is equal to a specified <see cref="Double"/> value.        /// </summary>        /// <param name="obj">A <see cref="Double"/> value to compare to this instance.</param>        /// <returns>        /// True if obj has the same value as this instance; otherwise, False.        /// </returns>        public bool Equals(double obj)        {            return m_value == obj;        }         /// <summary>        /// Returns the hash code for this instance.        /// </summary>        /// <returns>        /// A 32-bit signed integer hash code.        /// </returns>        public override int GetHashCode()        {            return m_value.GetHashCode();        }         /// <summary>        /// Converts the numeric value of this instance to its equivalent string representation.        /// </summary>        /// <returns>        /// The string representation of the value of this instance, consisting of a minus sign if        /// the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeros.        /// </returns>        public override string ToString()        {            return m_value.ToString();        }         /// <summary>        /// Converts the numeric value of this instance to its equivalent string representation, using        /// the specified format.        /// </summary>        /// <param name="format">A format string.</param>        /// <returns>        /// The string representation of the value of this instance as specified by format.        /// </returns>        public string ToString(string format)        {            return m_value.ToString(format);        }         /// <summary>        /// Converts the numeric value of this instance to its equivalent string representation using the        /// specified culture-specific format information.        /// </summary>        /// <param name="provider">        /// A <see cref="System.IFormatProvider"/> that supplies culture-specific formatting information.        /// </param>        /// <returns>        /// The string representation of the value of this instance as specified by provider.        /// </returns>        public string ToString(IFormatProvider provider)        {            return m_value.ToString(provider);        }         /// <summary>        /// Converts the numeric value of this instance to its equivalent string representation using the        /// specified format and culture-specific format information.        /// </summary>        /// <param name="format">A format specification.</param>        /// <param name="provider">        /// A <see cref="System.IFormatProvider"/> that supplies culture-specific formatting information.        /// </param>        /// <returns>        /// The string representation of the value of this instance as specified by format and provider.        /// </returns>        public string ToString(string format, IFormatProvider provider)        {            return m_value.ToString(format, provider);        }         /// <summary>        /// Converts the string representation of a number to its <see cref="Current"/> equivalent.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <returns>        /// A <see cref="Current"/> equivalent to the number contained in s.        /// </returns>        /// <exception cref="ArgumentNullException">s is null.</exception>        /// <exception cref="OverflowException">        /// s represents a number less than <see cref="Current.MinValue"/> or greater than <see cref="Current.MaxValue"/>.        /// </exception>        /// <exception cref="FormatException">s is not in the correct format.</exception>        public static Current Parse(string s)        {            return double.Parse(s);        }         /// <summary>        /// Converts the string representation of a number in a specified style to its <see cref="Current"/> equivalent.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <param name="style">        /// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.        /// </param>        /// <returns>        /// A <see cref="Current"/> equivalent to the number contained in s.        /// </returns>        /// <exception cref="ArgumentException">        /// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of        /// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.        /// </exception>        /// <exception cref="ArgumentNullException">s is null.</exception>        /// <exception cref="OverflowException">        /// s represents a number less than <see cref="Current.MinValue"/> or greater than <see cref="Current.MaxValue"/>.        /// </exception>        /// <exception cref="FormatException">s is not in a format compliant with style.</exception>        public static Current Parse(string s, NumberStyles style)        {            return double.Parse(s, style);        }         /// <summary>        /// Converts the string representation of a number in a specified culture-specific format to its <see cref="Current"/> equivalent.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <param name="provider">        /// A <see cref="System.IFormatProvider"/> that supplies culture-specific formatting information about s.        /// </param>        /// <returns>        /// A <see cref="Current"/> equivalent to the number contained in s.        /// </returns>        /// <exception cref="ArgumentNullException">s is null.</exception>        /// <exception cref="OverflowException">        /// s represents a number less than <see cref="Current.MinValue"/> or greater than <see cref="Current.MaxValue"/>.        /// </exception>        /// <exception cref="FormatException">s is not in the correct format.</exception>        public static Current Parse(string s, IFormatProvider provider)        {            return double.Parse(s, provider);        }         /// <summary>        /// Converts the string representation of a number in a specified style and culture-specific format to its <see cref="Current"/> equivalent.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <param name="style">        /// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.        /// </param>        /// <param name="provider">        /// A <see cref="System.IFormatProvider"/> that supplies culture-specific formatting information about s.        /// </param>        /// <returns>        /// A <see cref="Current"/> equivalent to the number contained in s.        /// </returns>        /// <exception cref="ArgumentException">        /// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of        /// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.        /// </exception>        /// <exception cref="ArgumentNullException">s is null.</exception>        /// <exception cref="OverflowException">        /// s represents a number less than <see cref="Current.MinValue"/> or greater than <see cref="Current.MaxValue"/>.        /// </exception>        /// <exception cref="FormatException">s is not in a format compliant with style.</exception>        public static Current Parse(string s, NumberStyles style, IFormatProvider provider)        {            return double.Parse(s, style, provider);        }         /// <summary>        /// Converts the string representation of a number to its <see cref="Current"/> equivalent. A return value        /// indicates whether the conversion succeeded or failed.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <param name="result">        /// When this method returns, contains the <see cref="Current"/> value equivalent to the number contained in s,        /// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null,        /// is not of the correct format, or represents a number less than <see cref="Current.MinValue"/> or greater than <see cref="Current.MaxValue"/>.        /// This parameter is passed uninitialized.        /// </param>        /// <returns>true if s was converted successfully; otherwise, false.</returns>        public static bool TryParse(string s, out Current result)        {            double parseResult;            bool parseResponse;             parseResponse = double.TryParse(s, out parseResult);            result = parseResult;             return parseResponse;        }         /// <summary>        /// Converts the string representation of a number in a specified style and culture-specific format to its        /// <see cref="Current"/> equivalent. A return value indicates whether the conversion succeeded or failed.        /// </summary>        /// <param name="s">A string containing a number to convert.</param>        /// <param name="style">        /// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.        /// </param>        /// <param name="result">        /// When this method returns, contains the <see cref="Current"/> value equivalent to the number contained in s,        /// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null,        /// is not in a format compliant with style, or represents a number less than <see cref="Current.MinValue"/> or        /// greater than <see cref="Current.MaxValue"/>. This parameter is passed uninitialized.        /// </param>        /// <param name="provider">        /// A <see cref="System.IFormatProvider"/> object that supplies culture-specific formatting information about s.        /// </param>        /// <returns>true if s was converted successfully; otherwise, false.</returns>        /// <exception cref="ArgumentException">        /// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of        /// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.        /// </exception>        public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out Current result)        {            double parseResult;            bool parseResponse;             parseResponse = double.TryParse(s, style, provider, out parseResult);            result = parseResult;             return parseResponse;        }         /// <summary>

Lines 1–500 of 821.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t