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>>
A 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).

  • Method Details

    • sectionType

      Class<S> sectionType()
      Get the strongly typed CodeSection 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 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.

      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, String previousText, S section)
      Intercepts an AbstractCodeWriter 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(Class<S> type, BiConsumer<W,S> appender)
      Provides a more concise way of creating anonymous CodeInterceptor.Appenders.

      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(String sectionName, BiConsumer<W,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.