Skip to content

Commit 2188eb0

Browse files
committed
Add Validate.isTrue(boolean, Supplier<String>)
Don't call TypeUtils.toString(Type) on every array item in TypeUtils.parameterize[WithOwner](Type, Class<?>, Map<TypeVariable<?>, Type>) unless required
1 parent 5ba7685 commit 2188eb0

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The <action> type attribute can be add,update,fix,remove.
7878
<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>
7979
<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>
8080
<action issue="LANG-1762" type="fix" dev="ggregory" due-to="Alonso Gonzalez, Gary Gregory">StopWatch methods should not delegate to deprecated methods.</action>
81+
<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>
8182
<!-- ADD -->
8283
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
8384
<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>
@@ -96,6 +97,7 @@ The <action> type attribute can be add,update,fix,remove.
9697
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils methods typed to CharSequence input and deprecate old versions typed to String.</action>
9798
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IterableStringTokenizer.</action>
9899
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableIntToFloatFunction.</action>
100+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Validate.isTrue(boolean, Supplier&lt;String&gt;).</action>
99101
<!-- UPDATE -->
100102
<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>
101103
<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>

src/main/java/org/apache/commons/lang3/Validate.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ public static void isInstanceOf(final Class<?> type, final Object obj, final Str
493493
* @see #isTrue(boolean, String, long)
494494
* @see #isTrue(boolean, String, double)
495495
* @see #isTrue(boolean, String, Object...)
496+
* @see #isTrue(boolean, Supplier)
496497
*/
497498
public static void isTrue(final boolean expression) {
498499
if (!expression) {
@@ -518,6 +519,7 @@ public static void isTrue(final boolean expression) {
518519
* @see #isTrue(boolean)
519520
* @see #isTrue(boolean, String, long)
520521
* @see #isTrue(boolean, String, Object...)
522+
* @see #isTrue(boolean, Supplier)
521523
*/
522524
public static void isTrue(final boolean expression, final String message, final double value) {
523525
if (!expression) {
@@ -543,6 +545,7 @@ public static void isTrue(final boolean expression, final String message, final
543545
* @see #isTrue(boolean)
544546
* @see #isTrue(boolean, String, double)
545547
* @see #isTrue(boolean, String, Object...)
548+
* @see #isTrue(boolean, Supplier)
546549
*/
547550
public static void isTrue(final boolean expression, final String message, final long value) {
548551
if (!expression) {
@@ -566,13 +569,36 @@ public static void isTrue(final boolean expression, final String message, final
566569
* @see #isTrue(boolean)
567570
* @see #isTrue(boolean, String, long)
568571
* @see #isTrue(boolean, String, double)
572+
* @see #isTrue(boolean, Supplier)
569573
*/
570574
public static void isTrue(final boolean expression, final String message, final Object... values) {
571575
if (!expression) {
572576
throw new IllegalArgumentException(getMessage(message, values));
573577
}
574578
}
575579

580+
/**
581+
* Validate that the argument condition is {@code true}; otherwise throwing an exception with the specified message. This method is useful when validating
582+
* according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.
583+
*
584+
* <pre>{@code
585+
* Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);
586+
* }</pre>
587+
*
588+
* @param expression the boolean expression to check
589+
* @param messageSupplier the exception message supplier
590+
* @throws IllegalArgumentException if expression is {@code false}
591+
* @see #isTrue(boolean)
592+
* @see #isTrue(boolean, String, long)
593+
* @see #isTrue(boolean, String, double)
594+
* @since 3.18.0
595+
*/
596+
public static void isTrue(final boolean expression, final Supplier<String> messageSupplier) {
597+
if (!expression) {
598+
throw new IllegalArgumentException(messageSupplier.get());
599+
}
600+
}
601+
576602
/**
577603
* Validate that the specified argument character sequence matches the specified regular
578604
* expression pattern; otherwise throwing an exception.

src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ private static Type[] extractTypeArgumentsFrom(final Map<TypeVariable<?>, Type>
553553
final Type[] result = new Type[variables.length];
554554
int index = 0;
555555
for (final TypeVariable<?> var : variables) {
556-
Validate.isTrue(mappings.containsKey(var), "missing argument mapping for %s", toString(var));
556+
Validate.isTrue(mappings.containsKey(var), () -> String.format("missing argument mapping for %s", toString(var)));
557557
result[index++] = mappings.get(var);
558558
}
559559
return result;

src/test/java/org/apache/commons/lang3/ValidateTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,22 @@ void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression
742742
}
743743
}
744744

745+
@Nested
746+
final class WithMessageSupplier {
747+
748+
@Test
749+
void shouldNotThrowForTrueExpression() {
750+
Validate.isTrue(true, () -> "MSG");
751+
}
752+
753+
@Test
754+
void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() {
755+
final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
756+
() -> Validate.isTrue(false, () -> String.format("MSG %s %s", "Object 1", "Object 2")));
757+
assertEquals("MSG Object 1 Object 2", ex.getMessage());
758+
}
759+
}
760+
745761
@Nested
746762
final class WithoutMessage {
747763

0 commit comments

Comments
 (0)