Class Formatter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
java.util.Formatter.
The compiler substitutes java.util.Formatter for this one, which is what
String.format and printf go through, so a project using either works unchanged.
What it replaces is a version that reaches a decimal formatter, a currency table and the locale
data behind them from the single method that dispatches on the conversion character. Because that
method is one method, every conversion is reachable from every use, and a format string of
"%s and %d" costs the whole graph: measured at 85 KB of WebAssembly after compression,
against a hello world of 7 KB. The digits are worked out here instead, by Decimal, and
Locale - which is 0.9 KB by itself - is all that is left of the rest.
Two conversions are not here, and both would put back what this class exists to remove.
%t and %T would make the date and calendar graph reachable from every
String.format call; use DateTimeFormatter instead. %a and %A write a
double in hexadecimal; use Double.toHexString. Each throws and names its replacement.
Grouping is inserted every three digits, which is right for the locales that group in threes and wrong for the ones that do not. The decimal point and the group separator themselves come from the platform's locale data.
- Since:
- 1.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionWriting into a string, in the platform's own locale.Formatter(Appendable out) Formatter(Appendable out, Locale locale) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()Writes a formatted string.Writes a formatted string in a given locale.Returns the failure the destination reported, or null when it has not failed.locale()Returns the locale, or null when there is none.out()Returns where this writes.toString()
-
Constructor Details
-
Formatter
public Formatter()Writing into a string, in the platform's own locale. -
Formatter
- Parameters:
out- where to write
-
Formatter
- Parameters:
locale- which locale's separators to use, or null for no localisation
-
Formatter
- Parameters:
out- where to writelocale- which locale's separators to use, or null for no localisation
-
-
Method Details
-
out
Returns where this writes.- Returns:
- where this writes
-
locale
Returns the locale, or null when there is none.- Returns:
- the locale, or null when there is none
-
ioException
Returns the failure the destination reported, or null when it has not failed.- Returns:
- the failure the destination reported, or null when it has not failed
-
format
Writes a formatted string.- Parameters:
format- the format stringargs- the arguments- Returns:
- this formatter
-
format
Writes a formatted string in a given locale.- Parameters:
locale- which locale's separators to use, or null for no localisationformat- the format stringargs- the arguments- Returns:
- this formatter
-
flush
public void flush() -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
-