|
| 1 | + // Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]). |
| 2 | +// https://github.com/angularsen/UnitsNet |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | +using System.Text; |
| 24 | +using UnitsNet.Units; |
| 25 | + |
| 26 | +namespace UnitsNet |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Represents the base units for a quantity or |
| 30 | + /// </summary> |
| 31 | + public sealed class BaseUnits |
| 32 | + { |
| 33 | + /// <inheritdoc /> |
| 34 | + public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, |
| 35 | + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) |
| 36 | + { |
| 37 | + if(length == LengthUnit.Undefined) |
| 38 | + throw new ArgumentException("The given length unit is undefined.", nameof(length)); |
| 39 | + else if(mass == MassUnit.Undefined) |
| 40 | + throw new ArgumentException("The given mass unit is undefined.", nameof(mass)); |
| 41 | + else if(time == DurationUnit.Undefined) |
| 42 | + throw new ArgumentException("The given time unit is undefined.", nameof(time)); |
| 43 | + else if(current == ElectricCurrentUnit.Undefined) |
| 44 | + throw new ArgumentException("The given electric current unit is undefined.", nameof(current)); |
| 45 | + else if(temperature == TemperatureUnit.Undefined) |
| 46 | + throw new ArgumentException("The given temperature unit is undefined.", nameof(temperature)); |
| 47 | + else if(amount == AmountOfSubstanceUnit.Undefined) |
| 48 | + throw new ArgumentException("The given amount of substance unit is undefined.", nameof(amount)); |
| 49 | + else if(luminousIntensity == LuminousIntensityUnit.Undefined) |
| 50 | + throw new ArgumentException("The given luminous intensity unit is undefined.", nameof(luminousIntensity)); |
| 51 | + |
| 52 | + Length = length; |
| 53 | + Mass = mass; |
| 54 | + Time = time; |
| 55 | + Current = current; |
| 56 | + Temperature = temperature; |
| 57 | + Amount = amount; |
| 58 | + LuminousIntensity = luminousIntensity; |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc /> |
| 62 | + public override bool Equals(object obj) |
| 63 | + { |
| 64 | + if(obj is null || !(obj is BaseUnits)) |
| 65 | + return false; |
| 66 | + |
| 67 | + var other = (BaseUnits)obj; |
| 68 | + |
| 69 | + return Length == other.Length && |
| 70 | + Mass == other.Mass && |
| 71 | + Time == other.Time && |
| 72 | + Current == other.Current && |
| 73 | + Temperature == other.Temperature && |
| 74 | + Amount == other.Amount && |
| 75 | + LuminousIntensity == other.LuminousIntensity; |
| 76 | + } |
| 77 | + |
| 78 | + /// <inheritdoc /> |
| 79 | + public override int GetHashCode() |
| 80 | + { |
| 81 | + return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode(); |
| 82 | + } |
| 83 | + |
| 84 | +#if !WINDOWS_UWP |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Check if two dimensions are equal. |
| 88 | + /// </summary> |
| 89 | + /// <param name="left">Left.</param> |
| 90 | + /// <param name="right">Right.</param> |
| 91 | + /// <returns>True if equal.</returns> |
| 92 | + public static bool operator ==(BaseUnits left, BaseUnits right) |
| 93 | + { |
| 94 | + return left?.Equals(right) == true; |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Check if two dimensions are unequal. |
| 99 | + /// </summary> |
| 100 | + /// <param name="left">Left.</param> |
| 101 | + /// <param name="right">Right.</param> |
| 102 | + /// <returns>True if not equal.</returns> |
| 103 | + public static bool operator !=(BaseUnits left, BaseUnits right) |
| 104 | + { |
| 105 | + return !(left == right); |
| 106 | + } |
| 107 | + |
| 108 | +#endif |
| 109 | + |
| 110 | + /// <inheritdoc /> |
| 111 | + public override string ToString() |
| 112 | + { |
| 113 | + var sb = new StringBuilder(); |
| 114 | + |
| 115 | + sb.AppendFormat("[Length]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Length)); |
| 116 | + sb.AppendFormat("[Mass]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Mass)); |
| 117 | + sb.AppendFormat("[Time]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Time)); |
| 118 | + sb.AppendFormat("[Current]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Current)); |
| 119 | + sb.AppendFormat("[Temperature]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Temperature)); |
| 120 | + sb.AppendFormat("[Amount]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Amount)); |
| 121 | + sb.AppendFormat("[LuminousIntensity]: {0} ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensity)); |
| 122 | + |
| 123 | + return sb.ToString(); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Gets the length unit (L). |
| 128 | + /// </summary> |
| 129 | + public LengthUnit Length { get; } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Gets the mass unit (M). |
| 133 | + /// </summary> |
| 134 | + public MassUnit Mass{ get; } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Gets the time unit (T). |
| 138 | + /// </summary> |
| 139 | + public DurationUnit Time{ get; } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Gets the electric current unit (I). |
| 143 | + /// </summary> |
| 144 | + public ElectricCurrentUnit Current{ get; } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Gets the temperature unit (Θ). |
| 148 | + /// </summary> |
| 149 | + public TemperatureUnit Temperature{ get; } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Gets the amount of substance unit (N). |
| 153 | + /// </summary> |
| 154 | + public AmountOfSubstanceUnit Amount{ get; } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Gets the luminous intensity unit (J). |
| 158 | + /// </summary> |
| 159 | + public LuminousIntensityUnit LuminousIntensity{ get; } |
| 160 | + } |
| 161 | +} |
0 commit comments