Skip to content

Commit

Permalink
Add Validate.isTrue(boolean, Supplier<String>)
Browse files Browse the repository at this point in the history
Don't call TypeUtils.toString(Type) on every array item in
TypeUtils.parameterize[WithOwner](Type, Class<?>, Map<TypeVariable<?>,
Type>) unless required
  • Loading branch information
garydgregory committed Jan 28, 2025
1 parent 5ba7685 commit 2188eb0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Spotbugs [ERROR] Medium: The field org.apache.commons.lang3.builder.DiffBuilder$SDiff.leftSupplier is transient but isn't set by deserialization [org.apache.commons.lang3.builder.DiffBuilder$SDiff] In DiffBuilder.java SE_TRANSIENT_FIELD_NOT_RESTORED.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Spotbugs [ERROR] Medium: The field org.apache.commons.lang3.builder.DiffBuilder$SDiff.rightSupplier is transient but isn't set by deserialization [org.apache.commons.lang3.builder.DiffBuilder$SDiff] In DiffBuilder.java SE_TRANSIENT_FIELD_NOT_RESTORED.</action>
<action issue="LANG-1762" type="fix" dev="ggregory" due-to="Alonso Gonzalez, Gary Gregory">StopWatch methods should not delegate to deprecated methods.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Don't call TypeUtils.toString(Type) on every array item in TypeUtils.parameterize[WithOwner](Type, Class, Map, Type>) unless required.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
Expand All @@ -96,6 +97,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils methods typed to CharSequence input and deprecate old versions typed to String.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IterableStringTokenizer.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableIntToFloatFunction.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Validate.isTrue(boolean, Supplier&lt;String&gt;).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 79 #1267, #1277, #1283, #1288, #1302.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/apache/commons/lang3/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ public static void isInstanceOf(final Class<?> type, final Object obj, final Str
* @see #isTrue(boolean, String, long)
* @see #isTrue(boolean, String, double)
* @see #isTrue(boolean, String, Object...)
* @see #isTrue(boolean, Supplier)
*/
public static void isTrue(final boolean expression) {
if (!expression) {
Expand All @@ -518,6 +519,7 @@ public static void isTrue(final boolean expression) {
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, long)
* @see #isTrue(boolean, String, Object...)
* @see #isTrue(boolean, Supplier)
*/
public static void isTrue(final boolean expression, final String message, final double value) {
if (!expression) {
Expand All @@ -543,6 +545,7 @@ public static void isTrue(final boolean expression, final String message, final
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, double)
* @see #isTrue(boolean, String, Object...)
* @see #isTrue(boolean, Supplier)
*/
public static void isTrue(final boolean expression, final String message, final long value) {
if (!expression) {
Expand All @@ -566,13 +569,36 @@ public static void isTrue(final boolean expression, final String message, final
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, long)
* @see #isTrue(boolean, String, double)
* @see #isTrue(boolean, Supplier)
*/
public static void isTrue(final boolean expression, final String message, final Object... values) {
if (!expression) {
throw new IllegalArgumentException(getMessage(message, values));
}
}

/**
* Validate that the argument condition is {@code true}; otherwise throwing an exception with the specified message. This method is useful when validating
* according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.
*
* <pre>{@code
* Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);
* }</pre>
*
* @param expression the boolean expression to check
* @param messageSupplier the exception message supplier
* @throws IllegalArgumentException if expression is {@code false}
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, long)
* @see #isTrue(boolean, String, double)
* @since 3.18.0
*/
public static void isTrue(final boolean expression, final Supplier<String> messageSupplier) {
if (!expression) {
throw new IllegalArgumentException(messageSupplier.get());
}
}

/**
* Validate that the specified argument character sequence matches the specified regular
* expression pattern; otherwise throwing an exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ private static Type[] extractTypeArgumentsFrom(final Map<TypeVariable<?>, Type>
final Type[] result = new Type[variables.length];
int index = 0;
for (final TypeVariable<?> var : variables) {
Validate.isTrue(mappings.containsKey(var), "missing argument mapping for %s", toString(var));
Validate.isTrue(mappings.containsKey(var), () -> String.format("missing argument mapping for %s", toString(var)));
result[index++] = mappings.get(var);
}
return result;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/apache/commons/lang3/ValidateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,22 @@ void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression
}
}

@Nested
final class WithMessageSupplier {

@Test
void shouldNotThrowForTrueExpression() {
Validate.isTrue(true, () -> "MSG");
}

@Test
void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() {
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> Validate.isTrue(false, () -> String.format("MSG %s %s", "Object 1", "Object 2")));
assertEquals("MSG Object 1 Object 2", ex.getMessage());
}
}

@Nested
final class WithoutMessage {

Expand Down

0 comments on commit 2188eb0

Please sign in to comment.