Class PreconditionExcerpts

java.lang.Object
org.inferred.freebuilder.processor.source.PreconditionExcerpts

public class PreconditionExcerpts extends Object
Code snippets that call or emulate Guava's Preconditions methods.
  • Field Details

    • ANY_OPERATOR

      private static final Pattern ANY_OPERATOR
      Matches all operators with a lower precedence than unary negation (!).

      False positives are acceptable, as the only downside is putting unnecessary brackets around the condition when Guava is not available, so a simple check for offending characters is fine, even though they might actually be in a string.

    • BOOLEAN_BINARY_OPERATOR

      private static final Pattern BOOLEAN_BINARY_OPERATOR
  • Constructor Details

    • PreconditionExcerpts

      private PreconditionExcerpts()
  • Method Details

    • checkArgument

      public static Excerpt checkArgument(String conditionTemplate, String messageTemplate, Object... args)
      Returns an excerpt equivalent to Guava's Preconditions.checkArgument(boolean, String, Object...).
      • If Guava is available, Preconditions.checkArgument will be used.
      • Otherwise, the check will be done with an if block.
      code.add(checkArgument("%1$s >= 0", "age must be >= 0 (got %1$s)", "age"));
      Parameters:
      args - excerpts containing the error message arguments to pass to the checkArgument method
      condition - a code template to pass to the checkArgument method
      message - the error message template to pass to the checkArgument method
    • checkState

      public static Excerpt checkState(String conditionTemplate, String messageTemplate, Object... args)
      Returns an excerpt equivalent to Guava's Preconditions.checkState(boolean, String, Object...).
      • If Guava is available, Preconditions.checkState will be used.
      • Otherwise, the check will be done with an if block.
      code.add(checkState("%1$s < %2$s",
              "start must be before end (got %1$s and %2$s)", "start", "end"));
      Parameters:
      args - excerpts containing the error message arguments to pass to the checkState method
      condition - an excerpt containing the expression to pass to the checkState method
      message - the error message template to pass to the checkState method
    • negate

      private static String negate(String conditionTemplate)
      Negates conditionTemplate, removing unnecessary brackets and double-negatives if possible.