@@ -9,24 +9,27 @@ namespace UnitsNet
9
9
/// <summary>
10
10
/// A collection of <see cref="QuantityInfo"/>.
11
11
/// </summary>
12
- public class QuantityInfoLookup
12
+ /// <remarks>
13
+ /// Access type is <c>internal</c> until this class is matured and ready for external use.
14
+ /// </remarks>
15
+ internal class QuantityInfoLookup
13
16
{
14
- private readonly Lazy < QuantityInfo [ ] > InfosLazy ;
15
- private readonly Lazy < Dictionary < ( Type , string ) , UnitInfo > > UnitTypeAndNameToUnitInfoLazy ;
17
+ private readonly Lazy < QuantityInfo [ ] > _infosLazy ;
18
+ private readonly Lazy < Dictionary < ( Type , string ) , UnitInfo > > _unitTypeAndNameToUnitInfoLazy ;
16
19
17
20
/// <summary>
18
21
/// New instance.
19
22
/// </summary>
20
- public QuantityInfoLookup ( )
23
+ /// <param name="quantityInfos"></param>
24
+ public QuantityInfoLookup ( ICollection < QuantityInfo > quantityInfos )
21
25
{
22
- ICollection < QuantityInfo > quantityInfos = Quantity . ByName . Values ;
23
26
Names = quantityInfos . Select ( qt => qt . Name ) . ToArray ( ) ;
24
27
25
- InfosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => quantityInfos
28
+ _infosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => quantityInfos
26
29
. OrderBy ( quantityInfo => quantityInfo . Name )
27
30
. ToArray ( ) ) ;
28
31
29
- UnitTypeAndNameToUnitInfoLazy = new Lazy < Dictionary < ( Type , string ) , UnitInfo > > ( ( ) =>
32
+ _unitTypeAndNameToUnitInfoLazy = new Lazy < Dictionary < ( Type , string ) , UnitInfo > > ( ( ) =>
30
33
{
31
34
return Infos
32
35
. SelectMany ( quantityInfo => quantityInfo . UnitInfos
@@ -45,18 +48,18 @@ public QuantityInfoLookup()
45
48
/// <summary>
46
49
/// All quantity information objects, such as <see cref="Length.Info"/> and <see cref="Mass.Info"/>.
47
50
/// </summary>
48
- public QuantityInfo [ ] Infos => InfosLazy . Value ;
51
+ public QuantityInfo [ ] Infos => _infosLazy . Value ;
49
52
50
53
/// <summary>
51
54
/// Get <see cref="UnitInfo"/> for a given unit enum value.
52
55
/// </summary>
53
- public UnitInfo GetUnitInfo ( Enum unitEnum ) => UnitTypeAndNameToUnitInfoLazy . Value [ ( unitEnum . GetType ( ) , unitEnum . ToString ( ) ) ] ;
56
+ public UnitInfo GetUnitInfo ( Enum unitEnum ) => _unitTypeAndNameToUnitInfoLazy . Value [ ( unitEnum . GetType ( ) , unitEnum . ToString ( ) ) ] ;
54
57
55
58
/// <summary>
56
59
/// Try to get <see cref="UnitInfo"/> for a given unit enum value.
57
60
/// </summary>
58
61
public bool TryGetUnitInfo ( Enum unitEnum , [ NotNullWhen ( true ) ] out UnitInfo ? unitInfo ) =>
59
- UnitTypeAndNameToUnitInfoLazy . Value . TryGetValue ( ( unitEnum . GetType ( ) , unitEnum . ToString ( ) ) , out unitInfo ) ;
62
+ _unitTypeAndNameToUnitInfoLazy . Value . TryGetValue ( ( unitEnum . GetType ( ) , unitEnum . ToString ( ) ) , out unitInfo ) ;
60
63
61
64
/// <summary>
62
65
///
@@ -65,7 +68,7 @@ public bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInfo? unit
65
68
/// <param name="unitInfo"></param>
66
69
public void AddUnitInfo ( Enum unit , UnitInfo unitInfo )
67
70
{
68
- UnitTypeAndNameToUnitInfoLazy . Value . Add ( ( unit . GetType ( ) , unit . ToString ( ) ) , unitInfo ) ;
71
+ _unitTypeAndNameToUnitInfoLazy . Value . Add ( ( unit . GetType ( ) , unit . ToString ( ) ) , unitInfo ) ;
69
72
}
70
73
71
74
/// <summary>
@@ -77,6 +80,7 @@ public void AddUnitInfo(Enum unit, UnitInfo unitInfo)
77
80
/// <exception cref="ArgumentException">Unit value is not a know unit enum type.</exception>
78
81
public IQuantity From ( QuantityValue value , Enum unit )
79
82
{
83
+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
80
84
return Quantity . TryFrom ( value , unit , out IQuantity ? quantity )
81
85
? quantity
82
86
: throw new UnitNotFoundException ( $ "Unit value { unit } of type { unit . GetType ( ) } is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a custom enum type defined outside the UnitsNet library?") ;
@@ -93,6 +97,7 @@ public bool TryFrom(double value, Enum unit, [NotNullWhen(true)] out IQuantity?
93
97
return false ;
94
98
}
95
99
100
+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
96
101
return Quantity . TryFrom ( ( QuantityValue ) value , unit , out quantity ) ;
97
102
}
98
103
@@ -112,23 +117,27 @@ public IQuantity Parse(IFormatProvider? formatProvider, Type quantityType, strin
112
117
if ( ! typeof ( IQuantity ) . IsAssignableFrom ( quantityType ) )
113
118
throw new ArgumentException ( $ "Type { quantityType } must be of type UnitsNet.IQuantity.") ;
114
119
120
+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
115
121
if ( Quantity . TryParse ( formatProvider , quantityType , quantityString , out IQuantity ? quantity ) )
116
122
return quantity ;
117
123
118
124
throw new UnitNotFoundException ( $ "Quantity string '{ quantityString } ' could not be parsed to quantity '{ quantityType } '.") ;
119
125
}
120
126
121
127
/// <inheritdoc cref="Quantity.TryParse(IFormatProvider,System.Type,string,out UnitsNet.IQuantity)"/>
122
- public bool TryParse ( Type quantityType , string quantityString , [ NotNullWhen ( true ) ] out IQuantity ? quantity ) =>
123
- Quantity . TryParse ( null , quantityType , quantityString , out quantity ) ;
128
+ public bool TryParse ( Type quantityType , string quantityString , [ NotNullWhen ( true ) ] out IQuantity ? quantity )
129
+ {
130
+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
131
+ return Quantity . TryParse ( null , quantityType , quantityString , out quantity ) ;
132
+ }
124
133
125
134
/// <summary>
126
135
/// Get a list of quantities that has the given base dimensions.
127
136
/// </summary>
128
137
/// <param name="baseDimensions">The base dimensions to match.</param>
129
138
public IEnumerable < QuantityInfo > GetQuantitiesWithBaseDimensions ( BaseDimensions baseDimensions )
130
139
{
131
- return InfosLazy . Value . Where ( info => info . BaseDimensions . Equals ( baseDimensions ) ) ;
140
+ return _infosLazy . Value . Where ( info => info . BaseDimensions . Equals ( baseDimensions ) ) ;
132
141
}
133
142
}
134
143
}
0 commit comments