Skip to content

Commit a140681

Browse files
committed
perf: UnitsOfTimeFormatter instead of properties files
1 parent 9e20601 commit a140681

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

common/src/main/java/io/github/jy95/fds/common/functions/UnitsOfTimeFormatter.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.jy95.fds.common.functions;
22

33
import com.ibm.icu.number.NumberFormatter;
4+
import com.ibm.icu.number.Precision;
45
import com.ibm.icu.util.MeasureUnit;
56

67
import java.util.Locale;
@@ -49,11 +50,14 @@ public static String formatWithCount(Locale locale, String unit, Number count) {
4950
* @return A formatted time-unit string without a count
5051
*/
5152
public static String formatWithoutCount(Locale locale, String unit, Number count) {
52-
String formattedWithCount = formatWithCount(locale, unit, count);
53-
// Remove the numeric portion and return only the unit text
54-
// ICU4J allows us to format with unit, but we use only the textual part in the final output.
55-
int indexOfSpace = formattedWithCount.indexOf(" ");
56-
57-
return formattedWithCount.substring(indexOfSpace + 1);
53+
// ICU4j doesn't have a method for extracting only the unit with plural form so a bit of magic here
54+
return NumberFormatter
55+
.withLocale(locale)
56+
.precision(Precision.maxSignificantDigits(1))
57+
.unit(UNIT_MAPPING.get(unit))
58+
.unitWidth(NumberFormatter.UnitWidth.FULL_NAME)
59+
.format(count)
60+
.toString()
61+
.substring(2); // 1 because of the number display + 1 because of spacing
5862
}
5963
}

0 commit comments

Comments
 (0)