Interface FormatterStep

All Superinterfaces:
AutoCloseable, Serializable
All Known Implementing Classes:
SerializeToByteArrayHack

public interface FormatterStep extends Serializable, AutoCloseable
An implementation of this class specifies a single step in a formatting process.

The input is guaranteed to have unix-style newlines, and the output is required to not introduce any windows-style newlines as well.

  • Method Details

    • getName

      String getName()
      The name of the step, for debugging purposes.
    • format

      @Nullable String format(String rawUnix, File file) throws Exception
      Returns a formatted version of the given content.
      Parameters:
      rawUnix - the content to format, guaranteed to have unix-style newlines ('\n'); never null
      file - the file which rawUnix was obtained from; never null. Pass the reference Formatter.NO_FILE_SENTINEL if and only if no file is actually associated with rawUnix
      Returns:
      the formatted content, guaranteed to only have unix-style newlines; may return null if the formatter step doesn't have any changes to make
      Throws:
      Exception - if the formatter step experiences a problem
    • lint

      @Nullable default List<Lint> lint(String content, File file) throws Exception
      Returns a list of lints against the given file content
      Parameters:
      content - the content to check
      file - the file which content was obtained from; never null. Pass an empty file using new File("") if and only if no file is actually associated with content
      Returns:
      a list of lints
      Throws:
      Exception - if the formatter step experiences a problem
    • filterByContent

      default FormatterStep filterByContent(OnMatch onMatch, String contentPattern)
      Returns a new FormatterStep which, observing the value of formatIfMatches, will only apply, or not, its changes to files which pass the given filter.
      Parameters:
      onMatch - determines if matches are included or excluded
      contentPattern - java regular expression used to filter in or out files which content contain pattern
      Returns:
      FormatterStep
    • filterByFile

      default FormatterStep filterByFile(SerializableFileFilter filter)
      Returns a new FormatterStep which will only apply its changes to files which pass the given filter.

      The provided filter must be serializable.

    • createLazy

      static <RoundtripState extends Serializable, EqualityState extends Serializable> FormatterStep createLazy(String name, ThrowingEx.Supplier<RoundtripState> roundtripInit, SerializedFunction<RoundtripState,EqualityState> equalityFunc, SerializedFunction<EqualityState,? extends FormatterFunc> formatterFunc)
      Parameters:
      name - The name of the formatter step.
      roundtripInit - If the step has any state, this supplier will calculate it lazily. The supplier doesn't have to be serializable, but the result it calculates needs to be serializable.
      equalityFunc - A pure serializable function (method reference recommended) which takes the result of `roundtripInit`, and returns a serializable object whose serialized representation will be used for `.equals` and `.hashCode` of the FormatterStep.
      formatterFunc - A pure serializable function (method reference recommended) which takes the result of `equalityFunc`, and returns a `FormatterFunc` which will be used for the actual formatting.
      Returns:
      A FormatterStep which can be losslessly roundtripped through the java serialization machinery.
    • create

      static <RoundtripState extends Serializable, EqualityState extends Serializable> FormatterStep create(String name, RoundtripState roundTrip, SerializedFunction<RoundtripState,EqualityState> equalityFunc, SerializedFunction<EqualityState,? extends FormatterFunc> formatterFunc)
      Parameters:
      name - The name of the formatter step.
      roundTrip - The roundtrip serializable state of the step.
      equalityFunc - A pure serializable function (method reference recommended) which takes the result of `roundTrip`, and returns a serializable object whose serialized representation will be used for `.equals` and `.hashCode` of the FormatterStep.
      formatterFunc - A pure serializable function (method reference recommended) which takes the result of `equalityFunc`, and returns a `FormatterFunc` which will be used for the actual formatting.
      Returns:
      A FormatterStep which can be losslessly roundtripped through the java serialization machinery.
    • createLazy

      static <State extends Serializable> FormatterStep createLazy(String name, ThrowingEx.Supplier<State> stateSupplier, SerializedFunction<State,FormatterFunc> stateToFormatter)
      Parameters:
      name - The name of the formatter step
      stateSupplier - If the rule has any state, this supplier will calculate it lazily, and the result will be passed to stateToFormatter
      stateToFormatter - A pure function which generates a formatting function using only the state supplied by state and nowhere else.
      Returns:
      A FormatterStep
    • create

      static <State extends Serializable> FormatterStep create(String name, State state, SerializedFunction<State,FormatterFunc> stateToFormatter)
      Parameters:
      name - The name of the formatter step
      state - If the rule has any state, this state must contain all of it
      stateToFormatter - A pure function which generates a formatting function using only the state supplied by state and nowhere else.
      Returns:
      A FormatterStep