Class PreconditionExcerpts


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

      Fields 
      Modifier and Type Field Description
      private static java.util.regex.Pattern ANY_OPERATOR
      Matches all operators with a lower precedence than unary negation (!).
      private static java.util.regex.Pattern BOOLEAN_BINARY_OPERATOR  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Excerpt checkArgument​(java.lang.String conditionTemplate, java.lang.String messageTemplate, java.lang.Object... args)
      Returns an excerpt equivalent to Guava's Preconditions.checkArgument(boolean, String, Object...).
      static Excerpt checkState​(java.lang.String conditionTemplate, java.lang.String messageTemplate, java.lang.Object... args)
      Returns an excerpt equivalent to Guava's Preconditions.checkState(boolean, String, Object...).
      private static java.lang.String negate​(java.lang.String conditionTemplate)
      Negates conditionTemplate, removing unnecessary brackets and double-negatives if possible.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ANY_OPERATOR

        private static final java.util.regex.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 java.util.regex.Pattern BOOLEAN_BINARY_OPERATOR
    • Constructor Detail

      • PreconditionExcerpts

        private PreconditionExcerpts()
    • Method Detail

      • checkArgument

        public static Excerpt checkArgument​(java.lang.String conditionTemplate,
                                            java.lang.String messageTemplate,
                                            java.lang.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:
        condition - a code template to pass to the checkArgument method
        message - the error message template to pass to the checkArgument method
        args - excerpts containing the error message arguments to pass to the checkArgument method
      • checkState

        public static Excerpt checkState​(java.lang.String conditionTemplate,
                                         java.lang.String messageTemplate,
                                         java.lang.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:
        condition - an excerpt containing the expression to pass to the checkState method
        message - the error message template to pass to the checkState method
        args - excerpts containing the error message arguments to pass to the checkState method
      • negate

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