Skip to content

Commit 9add272

Browse files
authored
Merge pull request #444 from tmilnthorp/BaseDimensions
Add BaseDimensions type and tests Add static and instance BaseDimensions props to quantities
2 parents 09bf7fa + a1e516f commit 9add272

File tree

178 files changed

+3022
-686
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+3022
-686
lines changed

UnitsNet.Tests/BaseDimensionsTests.cs

Lines changed: 580 additions & 0 deletions
Large diffs are not rendered by default.

UnitsNet/BaseDimensions.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
25+
namespace UnitsNet
26+
{
27+
/// <summary>
28+
/// Represents the base dimensions of a quantity.
29+
/// </summary>
30+
public sealed class BaseDimensions
31+
{
32+
public BaseDimensions(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
33+
{
34+
Length = length;
35+
Mass = mass;
36+
Time = time;
37+
Current = current;
38+
Temperature = temperature;
39+
Amount = amount;
40+
LuminousIntensity = luminousIntensity;
41+
}
42+
43+
public override bool Equals(object obj)
44+
{
45+
if(obj is null || !(obj is BaseDimensions))
46+
return false;
47+
48+
var baseDimensionsObj = (BaseDimensions)obj;
49+
50+
return Length == baseDimensionsObj.Length &&
51+
Mass == baseDimensionsObj.Mass &&
52+
Time == baseDimensionsObj.Time &&
53+
Current == baseDimensionsObj.Current &&
54+
Temperature == baseDimensionsObj.Temperature &&
55+
Amount == baseDimensionsObj.Amount &&
56+
LuminousIntensity == baseDimensionsObj.LuminousIntensity;
57+
}
58+
59+
public override int GetHashCode()
60+
{
61+
int hash = 17;
62+
hash = hash * 23 + Length;
63+
hash = hash * 23 + Mass;
64+
hash = hash * 23 + Time;
65+
hash = hash * 23 + Current;
66+
hash = hash * 23 + Temperature;
67+
hash = hash * 23 + Amount;
68+
hash = hash * 23 + LuminousIntensity;
69+
return hash;
70+
}
71+
72+
public BaseDimensions Multiply(BaseDimensions right)
73+
{
74+
return new BaseDimensions(
75+
Length + right.Length,
76+
Mass + right.Mass,
77+
Time + right.Time,
78+
Current + right.Current,
79+
Temperature + right.Temperature,
80+
Amount + right.Amount,
81+
LuminousIntensity + right.LuminousIntensity);
82+
}
83+
84+
public BaseDimensions Divide(BaseDimensions right)
85+
{
86+
return new BaseDimensions(
87+
Length - right.Length,
88+
Mass - right.Mass,
89+
Time - right.Time,
90+
Current - right.Current,
91+
Temperature - right.Temperature,
92+
Amount - right.Amount,
93+
LuminousIntensity - right.LuminousIntensity);
94+
}
95+
96+
#if !WINDOWS_UWP
97+
public static bool operator ==(BaseDimensions left, BaseDimensions right)
98+
{
99+
return left.Equals(right);
100+
}
101+
102+
public static bool operator !=(BaseDimensions left, BaseDimensions right)
103+
{
104+
return !( left == right );
105+
}
106+
107+
public static BaseDimensions operator *(BaseDimensions left, BaseDimensions right)
108+
{
109+
return left.Multiply(right);
110+
}
111+
112+
public static BaseDimensions operator /(BaseDimensions left, BaseDimensions right)
113+
{
114+
return left.Divide(right);
115+
}
116+
#endif
117+
118+
public override string ToString()
119+
{
120+
var sb = new StringBuilder();
121+
122+
AppendDimensionString(sb, "Length", Length);
123+
AppendDimensionString(sb, "Mass", Mass);
124+
AppendDimensionString(sb, "Time", Time);
125+
AppendDimensionString(sb, "Current", Current);
126+
AppendDimensionString(sb, "Temperature", Temperature);
127+
AppendDimensionString(sb, "Amount", Amount);
128+
AppendDimensionString(sb, "LuminousIntensity", LuminousIntensity);
129+
130+
return sb.ToString();
131+
}
132+
133+
private static void AppendDimensionString( StringBuilder sb, string name, int value )
134+
{
135+
var absoluteValue = Math.Abs( value );
136+
137+
if( absoluteValue > 0 )
138+
{
139+
sb.AppendFormat( "[{0}]", name );
140+
141+
if( absoluteValue > 1 )
142+
sb.AppendFormat( "^{0}", value );
143+
}
144+
}
145+
146+
/// <summary>
147+
/// Gets the length dimensions (L).
148+
/// </summary>
149+
public int Length { get; }
150+
151+
/// <summary>
152+
/// Gets the mass dimensions (M).
153+
/// </summary>
154+
public int Mass{ get; }
155+
156+
/// <summary>
157+
/// Gets the time dimensions (T).
158+
/// </summary>
159+
public int Time{ get; }
160+
161+
/// <summary>
162+
/// Gets the electric current dimensions (I).
163+
/// </summary>
164+
public int Current{ get; }
165+
166+
/// <summary>
167+
/// Gets the temperature dimensions (Θ).
168+
/// </summary>
169+
public int Temperature{ get; }
170+
171+
/// <summary>
172+
/// Gets the amount of substance dimensions (N).
173+
/// </summary>
174+
public int Amount{ get; }
175+
176+
/// <summary>
177+
/// Gets the luminous intensity dimensions (J).
178+
/// </summary>
179+
public int LuminousIntensity{ get; }
180+
}
181+
}

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ public Acceleration(double meterspersecondsquared)
158158
/// </summary>
159159
public static AccelerationUnit BaseUnit => AccelerationUnit.MeterPerSecondSquared;
160160

161+
private static readonly BaseDimensions _baseDimensions = new BaseDimensions(1, 0, -2, 0, 0, 0, 0);
162+
163+
/// <summary>
164+
/// The <see cref="BaseDimensions" /> of this quantity.
165+
/// </summary>
166+
public static BaseDimensions BaseDimensions
167+
{
168+
get{ return _baseDimensions; }
169+
}
170+
161171
/// <summary>
162172
/// All units of measurement for the Acceleration quantity.
163173
/// </summary>
@@ -805,7 +815,7 @@ public bool Equals(Acceleration other, Acceleration maxError)
805815

806816
public override int GetHashCode()
807817
{
808-
return new { Value, Unit }.GetHashCode();
818+
return new { Value, Unit }.GetHashCode();
809819
}
810820

811821
#endregion
@@ -1199,7 +1209,7 @@ public string ToString(
11991209
/// <returns>The value in the base unit representation.</returns>
12001210
private double AsBaseUnitMetersPerSecondSquared()
12011211
{
1202-
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return _value; }
1212+
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return _value; }
12031213

12041214
switch (Unit)
12051215
{
@@ -1218,10 +1228,15 @@ private double AsBaseUnitMetersPerSecondSquared()
12181228
case AccelerationUnit.StandardGravity: return _value*9.80665;
12191229
default:
12201230
throw new NotImplementedException("Unit not implemented: " + Unit);
1221-
}
1222-
}
1231+
}
1232+
}
12231233

1224-
/// <summary>Convenience method for working with internal numeric type.</summary>
1234+
/// <summary>Convenience method for working with internal numeric type.</summary>
12251235
private double AsBaseNumericType(AccelerationUnit unit) => Convert.ToDouble(As(unit));
1226-
}
1236+
1237+
/// <summary>
1238+
/// The <see cref="BaseDimensions" /> of this quantity.
1239+
/// </summary>
1240+
public BaseDimensions Dimensions => Acceleration.BaseDimensions;
1241+
}
12271242
}

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ public AmountOfSubstance(double moles)
158158
/// </summary>
159159
public static AmountOfSubstanceUnit BaseUnit => AmountOfSubstanceUnit.Mole;
160160

161+
private static readonly BaseDimensions _baseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 1, 0);
162+
163+
/// <summary>
164+
/// The <see cref="BaseDimensions" /> of this quantity.
165+
/// </summary>
166+
public static BaseDimensions BaseDimensions
167+
{
168+
get{ return _baseDimensions; }
169+
}
170+
161171
/// <summary>
162172
/// All units of measurement for the AmountOfSubstance quantity.
163173
/// </summary>
@@ -838,7 +848,7 @@ public bool Equals(AmountOfSubstance other, AmountOfSubstance maxError)
838848

839849
public override int GetHashCode()
840850
{
841-
return new { Value, Unit }.GetHashCode();
851+
return new { Value, Unit }.GetHashCode();
842852
}
843853

844854
#endregion
@@ -1233,7 +1243,7 @@ public string ToString(
12331243
/// <returns>The value in the base unit representation.</returns>
12341244
private double AsBaseUnitMoles()
12351245
{
1236-
if (Unit == AmountOfSubstanceUnit.Mole) { return _value; }
1246+
if (Unit == AmountOfSubstanceUnit.Mole) { return _value; }
12371247

12381248
switch (Unit)
12391249
{
@@ -1253,10 +1263,15 @@ private double AsBaseUnitMoles()
12531263
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
12541264
default:
12551265
throw new NotImplementedException("Unit not implemented: " + Unit);
1256-
}
1257-
}
1266+
}
1267+
}
12581268

1259-
/// <summary>Convenience method for working with internal numeric type.</summary>
1269+
/// <summary>Convenience method for working with internal numeric type.</summary>
12601270
private double AsBaseNumericType(AmountOfSubstanceUnit unit) => Convert.ToDouble(As(unit));
1261-
}
1271+
1272+
/// <summary>
1273+
/// The <see cref="BaseDimensions" /> of this quantity.
1274+
/// </summary>
1275+
public BaseDimensions Dimensions => AmountOfSubstance.BaseDimensions;
1276+
}
12621277
}

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public bool Equals(AmplitudeRatio other, AmplitudeRatio maxError)
516516

517517
public override int GetHashCode()
518518
{
519-
return new { Value, Unit }.GetHashCode();
519+
return new { Value, Unit }.GetHashCode();
520520
}
521521

522522
#endregion
@@ -901,7 +901,7 @@ public string ToString(
901901
/// <returns>The value in the base unit representation.</returns>
902902
private double AsBaseUnitDecibelVolts()
903903
{
904-
if (Unit == AmplitudeRatioUnit.DecibelVolt) { return _value; }
904+
if (Unit == AmplitudeRatioUnit.DecibelVolt) { return _value; }
905905

906906
switch (Unit)
907907
{
@@ -911,10 +911,11 @@ private double AsBaseUnitDecibelVolts()
911911
case AmplitudeRatioUnit.DecibelVolt: return _value;
912912
default:
913913
throw new NotImplementedException("Unit not implemented: " + Unit);
914-
}
915-
}
914+
}
915+
}
916916

917-
/// <summary>Convenience method for working with internal numeric type.</summary>
917+
/// <summary>Convenience method for working with internal numeric type.</summary>
918918
private double AsBaseNumericType(AmplitudeRatioUnit unit) => Convert.ToDouble(As(unit));
919-
}
919+
920+
}
920921
}

UnitsNet/GeneratedCode/Quantities/Angle.g.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ public bool Equals(Angle other, Angle maxError)
838838

839839
public override int GetHashCode()
840840
{
841-
return new { Value, Unit }.GetHashCode();
841+
return new { Value, Unit }.GetHashCode();
842842
}
843843

844844
#endregion
@@ -1233,7 +1233,7 @@ public string ToString(
12331233
/// <returns>The value in the base unit representation.</returns>
12341234
private double AsBaseUnitDegrees()
12351235
{
1236-
if (Unit == AngleUnit.Degree) { return _value; }
1236+
if (Unit == AngleUnit.Degree) { return _value; }
12371237

12381238
switch (Unit)
12391239
{
@@ -1253,10 +1253,11 @@ private double AsBaseUnitDegrees()
12531253
case AngleUnit.Revolution: return _value*360;
12541254
default:
12551255
throw new NotImplementedException("Unit not implemented: " + Unit);
1256-
}
1257-
}
1256+
}
1257+
}
12581258

1259-
/// <summary>Convenience method for working with internal numeric type.</summary>
1259+
/// <summary>Convenience method for working with internal numeric type.</summary>
12601260
private double AsBaseNumericType(AngleUnit unit) => Convert.ToDouble(As(unit));
1261-
}
1261+
1262+
}
12621263
}

0 commit comments

Comments
 (0)