Skip to content

Commit fc5cd8a

Browse files
committed
Add generic check to Require.
The new method `Require.that` checks that the argument is true. If not an `IllegalArgumentException` is thrown.
1 parent beac8b5 commit fc5cd8a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/org/culturegraph/mf/util/Require.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ public static int notNegative(final int value, final String message) {
7575
return value;
7676
}
7777

78+
/**
79+
* Throws an {@link IllegalArgumentException} if {@code condition} is false.
80+
*/
81+
public static void that(final boolean condition) {
82+
that(condition, "parameter is not valid");
83+
}
84+
85+
/**
86+
* Throws an {@link IllegalArgumentException} if {@code condition} is false.
87+
*
88+
* @param message
89+
* exception message
90+
* @return {@code value}
91+
*/
92+
private static void that(final boolean condition, final String message) {
93+
if (!condition) {
94+
throw new IllegalArgumentException(message);
95+
}
96+
}
97+
7898
/**
7999
* Throws an {@link IndexOutOfBoundsException} if {@code index} is negative
80100
* or equal to or greater than {@code arrayLength}.

src/test/java/org/culturegraph/mf/util/RequireTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public void notNegativeShouldReturnArgIfArgIsNotNegative() {
4949
assertEquals(0, Require.notNegative(0));
5050
}
5151

52+
@Test(expected = IllegalArgumentException.class)
53+
public void thatShouldFailIfArgumentIsFalse() {
54+
Require.that(false);
55+
}
56+
57+
@Test
58+
public void thatShouldDoNothingIfArgumentIsTrue() {
59+
Require.that(true);
60+
}
61+
5262
@Test(expected = IndexOutOfBoundsException.class)
5363
public void validArrayIndexShouldThrowIndexOutOfBoundsExceptionIfIndexIsNegative() {
5464
Require.validArrayIndex(-1, 2);

0 commit comments

Comments
 (0)