S
- Type of CodeSection to intercept.W
- Type of CodeWriter to expect.public interface CodeInterceptor<S extends CodeSection,W extends AbstractCodeWriter<W>>
CodeSection
interceptor for a specific type of CodeSection
.
These interceptors are executed after a state is popped by
AbstractCodeWriter
. Interceptors have an opportunity to
change the contents of the popped state and are expected to write to
the AbstractCodeWriter
they are given when called.
Unless you need to intercept previously written content and change it,
it's best to implement the CodeInterceptor.Appender
or CodeInterceptor.Prepender
interfaces
since they take care of properly writing previously written content to
the section (for example, only writing if it's non-empty, and using
writeInlineWithNoFormatting to avoid unintentional interpolation).
Modifier and Type | Interface and Description |
---|---|
static interface |
CodeInterceptor.Appender<S extends CodeSection,W extends AbstractCodeWriter<W>>
A code section interceptor that adds text after the intercepted section.
|
static interface |
CodeInterceptor.Prepender<S extends CodeSection,W extends AbstractCodeWriter<W>>
A code section interceptor that adds text before the intercepted section.
|
Modifier and Type | Method and Description |
---|---|
static <S extends CodeSection,W extends AbstractCodeWriter<W>> |
appender(java.lang.Class<S> type,
java.util.function.BiConsumer<W,S> appender)
Provides a more concise way of creating anonymous
CodeInterceptor.Appender s. |
static <W extends AbstractCodeWriter<W>> |
forName(java.lang.String sectionName,
java.util.function.BiConsumer<W,java.lang.String> consumer)
Creates an interceptor that works with any type of CodeSection and is filtered
only by the name of the section.
|
default boolean |
isIntercepted(S section)
Checks if the given section is filtered by this interceptor or not.
|
java.lang.Class<S> |
sectionType()
Get the strongly typed
CodeSection this interceptor is used
to intercept. |
void |
write(W writer,
java.lang.String previousText,
S section)
Intercepts an
AbstractCodeWriter section. |
java.lang.Class<S> sectionType()
CodeSection
this interceptor is used
to intercept.default boolean isIntercepted(S section)
In some cases sectionType()
might allow filtering a wider
array of types than what is actually filtered by an interceptor. The
most common example of this is intercepting any type of CodeSection
and only filtering based on the result of CodeSection.sectionName()
.
This method will return true
by default, meaning that any
type of CodeSection
that is an instance of the class returned
from sectionType()
will be intercepted.
section
- Section to test if this interceptor is relevant.void write(W writer, java.lang.String previousText, S section)
AbstractCodeWriter
section.writer
- Writer used to write content. If no write calls are
made, any intercepted text is lost.previousText
- The previous text that was written. This text
needs to be written again in order for it to
be kept in the section.section
- The strongly typed section value.static <S extends CodeSection,W extends AbstractCodeWriter<W>> CodeInterceptor<S,W> appender(java.lang.Class<S> type, java.util.function.BiConsumer<W,S> appender)
CodeInterceptor.Appender
s.
This method does not support custom filters on matched CodeSections. That functionality must be implemented by directly creating an Appender class.
S
- The type of section being intercepted.W
- The type of writer to use.type
- The type of section to intercept.appender
- A BiConsumer that takes the writer and section and is expected to make write calls.static <W extends AbstractCodeWriter<W>> CodeInterceptor<CodeSection,W> forName(java.lang.String sectionName, java.util.function.BiConsumer<W,java.lang.String> consumer)
W
- The type of code writer being used.sectionName
- The name of the section to intercept.consumer
- A consumer to invoke for intercepted sections that accepts the writer and previous text.