Skip to content

Commit 9e0c6c5

Browse files
authored
Merge pull request #1 from harshitopenxcell/main
feat: add Assert Util class
2 parents eaf2414 + 0224fb8 commit 9e0c6c5

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
#Gradle
26+
/.gradle/
27+
.gradle
28+
/build
29+
/gradle/
30+
31+
# Eclipse
32+
.classpath
33+
.project
34+
.settings/
35+
36+
# Intellij
37+
.idea/
38+
*.iml
39+
*.iws
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.javaquery.util;
2+
3+
import com.javaquery.util.collection.Collections;
4+
5+
import java.util.Collection;
6+
import java.util.Map;
7+
import java.util.function.Supplier;
8+
9+
/**
10+
* {@code Assert} is a collection of utility methods that helps asserting conditions and throw exception if condition fails
11+
*
12+
* @author Harshit
13+
* @since 1.0
14+
*/
15+
public final class Assert {
16+
17+
private Assert() {
18+
}
19+
20+
/**
21+
* <em>Assert</em> that {@code object} is {@code not null}.
22+
*
23+
* @param object object to check
24+
* @param <T> Type of the exception to be thrown
25+
* @param exceptionSupplier supplier of a exception to be thrown
26+
* @throws T if {@code object} is {@code null}
27+
* @throws NullPointerException if {@code object} is {@code null} and {@code exceptionSupplier} is {@code null}
28+
*/
29+
public static <T extends Throwable> void nonNull(Object object, Supplier<T> exceptionSupplier) throws T {
30+
if (object == null) {
31+
throw exceptionSupplier.get();
32+
}
33+
}
34+
35+
/**
36+
* <em>Assert</em> that {@code object} is {@code null}.
37+
*
38+
* @param object object to check
39+
* @param <T> Type of the exception to be thrown
40+
* @param exceptionSupplier supplier of a exception to throw
41+
* @throws T if {@code object} is {@code not null}
42+
* @throws NullPointerException if {@code object} is {@code not null} and {@code exceptionSupplier} is {@code null}
43+
*/
44+
public static <T extends Throwable> void isNull(Object object, Supplier<T> exceptionSupplier) throws T {
45+
if (object != null) {
46+
throw exceptionSupplier.get();
47+
}
48+
}
49+
50+
/**
51+
* <em>Assert</em> that {@code expression} is {@code true}.
52+
*
53+
* @param expression expression to check
54+
* @param <T> Type of the exception to be thrown
55+
* @param exceptionSupplier supplier of a exception to throw
56+
* @throws T if {@code expression} is {@code false}
57+
* @throws NullPointerException if {@code expression} is {@code false} and {@code exceptionSupplier} is {@code null}
58+
*/
59+
public static <T extends Throwable> void isTrue(boolean expression, Supplier<T> exceptionSupplier) throws T {
60+
if (!expression) {
61+
throw exceptionSupplier.get();
62+
}
63+
}
64+
65+
/**
66+
* <em>Assert</em> that {@code expression} is {@code false}.
67+
*
68+
* @param expression expression to check
69+
* @param <T> Type of the exception to be thrown
70+
* @param exceptionSupplier supplier of a exception to throw
71+
* @throws T if {@code expression} is {@code true}
72+
* @throws NullPointerException if {@code expression} is {@code true} and {@code exceptionSupplier} is {@code null}
73+
*/
74+
public static <T extends Throwable> void isFalse(boolean expression, Supplier<T> exceptionSupplier) throws T {
75+
if (expression) {
76+
throw exceptionSupplier.get();
77+
}
78+
}
79+
80+
/**
81+
* <em>Assert</em> that {@code collection} is {@code not null} and {@code not empty}.
82+
*
83+
* @param collection collection to check
84+
* @param <T> Type of the exception to be thrown
85+
* @param exceptionSupplier supplier of a exception to throw
86+
* @throws T if {@code collection} is {@code null} or {@code empty}
87+
* @throws NullPointerException if {@code collection} is {@code null} or {@code empty} and {@code exceptionSupplier} is {@code null}
88+
*/
89+
public static <T extends Throwable> void nonNullNonEmpty(Collection<?> collection, Supplier<T> exceptionSupplier) throws T {
90+
if (Collections.nullOrEmpty(collection)) {
91+
throw exceptionSupplier.get();
92+
}
93+
}
94+
95+
/**
96+
* <em>Assert</em> that {@code collection} is {@code null} or {@code empty}.
97+
*
98+
* @param collection collection to check
99+
* @param <T> Type of the exception to be thrown
100+
* @param exceptionSupplier supplier of a exception to throw
101+
* @throws T if {@code collection} is {@code not null} and {@code not empty}
102+
* @throws NullPointerException if {@code collection} is {@code not null} and {@code not empty} and {@code exceptionSupplier} is {@code null}
103+
*/
104+
public static <T extends Throwable> void nullOrEmpty(Collection<?> collection, Supplier<T> exceptionSupplier) throws T {
105+
if (Collections.nonNullNonEmpty(collection)) {
106+
throw exceptionSupplier.get();
107+
}
108+
}
109+
110+
/**
111+
* <em>Assert</em> that {@code map} is {@code not null} and {@code not empty}.
112+
*
113+
* @param map map to check
114+
* @param <T> Type of the exception to be thrown
115+
* @param exceptionSupplier supplier of a exception to throw
116+
* @throws T if {@code map} is {@code null} or {@code empty}
117+
* @throws NullPointerException if {@code map} is {@code null} or {@code empty} and {@code exceptionSupplier} is {@code null}
118+
*/
119+
public static <T extends Throwable> void nonNullNonEmptyMap(Map<?, ?> map, Supplier<T> exceptionSupplier) throws T {
120+
if (Collections.nullOrEmpty(map)) {
121+
throw exceptionSupplier.get();
122+
}
123+
}
124+
125+
/**
126+
* <em>Assert</em> that {@code map} is {@code null} or {@code empty}.
127+
*
128+
* @param map map to check
129+
* @param <T> Type of the exception to be thrown
130+
* @param exceptionSupplier supplier of a exception to throw
131+
* @throws T if {@code map} is {@code not null} and {@code not empty}
132+
* @throws NullPointerException if {@code map} is {@code not null} and {@code not empty} and {@code exceptionSupplier} is {@code null}
133+
*/
134+
public static <T extends Throwable> void nullOrEmptyMap(Map<?, ?> map, Supplier<T> exceptionSupplier) throws T {
135+
if (Collections.nonNullNonEmpty(map)) {
136+
throw exceptionSupplier.get();
137+
}
138+
}
139+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.javaquery.util;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
class TestAssert {
13+
14+
private static final RuntimeException RUNTIME_EXCEPTION = new RuntimeException("exception");
15+
private static final Object NULL_OBJECT = null;
16+
private static final Object NON_NULL_OBJECT = new Object();
17+
private static final Collection<Integer> EMPTY_COLLECTION = new ArrayList<>(0);
18+
private static final Collection<Integer> NON_EMPTY_COLLECTION = Collections.singletonList(1);
19+
private static final Map<Integer, String> EMPTY_MAP = new HashMap<>(0);
20+
private static final Map<Integer, String> NON_EMPTY_MAP = Collections.singletonMap(1, "A");
21+
22+
@Test
23+
void test_nonNull() {
24+
Assertions.assertDoesNotThrow(() -> Assert.nonNull(NON_NULL_OBJECT, TestAssert::getRuntimeException));
25+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nonNull(NULL_OBJECT, TestAssert::getRuntimeException));
26+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nonNull(NULL_OBJECT, null));
27+
}
28+
29+
@Test
30+
void test_isNull() {
31+
Assertions.assertDoesNotThrow(() -> Assert.isNull(NULL_OBJECT, TestAssert::getRuntimeException));
32+
Assertions.assertThrows(RuntimeException.class, () -> Assert.isNull(NON_NULL_OBJECT, TestAssert::getRuntimeException));
33+
Assertions.assertThrows(NullPointerException.class, () -> Assert.isNull(NON_NULL_OBJECT, null));
34+
}
35+
36+
@Test
37+
void test_isTrue() {
38+
Assertions.assertDoesNotThrow(() -> Assert.isTrue(true, TestAssert::getRuntimeException));
39+
Assertions.assertThrows(RuntimeException.class, () -> Assert.isTrue(false, TestAssert::getRuntimeException));
40+
Assertions.assertThrows(NullPointerException.class, () -> Assert.isTrue(false, null));
41+
}
42+
43+
@Test
44+
void test_isFalse() {
45+
Assertions.assertDoesNotThrow(() -> Assert.isFalse(false, TestAssert::getRuntimeException));
46+
Assertions.assertThrows(RuntimeException.class, () -> Assert.isFalse(true, TestAssert::getRuntimeException));
47+
Assertions.assertThrows(NullPointerException.class, () -> Assert.isFalse(true, null));
48+
}
49+
50+
@Test
51+
void test_nonNullNonEmpty() {
52+
Assertions.assertDoesNotThrow(() -> Assert.nonNullNonEmpty(NON_EMPTY_COLLECTION, TestAssert::getRuntimeException));
53+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nonNullNonEmpty(null, TestAssert::getRuntimeException));
54+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nonNullNonEmpty(null, null));
55+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nonNullNonEmpty(EMPTY_COLLECTION, TestAssert::getRuntimeException));
56+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nonNullNonEmpty(EMPTY_COLLECTION, null));
57+
}
58+
59+
@Test
60+
void test_nullOrEmpty() {
61+
Assertions.assertDoesNotThrow(() -> Assert.nullOrEmpty(null, TestAssert::getRuntimeException));
62+
Assertions.assertDoesNotThrow(() -> Assert.nullOrEmpty(EMPTY_COLLECTION, TestAssert::getRuntimeException));
63+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nullOrEmpty(NON_EMPTY_COLLECTION, TestAssert::getRuntimeException));
64+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nullOrEmpty(NON_EMPTY_COLLECTION, null));
65+
}
66+
67+
@Test
68+
void test_nonNullNonEmptyMap() {
69+
Assertions.assertDoesNotThrow(() -> Assert.nonNullNonEmptyMap(NON_EMPTY_MAP, TestAssert::getRuntimeException));
70+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nonNullNonEmptyMap(null, TestAssert::getRuntimeException));
71+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nonNullNonEmptyMap(null, null));
72+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nonNullNonEmptyMap(EMPTY_MAP, TestAssert::getRuntimeException));
73+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nonNullNonEmptyMap(EMPTY_MAP, null));
74+
}
75+
76+
@Test
77+
void test_nullOrEmptyMap() {
78+
Assertions.assertDoesNotThrow(() -> Assert.nullOrEmptyMap(null, TestAssert::getRuntimeException));
79+
Assertions.assertDoesNotThrow(() -> Assert.nullOrEmptyMap(EMPTY_MAP, TestAssert::getRuntimeException));
80+
Assertions.assertThrows(RuntimeException.class, () -> Assert.nullOrEmptyMap(NON_EMPTY_MAP, TestAssert::getRuntimeException));
81+
Assertions.assertThrows(NullPointerException.class, () -> Assert.nullOrEmptyMap(NON_EMPTY_MAP, null));
82+
}
83+
84+
private static RuntimeException getRuntimeException() {
85+
return RUNTIME_EXCEPTION;
86+
}
87+
}

0 commit comments

Comments
 (0)