Skip to content

Commit

Permalink
add Preconditions.checkArgumentNotNull which throws IAE (#59), add jd…
Browse files Browse the repository at this point in the history
…k 17 to CI

* add Preconditions.checkArgumentNotNull which throws IllegalArgumentException when argument null
* add jdk 17 to ci.yml
  • Loading branch information
davidmoten committed Mar 30, 2023
1 parent af7e401 commit ead9f22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['8', '11']
java: ['8', '11', '17']
steps:
- uses: actions/checkout@v2
- name: Set up JDK ${{ matrix.java }}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/davidmoten/guavamini/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public static <T> T checkNotNull(T t, String message) {
throw new NullPointerException(message);
return t;
}

public static <T> T checkArgumentNotNull(T t) {
return checkArgumentNotNull(t, "argument cannot be null");
}

public static <T> T checkArgumentNotNull(T t, String message) {
if (t == null) {
throw new IllegalArgumentException(message);
}
return t;
}

public static void checkArgument(boolean b, String message) {
if (!b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -19,7 +20,7 @@ public void testNullThrowsException() {
public void testNotNullDoesNotThrowException() {
Preconditions.checkNotNull(new Object());
}

@Test
public void testNotNullReturnsTypedObject() {
Integer n = 1;
Expand Down Expand Up @@ -62,4 +63,20 @@ public void testCoverage() {
Asserts.assertIsUtilityClass(Preconditions.class);
}

@Test
public void testCheckArgumentNotNullThrowsIAE() {
try {
Preconditions.checkArgumentNotNull(null);
Assert.fail();
} catch (IllegalArgumentException e) {
assertEquals("argument cannot be null", e.getMessage());
}
}

@Test
public void testCheckArgumentNotNullReturnsWhenNotNull() {
Object o = new Object();
assertTrue(o == Preconditions.checkArgumentNotNull(o));
}

}

0 comments on commit ead9f22

Please sign in to comment.