1 package fr.ifremer.quadrige3.core.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.lang3.StringUtils;
25
26 import java.util.Collection;
27 import java.util.Objects;
28
29
30
31
32 public class Assert extends org.springframework.util.Assert {
33
34 @SuppressWarnings("deprecation")
35 public static void notNull(Object object) {
36 notNull(object, "this argument is required; it must not be null");
37 }
38
39 @SuppressWarnings("deprecation")
40 public static void isNull(Object object) {
41 isNull(object, "this argument must be null");
42 }
43
44 @SuppressWarnings("deprecation")
45 public static void notEmpty(Collection<?> collection) {
46 notEmpty(collection, "this collection must not be empty: it must contain at least 1 element");
47 }
48
49 @SuppressWarnings("deprecation")
50 public static void isTrue(boolean expression) {
51 isTrue(expression, "this expression must be true");
52 }
53
54 public static void nonNullElement(Collection<?> collection) {
55 if (collection != null && collection.stream().anyMatch(Objects::isNull)) {
56 throw new IllegalArgumentException("this collection must not contain any null elements");
57 }
58 }
59
60 @SuppressWarnings("deprecation")
61 public static void notEmpty(Object[] array) {
62 notEmpty(array, "this array must not be empty: it must contain at least 1 element");
63 }
64
65 public static void notBlank(String string) {
66 notBlank(string, "this string must not be blank");
67 }
68
69 public static void notBlank(String string, String message) {
70 isTrue(StringUtils.isNotBlank(string), message);
71 }
72
73 public static void equals(Object o1, Object o2) {
74 isTrue(Objects.equals(o1, o2), "both objects must be equals");
75 }
76
77 public static void equals(Object o1, Object o2, String message) {
78 isTrue(Objects.equals(o1, o2), message);
79 }
80
81 public static void size(Object object, int size) {
82 isTrue(CollectionUtils.size(object) == size, String.format("this object must have %d elements", size));
83 }
84
85 }