- All Known Subinterfaces:
DefaultFormatter
,SpecFormatter
Formats and then sends the results to the downstream appender.
Implementations should be singleton like and should not contain state. By default
native types are passed straight through to the downstream appender. If this is not
desired one can override those methods.
Important: the formatter does not decide what types are allowed at compile time to
be formatted. To control what types are allowed to be formatted see
JStacheFormatterTypes
.
Implementing
An alternative to implementing this complicated interface is to simply make aFunction<@Nullable Object, String>
and call of(Function)
to create a
formatter.
To implement a custom formatter:
- Implement this interface or use
of(Function)
. - Register the custom formatter with
JStacheFormatter
. - Add additional allowed types with
JStacheFormatterTypes
on to the class that is annotated withJStacheFormatter
- Set
JStacheConfig.formatter()
to the class that has theJStacheFormatter
.
null
for
nullable format
calls as the downstream appender do not allow null
. - Author:
- agentgt
- See Also:
- API Note
- Although the formatter has access to the raw
Output
the formatter should never use it directly and simply pass it on to the downstream appender.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Implement to allow formatting of custom objects you want to output. -
Method Summary
Modifier and TypeMethodDescriptiondefault String
Formats an object by usingStringBuilder
and callingformat(Appender, Output, String, Class, Object)
.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.format
(Appender downstream, A a, String path, @Nullable Formatter.Formattable f) Formats the formattable object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.Formats the object and then sends the results to the downstream appender.static Formatter
Adapts a function to a formatter.
-
Method Details
-
apply
Formats an object by usingStringBuilder
and callingformat(Appender, Output, String, Class, Object)
. -
format
<A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, Class<?> c, @Nullable Object o) throws E Formats the object and then sends the results to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathc
- the object class but is not guaranteed to be accurate. If it is not known Object.class will be used.o
- the object which maybe null- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, char c) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathc
- character- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, short s) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like paths
- short- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, int i) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathi
- integer- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, long l) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathl
- long- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, double d) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathd
- double- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, boolean b) throws E Formats the object and then sends the results to the downstream appender. The default implementation passes natives through to the downstream appender.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathb
- boolean- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender.
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, @Nullable String s) throws E Formats the object and then sends the results to the downstream appender. The default implementation callsformat(Appender, Output, String, Class, Object)
and it is generally recommend you override for performance.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like paths
- String value which maybenull
.- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender. Also take note that the string value maybe null!
-
format
default <A extends Output<E>,E extends Exception> void format(Appender downstream, A a, String path, @Nullable Formatter.Formattable f) throws E Formats the formattable object and then sends the results to the downstream appender. The default implementation will call the supplied Formatter.Formattable if it is not null otherwise it will callformat(Appender, Output, String, Class, Object)
to handle the null case.- Type Parameters:
A
- the appendable typeE
- the appender exception type- Parameters:
downstream
- the downstream appender to be used instead of the appendable directlya
- the appendable to be passed to the appenderpath
- the dotted mustache like pathf
- Formattable which maybenull
.- Throws:
E
- if the appender or appendable throws an exception- API Note
- Although the formatter has access to the raw
Appendable
the formatter should never use it directly and simply pass it on to the downstream appender. Also take note that the string value maybe null!
-
of
Adapts a function to a formatter. If the function is already a formatter then it is simply returned (noop). Thus it is safe to repeatedly call this on formatters. If the function is adapted the returned adapted formatter does not pass native types to the inputted function.- Parameters:
formatterFunction
- if it is already an escaper- Returns:
- adapted formattter
-