|
| 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 System.Linq; |
| 25 | + |
| 26 | +namespace UnitsNet |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Represents the base dimensions of a quantity. |
| 30 | + /// </summary> |
| 31 | + public sealed class BaseDimensions |
| 32 | + { |
| 33 | + /// <inheritdoc /> |
| 34 | + public BaseDimensions(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity) |
| 35 | + { |
| 36 | + Length = length; |
| 37 | + Mass = mass; |
| 38 | + Time = time; |
| 39 | + Current = current; |
| 40 | + Temperature = temperature; |
| 41 | + Amount = amount; |
| 42 | + LuminousIntensity = luminousIntensity; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Checks if the dimensions represent a base quantity. |
| 47 | + /// </summary> |
| 48 | + /// <returns>True if the dimensions represent a base quantity, otherwise false.</returns> |
| 49 | + public bool IsBaseQuantity() |
| 50 | + { |
| 51 | + var dimensionsArray = new[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}; |
| 52 | + bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1; |
| 53 | + return onlyOneEqualsOne; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Checks if the dimensions represent a derived quantity. |
| 58 | + /// </summary> |
| 59 | + /// <returns>True if the dimensions represent a derived quantity, otherwise false.</returns> |
| 60 | + public bool IsDerivedQuantity() |
| 61 | + { |
| 62 | + return !IsBaseQuantity() && !IsDimensionless(); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Checks if this base dimensions object represents a dimensionless quantity. |
| 67 | + /// </summary> |
| 68 | + /// <returns>True if this object represents a dimensionless quantity, otherwise false.</returns> |
| 69 | + public bool IsDimensionless() |
| 70 | + { |
| 71 | + return Equals(this, Dimensionless); |
| 72 | + } |
| 73 | + |
| 74 | + /// <inheritdoc /> |
| 75 | + public override bool Equals(object obj) |
| 76 | + { |
| 77 | + if(!(obj is BaseDimensions)) |
| 78 | + return false; |
| 79 | + |
| 80 | + var other = (BaseDimensions)obj; |
| 81 | + |
| 82 | + return Length == other.Length && |
| 83 | + Mass == other.Mass && |
| 84 | + Time == other.Time && |
| 85 | + Current == other.Current && |
| 86 | + Temperature == other.Temperature && |
| 87 | + Amount == other.Amount && |
| 88 | + LuminousIntensity == other.LuminousIntensity; |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc /> |
| 92 | + public override int GetHashCode() |
| 93 | + { |
| 94 | + return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode(); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Get resulting dimensions after multiplying two dimensions, by performing addition of each dimension. |
| 99 | + /// </summary> |
| 100 | + /// <param name="right">Other dimensions.</param> |
| 101 | + /// <returns>Resulting dimensions.</returns> |
| 102 | + public BaseDimensions Multiply(BaseDimensions right) |
| 103 | + { |
| 104 | + if(right is null) |
| 105 | + throw new ArgumentNullException(nameof(right)); |
| 106 | + |
| 107 | + return new BaseDimensions( |
| 108 | + Length + right.Length, |
| 109 | + Mass + right.Mass, |
| 110 | + Time + right.Time, |
| 111 | + Current + right.Current, |
| 112 | + Temperature + right.Temperature, |
| 113 | + Amount + right.Amount, |
| 114 | + LuminousIntensity + right.LuminousIntensity); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Get resulting dimensions after dividing two dimensions, by performing subtraction of each dimension. |
| 119 | + /// </summary> |
| 120 | + /// <param name="right">Other dimensions.</param> |
| 121 | + /// <returns>Resulting dimensions.</returns> |
| 122 | + public BaseDimensions Divide(BaseDimensions right) |
| 123 | + { |
| 124 | + if(right is null) |
| 125 | + throw new ArgumentNullException(nameof(right)); |
| 126 | + |
| 127 | + return new BaseDimensions( |
| 128 | + Length - right.Length, |
| 129 | + Mass - right.Mass, |
| 130 | + Time - right.Time, |
| 131 | + Current - right.Current, |
| 132 | + Temperature - right.Temperature, |
| 133 | + Amount - right.Amount, |
| 134 | + LuminousIntensity - right.LuminousIntensity); |
| 135 | + } |
| 136 | + |
| 137 | + /// <inheritdoc /> |
| 138 | + public override string ToString() |
| 139 | + { |
| 140 | + var sb = new StringBuilder(); |
| 141 | + |
| 142 | + AppendDimensionString(sb, "Length", Length); |
| 143 | + AppendDimensionString(sb, "Mass", Mass); |
| 144 | + AppendDimensionString(sb, "Time", Time); |
| 145 | + AppendDimensionString(sb, "Current", Current); |
| 146 | + AppendDimensionString(sb, "Temperature", Temperature); |
| 147 | + AppendDimensionString(sb, "Amount", Amount); |
| 148 | + AppendDimensionString(sb, "LuminousIntensity", LuminousIntensity); |
| 149 | + |
| 150 | + return sb.ToString(); |
| 151 | + } |
| 152 | + |
| 153 | + private static void AppendDimensionString(StringBuilder sb, string name, int value) |
| 154 | + { |
| 155 | + var absoluteValue = Math.Abs(value); |
| 156 | + |
| 157 | + if(absoluteValue > 0) |
| 158 | + { |
| 159 | + sb.AppendFormat("[{0}]", name); |
| 160 | + |
| 161 | + if(absoluteValue > 1) |
| 162 | + sb.AppendFormat("^{0}", value); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Gets the length dimensions (L). |
| 168 | + /// </summary> |
| 169 | + public int Length { get; } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Gets the mass dimensions (M). |
| 173 | + /// </summary> |
| 174 | + public int Mass{ get; } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Gets the time dimensions (T). |
| 178 | + /// </summary> |
| 179 | + public int Time{ get; } |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Gets the electric current dimensions (I). |
| 183 | + /// </summary> |
| 184 | + public int Current{ get; } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Gets the temperature dimensions (Θ). |
| 188 | + /// </summary> |
| 189 | + public int Temperature{ get; } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Gets the amount of substance dimensions (N). |
| 193 | + /// </summary> |
| 194 | + public int Amount{ get; } |
| 195 | + |
| 196 | + /// <summary> |
| 197 | + /// Gets the luminous intensity dimensions (J). |
| 198 | + /// </summary> |
| 199 | + public int LuminousIntensity{ get; } |
| 200 | + |
| 201 | + /// <summary> |
| 202 | + /// Represents a dimensionless (unitless) quantity. |
| 203 | + /// </summary> |
| 204 | + public static BaseDimensions Dimensionless { get; } = new BaseDimensions(0, 0, 0, 0, 0, 0, 0); |
| 205 | + } |
| 206 | +} |
0 commit comments