@@ -9,24 +9,27 @@ namespace UnitsNet
99 /// <summary>
1010 /// A collection of <see cref="QuantityInfo"/>.
1111 /// </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
1316 {
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 ;
1619
1720 /// <summary>
1821 /// New instance.
1922 /// </summary>
20- public QuantityInfoLookup ( )
23+ /// <param name="quantityInfos"></param>
24+ public QuantityInfoLookup ( ICollection < QuantityInfo > quantityInfos )
2125 {
22- ICollection < QuantityInfo > quantityInfos = Quantity . ByName . Values ;
2326 Names = quantityInfos . Select ( qt => qt . Name ) . ToArray ( ) ;
2427
25- InfosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => quantityInfos
28+ _infosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => quantityInfos
2629 . OrderBy ( quantityInfo => quantityInfo . Name )
2730 . ToArray ( ) ) ;
2831
29- UnitTypeAndNameToUnitInfoLazy = new Lazy < Dictionary < ( Type , string ) , UnitInfo > > ( ( ) =>
32+ _unitTypeAndNameToUnitInfoLazy = new Lazy < Dictionary < ( Type , string ) , UnitInfo > > ( ( ) =>
3033 {
3134 return Infos
3235 . SelectMany ( quantityInfo => quantityInfo . UnitInfos
@@ -45,18 +48,18 @@ public QuantityInfoLookup()
4548 /// <summary>
4649 /// All quantity information objects, such as <see cref="Length.Info"/> and <see cref="Mass.Info"/>.
4750 /// </summary>
48- public QuantityInfo [ ] Infos => InfosLazy . Value ;
51+ public QuantityInfo [ ] Infos => _infosLazy . Value ;
4952
5053 /// <summary>
5154 /// Get <see cref="UnitInfo"/> for a given unit enum value.
5255 /// </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 ( ) ) ] ;
5457
5558 /// <summary>
5659 /// Try to get <see cref="UnitInfo"/> for a given unit enum value.
5760 /// </summary>
5861 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 ) ;
6063
6164 /// <summary>
6265 ///
@@ -65,7 +68,7 @@ public bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInfo? unit
6568 /// <param name="unitInfo"></param>
6669 public void AddUnitInfo ( Enum unit , UnitInfo unitInfo )
6770 {
68- UnitTypeAndNameToUnitInfoLazy . Value . Add ( ( unit . GetType ( ) , unit . ToString ( ) ) , unitInfo ) ;
71+ _unitTypeAndNameToUnitInfoLazy . Value . Add ( ( unit . GetType ( ) , unit . ToString ( ) ) , unitInfo ) ;
6972 }
7073
7174 /// <summary>
@@ -77,6 +80,7 @@ public void AddUnitInfo(Enum unit, UnitInfo unitInfo)
7780 /// <exception cref="ArgumentException">Unit value is not a know unit enum type.</exception>
7881 public IQuantity From ( QuantityValue value , Enum unit )
7982 {
83+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
8084 return Quantity . TryFrom ( value , unit , out IQuantity ? quantity )
8185 ? quantity
8286 : 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?
9397 return false ;
9498 }
9599
100+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
96101 return Quantity . TryFrom ( ( QuantityValue ) value , unit , out quantity ) ;
97102 }
98103
@@ -112,23 +117,27 @@ public IQuantity Parse(IFormatProvider? formatProvider, Type quantityType, strin
112117 if ( ! typeof ( IQuantity ) . IsAssignableFrom ( quantityType ) )
113118 throw new ArgumentException ( $ "Type { quantityType } must be of type UnitsNet.IQuantity.") ;
114119
120+ // TODO Support custom units, currently only hardcoded built-in quantities are supported.
115121 if ( Quantity . TryParse ( formatProvider , quantityType , quantityString , out IQuantity ? quantity ) )
116122 return quantity ;
117123
118124 throw new UnitNotFoundException ( $ "Quantity string '{ quantityString } ' could not be parsed to quantity '{ quantityType } '.") ;
119125 }
120126
121127 /// <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+ }
124133
125134 /// <summary>
126135 /// Get a list of quantities that has the given base dimensions.
127136 /// </summary>
128137 /// <param name="baseDimensions">The base dimensions to match.</param>
129138 public IEnumerable < QuantityInfo > GetQuantitiesWithBaseDimensions ( BaseDimensions baseDimensions )
130139 {
131- return InfosLazy . Value . Where ( info => info . BaseDimensions . Equals ( baseDimensions ) ) ;
140+ return _infosLazy . Value . Where ( info => info . BaseDimensions . Equals ( baseDimensions ) ) ;
132141 }
133142 }
134143}
0 commit comments