Interface CodeInterceptor<S extends CodeSection,W extends AbstractCodeWriter<W>>
-
- Type Parameters:
S
- Type of CodeSection to intercept.W
- Type of CodeWriter to expect.
- All Known Subinterfaces:
CodeInterceptor.Appender<S,W>
,CodeInterceptor.Prepender<S,W>
public interface CodeInterceptor<S extends CodeSection,W extends AbstractCodeWriter<W>>
ACodeSection
interceptor for a specific type ofCodeSection
.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 theAbstractCodeWriter
they are given when called.Unless you need to intercept previously written content and change it, it's best to implement the
CodeInterceptor.Appender
orCodeInterceptor.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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface 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.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <S extends CodeSection,W extends AbstractCodeWriter<W>>
CodeInterceptor<S,W>appender(java.lang.Class<S> type, java.util.function.BiConsumer<W,S> appender)
Provides a more concise way of creating anonymousCodeInterceptor.Appender
s.static <W extends AbstractCodeWriter<W>>
CodeInterceptor<CodeSection,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 typedCodeSection
this interceptor is used to intercept.void
write(W writer, java.lang.String previousText, S section)
Intercepts anAbstractCodeWriter
section.
-
-
-
Method Detail
-
sectionType
java.lang.Class<S> sectionType()
Get the strongly typedCodeSection
this interceptor is used to intercept.- Returns:
- The code section to intercept.
-
isIntercepted
default boolean isIntercepted(S section)
Checks if the given section is filtered by this interceptor or not.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 ofCodeSection.sectionName()
.This method will return
true
by default, meaning that any type ofCodeSection
that is an instance of the class returned fromsectionType()
will be intercepted.- Parameters:
section
- Section to test if this interceptor is relevant.- Returns:
- Returns true if the section is intercepted or not.
-
write
void write(W writer, java.lang.String previousText, S section)
Intercepts anAbstractCodeWriter
section.- Parameters:
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.
-
appender
static <S extends CodeSection,W extends AbstractCodeWriter<W>> CodeInterceptor<S,W> appender(java.lang.Class<S> type, java.util.function.BiConsumer<W,S> appender)
Provides a more concise way of creating anonymousCodeInterceptor.Appender
s.This method does not support custom filters on matched CodeSections. That functionality must be implemented by directly creating an Appender class.
- Type Parameters:
S
- The type of section being intercepted.W
- The type of writer to use.- Parameters:
type
- The type of section to intercept.appender
- A BiConsumer that takes the writer and section and is expected to make write calls.- Returns:
- Returns the created Appender.
-
forName
static <W extends AbstractCodeWriter<W>> CodeInterceptor<CodeSection,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.- Type Parameters:
W
- The type of code writer being used.- Parameters:
sectionName
- The name of the section to intercept.consumer
- A consumer to invoke for intercepted sections that accepts the writer and previous text.- Returns:
- Returns the created interceptor.
-
-