11package org .rumbledb .runtime .functions .util .formatting .pictures .FormatInteger ;
22
3- import com .ibm .icu .util .ULocale ;
4-
53import org .rumbledb .exceptions .ExceptionMetadata ;
64import org .rumbledb .runtime .functions .util .formatting .NumberWords ;
75import org .rumbledb .runtime .functions .util .formatting .NumericFormattingSupport ;
@@ -58,52 +56,34 @@ public static String format(BigInteger value, String pictureString, String langu
5856 boolean isNegative = value .signum () < 0 ;
5957 BigInteger absValue = value .abs ();
6058
61- // Resolve the locale once and pass it to the handlers.
62- ULocale locale = LanguageSupport . resolveULocale (
63- LanguageSupport . effectiveLanguageOf ( LanguageSupport . normalizeLanguage ( language ) )
59+ FormatIntegerPicture picture = PICTURE_CACHE . computeIfAbsent (
60+ pictureString ,
61+ key -> FormatIntegerPictureParser . parse ( key , metadata )
6462 );
65-
66- FormatIntegerPicture picture = PICTURE_CACHE .get (pictureString );
67- if (picture == null ) {
68- picture = FormatIntegerPictureParser .parse (pictureString , metadata );
69- PICTURE_CACHE .putIfAbsent (pictureString , picture );
70- }
7163 PrimaryFormatToken primary = picture .getPrimaryFormatToken ();
7264 IntegerFormatModifier modifier = picture .getFormatModifier ();
7365
74- String result ;
66+ String result = switch (primary .getType ()) {
67+ case PrimaryFormatToken .DECIMAL -> handleDecimal (absValue , primary , modifier , language );
68+ case PrimaryFormatToken .ALPHABETIC_UPPER , PrimaryFormatToken .ALPHABETIC_LOWER ->
69+ handleAlphabetic (absValue , primary , modifier , language );
70+ case PrimaryFormatToken .ROMAN_UPPER , PrimaryFormatToken .ROMAN_LOWER ->
71+ handleRoman (absValue , primary , modifier , language );
72+ case PrimaryFormatToken .WORDS_LOWER , PrimaryFormatToken .WORDS_UPPER , PrimaryFormatToken .WORDS_TITLE ->
73+ handleWords (absValue , primary , modifier , language );
74+ default -> handleOther (absValue , modifier , language );
75+ };
7576
7677 // Invariant: value => 0
7778
78- switch (primary .getType ()) {
79- case PrimaryFormatToken .DECIMAL :
80- result = handleDecimal (absValue , primary , modifier , locale );
81- break ;
82- case PrimaryFormatToken .ALPHABETIC_UPPER :
83- case PrimaryFormatToken .ALPHABETIC_LOWER :
84- result = handleAlphabetic (absValue , primary , modifier , locale );
85- break ;
86- case PrimaryFormatToken .ROMAN_UPPER :
87- case PrimaryFormatToken .ROMAN_LOWER :
88- result = handleRoman (absValue , primary , modifier , locale );
89- break ;
90- case PrimaryFormatToken .WORDS_LOWER :
91- case PrimaryFormatToken .WORDS_UPPER :
92- case PrimaryFormatToken .WORDS_TITLE :
93- result = handleWords (absValue , primary , modifier , locale );
94- break ;
95- default :
96- result = handleOther (absValue , modifier , locale );
97- }
98-
9979 return !isNegative ? result : ("-" + result );
10080 }
10181
10282 private static String handleDecimal (
10383 BigInteger value ,
10484 PrimaryFormatToken primary ,
10585 IntegerFormatModifier modifier ,
106- ULocale locale
86+ String language
10787 ) {
10888 NumericPicture picture = primary .getNumericPicture ();
10989
@@ -122,7 +102,7 @@ private static String handleDecimal(
122102 // now)
123103
124104 if (IntegerFormatModifier .ORDINAL .equals (modifier .getNumberType ())) {
125- digits = digits + NumberWords .ordinalSuffix (value , locale );
105+ digits = digits + NumberWords .ordinalSuffix (value , LanguageSupport . resolveEffectiveULocale ( language ) );
126106 }
127107
128108 return digits ;
@@ -132,10 +112,10 @@ private static String handleRoman(
132112 BigInteger value ,
133113 PrimaryFormatToken primary ,
134114 IntegerFormatModifier modifier ,
135- ULocale locale
115+ String language
136116 ) {
137117 if (value .signum () == 0 || value .compareTo (BigInteger .valueOf (3999 )) > 0 ) {
138- return handleOther (value , modifier , locale );
118+ return handleOther (value , modifier , language );
139119 }
140120
141121 // For Roman, unsupported ordinal handling is ignored and cardinal numbering is used.
@@ -146,10 +126,10 @@ private static String handleAlphabetic(
146126 BigInteger value ,
147127 PrimaryFormatToken primary ,
148128 IntegerFormatModifier modifier ,
149- ULocale locale
129+ String language
150130 ) {
151131 if (value .signum () == 0 || value .compareTo (BigInteger .valueOf (Integer .MAX_VALUE )) > 0 ) {
152- return handleOther (value , modifier , locale );
132+ return handleOther (value , modifier , language );
153133 }
154134
155135 String result = NumericFormattingSupport .integerToAlphabetic (
@@ -165,18 +145,26 @@ private static String handleWords(
165145 BigInteger value ,
166146 PrimaryFormatToken primary ,
167147 IntegerFormatModifier modifier ,
168- ULocale locale
148+ String language
169149 ) {
170150 if (value .compareTo (BigInteger .valueOf (Integer .MAX_VALUE )) > 0 ) {
171- return handleOther (value , modifier , locale );
151+ return handleOther (value , modifier , language );
172152 }
173153
174154 String result ;
175155
176156 if (IntegerFormatModifier .ORDINAL .equals (modifier .getNumberType ())) {
177- result = NumberWords .ordinalWords (value .longValueExact (), locale , modifier .getFormatSpecifier ());
157+ result = NumberWords .ordinalWords (
158+ value .longValueExact (),
159+ LanguageSupport .resolveEffectiveULocale (language ),
160+ modifier .getFormatSpecifier ()
161+ );
178162 } else {
179- result = NumberWords .cardinal (value .longValueExact (), locale , modifier .getFormatSpecifier ());
163+ result = NumberWords .cardinal (
164+ value .longValueExact (),
165+ LanguageSupport .resolveEffectiveULocale (language ),
166+ modifier .getFormatSpecifier ()
167+ );
180168 }
181169
182170 if (primary .getType ().equals (PrimaryFormatToken .WORDS_LOWER )) {
@@ -190,12 +178,12 @@ private static String handleWords(
190178 private static String handleOther (
191179 BigInteger value ,
192180 IntegerFormatModifier modifier ,
193- ULocale locale
181+ String language
194182 ) {
195183 String result = NumericFormattingSupport .toDecimalString (value );
196184
197185 if (IntegerFormatModifier .ORDINAL .equals (modifier .getNumberType ())) {
198- result = result + NumberWords .ordinalSuffix (value , locale );
186+ result = result + NumberWords .ordinalSuffix (value , LanguageSupport . resolveEffectiveULocale ( language ) );
199187 }
200188
201189 return result ;
0 commit comments