Formattable Custom Width, Flags, And Precision

Implement java.util.Formattable so a custom type honors alternate #, precision .N, left-justify -, width, and uppercase %S flags in String.format.

JavaCli Tooling

Code

snippet
import java.util.Formattable;
import java.util.FormattableFlags;
import java.util.Formatter;
import java.util.Locale;

public class DigestFormatting {

    record DigestRef(String hex) implements Formattable {
        @Override
        public void formatTo(Formatter formatter, int flags, int width, int precision) {
            boolean alternate = (flags & FormattableFlags.ALTERNATE) != 0;
            boolean uppercase = (flags & FormattableFlags.UPPERCASE) != 0;
            boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) != 0;

            String value = alternate ? "0x" + hex : hex;
            if (precision >= 0 && precision < value.length()) {
                value = value.substring(0, precision);
            }
            if (uppercase) {
                Locale locale = formatter.locale() != null ? formatter.locale() : Locale.ROOT;
                value = value.toUpperCase(locale);
            }

            StringBuilder out = new StringBuilder(value);
            int pad = width - out.length();
            while (pad-- > 0) {
                if (leftJustify) {
                    out.append(' ');
                } else {
                    out.insert(0, ' ');
                }
            }
            formatter.format("%s", out.toString());
        }
    }

    public static void main(String[] args) {
        DigestRef ref = new DigestRef("a3f9c012e4");

        System.out.println("default:     [" + String.format("%s", ref) + "]");
        System.out.println("alternate #: [" + String.format("%#s", ref) + "]");
        System.out.println("truncate .6: [" + String.format("%#.6s", ref) + "]");
        System.out.println("upper %S:    [" + String.format("%#10.6S", ref) + "]");
        System.out.println("left %-10s: [" + String.format("%-#10.6s", ref) + "]");
    }
}

Mechanics trace — Formattable Custom Width, Flags, And Precision

Structurally, expect the classes DigestFormatting and DigestRef and the functions formatTo and main and nothing else.

Repetition is confined to while (pad-- > 0) — the rest of the file is straight-line.

Caveats

  • Pure standard library code — paste straight into your project and run without setup.

Putting it to use

Entry signature: public void formatTo(Formatter formatter, int flags, int width, int precision)

The run prints default: [, alternate #: [, truncate .6: [ — each prefix names the step that produced it.

Core lines

Locale locale = formatter.locale() != null ? formatter.locale() : Locale.ROOT;
boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) != 0;
boolean alternate = (flags & FormattableFlags.ALTERNATE) != 0;

Interactive Tools for This Topic

FAQ

What does Formattable Custom Width, Flags, And Precision print when run?
Invoke the file as-is — the `default: [`, `alternate #: [`, `truncate .6: [` prefixes identify which step printed each line.