Interface FmtFormattable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
In {fmt}, the library picks formatter<T> by static type and calls parse() on the
part after ':' inside a replacement field, then format() to write the value. Here
there is no separate parse step exposed: the substring after ':' (or null / empty if
omitted) is passed as spec, and the implementation decides how to interpret it—often
mirroring what parse() would have accepted.
When a value implements this interface, Fmt delegates to appendFormatted(java.lang.Appendable, java.util.Locale, java.lang.String)
instead of String.valueOf(java.lang.Object) / String.format(java.lang.String, java.lang.Object...). For an empty or null
spec, use your type’s default textual form; for a non-empty spec, you may delegate
to String.format(locale, "%" + spec, ...) or apply custom rules.
Example (sketch of a “proxy string” like StringProxy):
public final class StringProxy implements FmtFormattable {
private final CharSequence data;
public StringProxy(CharSequence data) { this.data = data; }
@Override
public void appendFormatted(Appendable out, Locale locale, String spec) throws IOException {
String s = data.toString();
if (spec == null || spec.isEmpty()) {
out.append(s);
} else {
out.append(String.format(locale, "%" + spec, s));
}
}
}
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendFormatted(Appendable out, Locale locale, String spec) Writes this value tooutusing the given locale and format spec.
-
Method Details
-
appendFormatted
Writes this value tooutusing the given locale and format spec.- Parameters:
out- destination (same idea as {fmt}’sformat_context::out())locale- locale for locale-sensitive formattingspec- text after':'in a field like{name:spec};nullor empty if the field has no colon part (e.g.{}or{x})- Throws:
IOException
-