Skip to content

Commit a110555

Browse files
committed
Adding BaseUnits class. Adding SI unit system.
1 parent c5c6e51 commit a110555

File tree

5 files changed

+307
-0
lines changed

5 files changed

+307
-0
lines changed

UnitsNet.Tests/BaseUnitsTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 UnitsNet.Units;
23+
using Xunit;
24+
25+
namespace UnitsNet.Tests
26+
{
27+
public class BaseUnitsTests
28+
{
29+
[Fact]
30+
public void Constructor()
31+
{
32+
var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
33+
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela);
34+
35+
Assert.Equal(LengthUnit.Meter, baseUnits.Length);
36+
Assert.Equal(MassUnit.Kilogram, baseUnits.Mass);
37+
Assert.Equal(DurationUnit.Second, baseUnits.Time);
38+
Assert.Equal(ElectricCurrentUnit.Ampere, baseUnits.Current);
39+
Assert.Equal(TemperatureUnit.Kelvin, baseUnits.Temperature);
40+
Assert.Equal(AmountOfSubstanceUnit.Mole, baseUnits.Amount);
41+
Assert.Equal(LuminousIntensityUnit.Candela, baseUnits.LuminousIntensity);
42+
}
43+
}
44+
}

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 UnitsNet.Units;
23+
using Xunit;
24+
25+
namespace UnitsNet.Tests
26+
{
27+
public class UnitSystemTests
28+
{
29+
[Fact]
30+
public void Constructor()
31+
{
32+
var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
33+
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela);
34+
35+
var unitSystem = new UnitSystem(siBaseUnits);
36+
37+
Assert.Equal(unitSystem.BaseUnits, siBaseUnits);
38+
}
39+
40+
[Fact]
41+
public void SIUnitSystemHasCorrectBaseUnits()
42+
{
43+
var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
44+
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela);
45+
46+
Assert.Equal(LengthUnit.Meter, UnitSystem.SI.BaseUnits.Length);
47+
Assert.Equal(MassUnit.Kilogram, UnitSystem.SI.BaseUnits.Mass);
48+
Assert.Equal(DurationUnit.Second, UnitSystem.SI.BaseUnits.Time);
49+
Assert.Equal(ElectricCurrentUnit.Ampere, UnitSystem.SI.BaseUnits.Current);
50+
Assert.Equal(TemperatureUnit.Kelvin, UnitSystem.SI.BaseUnits.Temperature);
51+
Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount);
52+
Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity);
53+
}
54+
}
55+
}

UnitsNet/BaseDimensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public BaseDimensions Divide(BaseDimensions right)
107107
}
108108

109109
#if !WINDOWS_UWP
110+
110111
/// <summary>
111112
/// Check if two dimensions are equal.
112113
/// </summary>
@@ -150,6 +151,7 @@ public BaseDimensions Divide(BaseDimensions right)
150151
{
151152
return left.Divide(right);
152153
}
154+
153155
#endif
154156

155157
/// <inheritdoc />

UnitsNet/BaseUnits.cs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}

UnitsNet/UnitSystem.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
public class UnitSystem
29+
{
30+
public UnitSystem(BaseUnits baseUnits)
31+
{
32+
BaseUnits = baseUnits;
33+
}
34+
35+
public BaseUnits BaseUnits { get; }
36+
37+
private static readonly BaseUnits SIBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
38+
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela);
39+
40+
/// <summary>
41+
/// Gets the SI unit system.
42+
/// </summary>
43+
public static UnitSystem SI { get; } = new UnitSystem(SIBaseUnits);
44+
}
45+
}

0 commit comments

Comments
 (0)