Class PreconditionExcerpts
- java.lang.Object
-
- org.inferred.freebuilder.processor.source.PreconditionExcerpts
-
public class PreconditionExcerpts extends java.lang.ObjectCode snippets that call or emulate Guava'sPreconditionsmethods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPreconditionExcerpts.GuavaCheckExcerpt
-
Field Summary
Fields Modifier and Type Field Description private static java.util.regex.PatternANY_OPERATORMatches all operators with a lower precedence than unary negation (!).private static java.util.regex.PatternBOOLEAN_BINARY_OPERATOR
-
Constructor Summary
Constructors Modifier Constructor Description privatePreconditionExcerpts()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ExcerptcheckArgument(java.lang.String conditionTemplate, java.lang.String messageTemplate, java.lang.Object... args)Returns an excerpt equivalent to Guava'sPreconditions.checkArgument(boolean, String, Object...).static ExcerptcheckState(java.lang.String conditionTemplate, java.lang.String messageTemplate, java.lang.Object... args)Returns an excerpt equivalent to Guava'sPreconditions.checkState(boolean, String, Object...).private static java.lang.Stringnegate(java.lang.String conditionTemplate)NegatesconditionTemplate, removing unnecessary brackets and double-negatives if possible.
-
-
-
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
-
-
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'sPreconditions.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 methodmessage- the error message template to pass to the checkArgument methodargs- 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'sPreconditions.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 methodmessage- the error message template to pass to the checkState methodargs- excerpts containing the error message arguments to pass to the checkState method
-
negate
private static java.lang.String negate(java.lang.String conditionTemplate)
NegatesconditionTemplate, removing unnecessary brackets and double-negatives if possible.
-
-