Class Formatter

java.lang.Object
dev.gmitch215.bytebox.text.Formatter
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public final class Formatter extends Object implements Closeable, Flushable
A formatter, standing in for 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 Details

    • Formatter

      public Formatter()
      Writing into a string, in the platform's own locale.
    • Formatter

      public Formatter(Appendable out)
      Parameters:
      out - where to write
    • Formatter

      public Formatter(Locale locale)
      Parameters:
      locale - which locale's separators to use, or null for no localisation
    • Formatter

      public Formatter(Appendable out, Locale locale)
      Parameters:
      out - where to write
      locale - which locale's separators to use, or null for no localisation
  • Method Details

    • out

      public Appendable out()
      Returns where this writes.
      Returns:
      where this writes
    • locale

      public Locale locale()
      Returns the locale, or null when there is none.
      Returns:
      the locale, or null when there is none
    • ioException

      public IOException 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

      public Formatter format(String format, Object... args)
      Writes a formatted string.
      Parameters:
      format - the format string
      args - the arguments
      Returns:
      this formatter
    • format

      public Formatter format(Locale locale, String format, Object... args)
      Writes a formatted string in a given locale.
      Parameters:
      locale - which locale's separators to use, or null for no localisation
      format - the format string
      args - the arguments
      Returns:
      this formatter
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object