Interface SplitUtil.Strategy

Enclosing class:
SplitUtil

public static interface SplitUtil.Strategy
Function which applies a programmable strategy used to determine a splitting point.
See Also:
  • Field Details

    • ANYWHERE

      static final SplitUtil.Strategy ANYWHERE
      Strategy which splits at any character to satisfy the limit.
      This is the default strategy if none is provided, and should ideally only be the final one in your list.
    • NEWLINE

      static final SplitUtil.Strategy NEWLINE
      Splits on newline characters. Specifically on '\n'.
    • WHITESPACE

      static final SplitUtil.Strategy WHITESPACE
      Splits on every character which is considered whitespace.
  • Method Details

    • apply

      int apply(@Nonnull String string, int offset, int limit)
      Implements a splitting strategy.

      The goal of a strategy is to implement a greedy algorithm to find the optimal point to split the string. Ideally, this should be close to the limit.

      This should not return an offset larger than limit. Any offset lower than the input offset, is interpreted as unsuccessful.

      Parameters:
      string - The input string
      offset - The current offset where to start your substring
      limit - The maximum length your substring should be
      Returns:
      The exclusive end index of your chunk, negative to indicate failure. (should be in range of offset < x <= limit).
    • onChar

      @Nonnull static SplitUtil.Strategy onChar(char c)
      Strategy to split on the provided character.

      An example use-case would be to define places via '\0' and then splitting exactly on those.

      Parameters:
      c - The splitting character
      Returns:
      The strategy to split on that character
    • onChar

      Strategy to split on the provided character tests.
      Parameters:
      predicate - The splitting character test
      Returns:
      The strategy to split on characters that pass the test
      Throws:
      IllegalArgumentException - If the predicate is null