@@ -31,14 +31,9 @@ namespace UnitsNet
31
31
{
32
32
internal class UnitValueAbbreviationLookup
33
33
{
34
- private UnitToAbbreviationMap unitToAbbreviationMap = new UnitToAbbreviationMap ( ) ;
35
- private AbbreviationToUnitMap abbreviationToUnitMap = new AbbreviationToUnitMap ( ) ;
36
-
37
- internal UnitValueAbbreviationLookup ( )
38
- {
39
- unitToAbbreviationMap = new UnitToAbbreviationMap ( ) ;
40
- abbreviationToUnitMap = new AbbreviationToUnitMap ( ) ;
41
- }
34
+ private readonly UnitToAbbreviationMap unitToAbbreviationMap = new UnitToAbbreviationMap ( ) ;
35
+ private readonly AbbreviationToUnitMap abbreviationToUnitMap = new AbbreviationToUnitMap ( ) ;
36
+ private readonly AbbreviationToUnitMap lowerCaseAbbreviationToUnitMap = new AbbreviationToUnitMap ( ) ;
42
37
43
38
internal string [ ] GetAllUnitAbbreviationsForQuantity ( )
44
39
{
@@ -61,32 +56,43 @@ internal List<string> GetAbbreviationsForUnit(int unit)
61
56
return abbreviations . Distinct ( ) . ToList ( ) ;
62
57
}
63
58
64
- internal List < int > GetUnitsForAbbreviation ( string abbreviation )
59
+ internal List < int > GetUnitsForAbbreviation ( string abbreviation , bool ignoreCase )
65
60
{
66
61
var lowerCaseAbbreviation = abbreviation . ToLower ( ) ;
67
- if ( ! abbreviationToUnitMap . TryGetValue ( lowerCaseAbbreviation , out var units ) )
68
- abbreviationToUnitMap [ lowerCaseAbbreviation ] = units = new List < int > ( ) ;
62
+ var key = ignoreCase ? lowerCaseAbbreviation : abbreviation ;
63
+ var map = ignoreCase ? lowerCaseAbbreviationToUnitMap : abbreviationToUnitMap ;
64
+
65
+ if ( ! map . TryGetValue ( key , out List < int > units ) )
66
+ map [ key ] = units = new List < int > ( ) ;
69
67
70
68
return units . Distinct ( ) . ToList ( ) ;
71
69
}
72
70
73
71
internal void Add ( int unit , string abbreviation , bool setAsDefault = false )
74
72
{
75
73
var lowerCaseAbbreviation = abbreviation . ToLower ( ) ;
74
+
76
75
if ( ! unitToAbbreviationMap . TryGetValue ( unit , out var abbreviationsForUnit ) )
77
76
abbreviationsForUnit = unitToAbbreviationMap [ unit ] = new List < string > ( ) ;
78
77
79
- if ( ! abbreviationToUnitMap . TryGetValue ( lowerCaseAbbreviation , out var unitsForAbbreviation ) )
80
- abbreviationToUnitMap [ lowerCaseAbbreviation ] = unitsForAbbreviation = new List < int > ( ) ;
78
+ if ( ! abbreviationToUnitMap . TryGetValue ( abbreviation , out var unitsForAbbreviation ) )
79
+ abbreviationToUnitMap [ abbreviation ] = unitsForAbbreviation = new List < int > ( ) ;
80
+
81
+ if ( ! lowerCaseAbbreviationToUnitMap . TryGetValue ( lowerCaseAbbreviation , out var unitsForLowerCaseAbbreviation ) )
82
+ lowerCaseAbbreviationToUnitMap [ lowerCaseAbbreviation ] = unitsForLowerCaseAbbreviation = new List < int > ( ) ;
83
+
84
+ unitsForLowerCaseAbbreviation . Remove ( unit ) ;
85
+ unitsForLowerCaseAbbreviation . Add ( unit ) ;
81
86
82
- abbreviationsForUnit . Remove ( abbreviation ) ;
83
87
unitsForAbbreviation . Remove ( unit ) ;
88
+ unitsForAbbreviation . Add ( unit ) ;
84
89
90
+ abbreviationsForUnit . Remove ( abbreviation ) ;
85
91
if ( setAsDefault )
86
92
abbreviationsForUnit . Insert ( 0 , abbreviation ) ;
87
93
else
88
94
abbreviationsForUnit . Add ( abbreviation ) ;
89
- unitsForAbbreviation . Add ( unit ) ;
95
+
90
96
}
91
97
}
92
98
}
0 commit comments