Skip to content

Commit

Permalink
Add 1-arg, 2-arg, 3-arg methods (#29)
Browse files Browse the repository at this point in the history
These methods avoid allocating an array, so should have a tiny
performance improvement on the happy path.

Adding them now to hopefully preempt a guava 21 style breakage, because
while adding these later would be compilation compatible, the resultant
binary would not be compatible!
  • Loading branch information
iamdanfox authored Mar 27, 2018
1 parent 034e74a commit 1ffa84f
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions preconditions/src/main/java/com/palantir/logsafe/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,52 @@ public static void checkArgument(boolean expression) {
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* @param expression a boolean expression
* @param message the loggable exception message
* @throws SafeIllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, String message) {
if (!expression) {
throw new SafeIllegalArgumentException(message);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkArgument(boolean, String, Arg...)} for details.
*/
public static void checkArgument(boolean expression, String message, Arg<?> arg) {
if (!expression) {
throw new SafeIllegalArgumentException(message, arg);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkArgument(boolean, String, Arg...)} for details.
*/
public static void checkArgument(boolean expression, String message, Arg<?> arg1, Arg<?> arg2) {
if (!expression) {
throw new SafeIllegalArgumentException(message, arg1, arg2);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkArgument(boolean, String, Arg...)} for details.
*/
public static void checkArgument(boolean expression, String message, Arg<?> arg1, Arg<?> arg2, Arg<?> arg3) {
if (!expression) {
throw new SafeIllegalArgumentException(message, arg1, arg2, arg3);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
Expand Down Expand Up @@ -65,6 +111,53 @@ public static void checkState(boolean expression) {
}
}

/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*
* @param expression a boolean expression
* @param message the loggable exception message
* @throws SafeIllegalStateException if {@code expression} is false
*/
public static void checkState(boolean expression, String message) {
if (!expression) {
throw new SafeIllegalStateException(message);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkState(boolean, String, Arg...)} for details.
*/
public static void checkState(boolean expression, String message, Arg<?> arg) {
if (!expression) {
throw new SafeIllegalStateException(message, arg);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkState(boolean, String, Arg...)} for details.
*/
public static void checkState(boolean expression, String message, Arg<?> arg1, Arg<?> arg2) {
if (!expression) {
throw new SafeIllegalStateException(message, arg1, arg2);
}
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* <p>See {@link #checkState(boolean, String, Arg...)} for details.
*/
public static void checkState(boolean expression, String message, Arg<?> arg1, Arg<?> arg2, Arg<?> arg3) {
if (!expression) {
throw new SafeIllegalStateException(message, arg1, arg2, arg3);
}
}

/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
Expand Down Expand Up @@ -95,6 +188,61 @@ public static <T> T checkNotNull(T reference) {
return reference;
}

/**
* Ensures that an Object reference passed as a parameter to the calling method is not null.
*
* @param reference an String reference
* @param message the loggable exception message
* @return the non-null reference that was validated
* @throws SafeNullPointerException if {@code reference} is null
*/
@CanIgnoreReturnValue
public static <T> T checkNotNull(T reference, String message) {
if (reference == null) {
throw new SafeNullPointerException(message);
}
return reference;
}

/**
* Ensures that an Object reference passed as a parameter to the calling method is not null.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@CanIgnoreReturnValue
public static <T> T checkNotNull(T reference, String message, Arg<?> arg) {
if (reference == null) {
throw new SafeNullPointerException(message, arg);
}
return reference;
}

/**
* Ensures that an Object reference passed as a parameter to the calling method is not null.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@CanIgnoreReturnValue
public static <T> T checkNotNull(T reference, String message, Arg<?> arg1, Arg<?> arg2) {
if (reference == null) {
throw new SafeNullPointerException(message, arg1, arg2);
}
return reference;
}

/**
* Ensures that an Object reference passed as a parameter to the calling method is not null.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@CanIgnoreReturnValue
public static <T> T checkNotNull(T reference, String message, Arg<?> arg1, Arg<?> arg2, Arg<?> arg3) {
if (reference == null) {
throw new SafeNullPointerException(message, arg1, arg2, arg3);
}
return reference;
}

/**
* Ensures that an Object reference passed as a parameter to the calling method is not null.
*
Expand Down

0 comments on commit 1ffa84f

Please sign in to comment.