View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Shared
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
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   * @author peck7 on 14/03/2019.
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  }