Skip to content

Commit 38a502c

Browse files
tmilnthorpangularsen
authored andcommitted
Creating UnitInfo class to hold info about units. (#602)
* Creating UnitInfo class to hold info about units. Future additions: BaseUnits per-unit, etc. so we don't have to keep adding parallel arrays to QuantityInfo
1 parent 751bf32 commit 38a502c

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

UnitsNet.Tests/UnitInfoTests.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 UnitsNet.Units;
23+
using Xunit;
24+
25+
namespace UnitsNet.Tests
26+
{
27+
public class UnitInfoTests
28+
{
29+
[Fact]
30+
public void ConstructorTest()
31+
{
32+
var unitInfo = new UnitInfo(LengthUnit.Meter);
33+
Assert.Equal(LengthUnit.Meter, unitInfo.Value);
34+
Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name);
35+
}
36+
37+
[Fact]
38+
public void GenericConstructorTest()
39+
{
40+
var unitInfo = new UnitInfo<LengthUnit>(LengthUnit.Meter);
41+
Assert.Equal(LengthUnit.Meter, unitInfo.Value);
42+
Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name);
43+
}
44+
}
45+
}

UnitsNet/QuantityInfo.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public QuantityInfo(QuantityType quantityType, [NotNull] Enum[] units, [NotNull]
6464
Name = quantityType.ToString();
6565
QuantityType = quantityType;
6666
UnitType = UnitEnumTypes.First(t => t.Name == $"{quantityType}Unit");
67-
UnitNames = units.Select(u => u.ToString()).ToArray();
67+
UnitInfos = units.Select(unit => new UnitInfo(unit)).ToArray();
68+
UnitNames = UnitInfos.Select(unitInfo => unitInfo.Name).ToArray();
6869
Units = units;
69-
BaseUnit = baseUnit;
70+
BaseUnitInfo = new UnitInfo(baseUnit);
71+
BaseUnit = BaseUnitInfo.Value;
7072
Zero = zero;
7173
ValueType = zero.GetType();
7274
BaseDimensions = baseDimensions;
@@ -82,20 +84,27 @@ public QuantityInfo(QuantityType quantityType, [NotNull] Enum[] units, [NotNull]
8284
/// </summary>
8385
public QuantityType QuantityType { get; }
8486

87+
public UnitInfo[] UnitInfos { get; }
88+
8589
/// <summary>
8690
/// All unit names for the quantity, such as ["Centimeter", "Decimeter", "Meter", ...].
8791
/// </summary>
92+
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
8893
public string[] UnitNames { get; }
8994

9095
/// <summary>
9196
/// All unit enum values for the quantity, such as [<see cref="LengthUnit.Centimeter" />,
9297
/// <see cref="LengthUnit.Decimeter" />, <see cref="LengthUnit.Meter" />, ...].
9398
/// </summary>
99+
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
94100
public Enum[] Units { get; }
95101

102+
public UnitInfo BaseUnitInfo { get; }
103+
96104
/// <summary>
97105
/// The base unit for the quantity, such as <see cref="LengthUnit.Meter" />.
98106
/// </summary>
107+
[Obsolete("This property is deprecated and will be removed at a future release. Please use the BaseUnitInfo property.")]
99108
public Enum BaseUnit { get; }
100109

101110
/// <summary>
@@ -134,13 +143,24 @@ public QuantityInfo(QuantityType quantityType, TUnit[] units, TUnit baseUnit, IQ
134143
: base(quantityType, units.Cast<Enum>().ToArray(), baseUnit, zero, baseDimensions)
135144
{
136145
Zero = zero;
146+
UnitInfos = units.Select(unit => new UnitInfo<TUnit>(unit)).ToArray();
137147
Units = units;
138-
BaseUnit = baseUnit;
148+
BaseUnitInfo = new UnitInfo<TUnit>(baseUnit);
149+
BaseUnit = BaseUnitInfo.Value;
139150
}
140151

152+
/// <inheritdoc cref="QuantityInfo.UnitInfos" />
153+
public new UnitInfo<TUnit>[] UnitInfos { get; }
154+
141155
/// <inheritdoc cref="QuantityInfo.Units" />
156+
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
142157
public new TUnit[] Units { get; }
143158

159+
/// <inheritdoc cref="QuantityInfo.BaseUnitInfo" />
160+
public new UnitInfo<TUnit> BaseUnitInfo { get; }
161+
162+
/// <inheritdoc cref="QuantityInfo.BaseUnit" />
163+
[Obsolete("This property is deprecated and will be removed at a future release. Please use the BaseUnitInfo property.")]
144164
public new TUnit BaseUnit { get; }
145165

146166
/// <inheritdoc cref="QuantityInfo.Zero" />

UnitsNet/UnitInfo.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 UnitsNet.Units;
24+
25+
namespace UnitsNet
26+
{
27+
/// <summary>
28+
/// Information about the unit, such as its name and value.
29+
/// This is useful to enumerate units and present names with quantities and units
30+
/// chosen dynamically at runtime, such as unit conversion apps or allowing the user to change the
31+
/// unit representation.
32+
/// </summary>
33+
/// <remarks>
34+
/// Typically you obtain this by looking it up via <see cref="QuantityInfo.UnitInfos" />.
35+
/// </remarks>
36+
#if WINDOWS_UWP
37+
internal
38+
#else
39+
public
40+
#endif
41+
class UnitInfo
42+
{
43+
public UnitInfo(Enum value)
44+
{
45+
Value = value;
46+
Name = value.ToString();
47+
}
48+
49+
/// <summary>
50+
/// The enum value of the unit, such as [<see cref="LengthUnit.Centimeter" />,
51+
/// <see cref="LengthUnit.Decimeter" />, <see cref="LengthUnit.Meter" />, ...].
52+
/// </summary>
53+
public Enum Value;
54+
55+
/// <summary>
56+
/// The name of the unit, such as ["Centimeter", "Decimeter", "Meter", ...].
57+
/// </summary>
58+
public string Name { get; }
59+
}
60+
61+
#if !WINDOWS_UWP
62+
/// <inheritdoc cref="UnitInfo" />
63+
/// <remarks>
64+
/// This is a specialization of <see cref="UnitInfo" />, for when the unit type is known.
65+
/// Typically you obtain this by looking it up statically from <see cref="QuantityInfo{LengthUnit}.UnitInfos" /> or
66+
/// or dynamically via <see cref="IQuantity{TUnitType}.QuantityInfo" />.
67+
/// </remarks>
68+
/// <typeparam name="TUnit">The unit enum type, such as <see cref="LengthUnit" />. </typeparam>
69+
public class UnitInfo<TUnit> : UnitInfo
70+
where TUnit : Enum
71+
{
72+
public UnitInfo(TUnit value) : base(value)
73+
{
74+
Value = value;
75+
}
76+
77+
public new TUnit Value { get; }
78+
}
79+
#endif
80+
}

0 commit comments

Comments
 (0)