View Javadoc

1   /*
2    ParameterCheckerUtils.java
3    Creation date : 4/07/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.common.utils.check;
26  
27  import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isEmpty;
28  
29  import java.util.Collection;
30  
31  /**
32   * The class utilities simplifying parameter checking.
33   *
34   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
35   * @since 1.3.0
36   * @version 1.3.0
37   */
38  public abstract class ParameterCheckerUtils {
39  
40      /**
41       * Checks if an object is null and throw a {@link NullPointerException} if necessary.
42       *
43       * @param obj
44       *            the object to test, mustn't be <code>null</code>.
45       * @param message
46       *            the message to set in the exception if needed.
47       * @since 1.3.0
48       */
49  
50      public static void checkNull(final Object obj, final String message) {
51          if (obj == null) {
52              throw new NullPointerException(message);
53          }
54      }
55  
56      /**
57       * Checks if a {@link String} is null and throw a {@link NullPointerException} if necessary.
58       *
59       * @param str
60       *            the {@link String} to test, mustn't be <code>null</code>.
61       * @param message
62       *            the message to set in the exception if needed.
63       * @since 1.3.0
64       */
65      public static void checkNullOrEmpty(final String str, final String message) {
66          if (isEmpty(str)) {
67              throw new NullPointerException(message);
68          }
69      }
70  
71      /**
72       * Checks if a collection doesn't have no more element than the one specified.
73       *
74       * @param collection
75       *            the collection to test, mustn't be <code>null</code>.
76       * @param maximumNumberOfElements
77       *            the maximum number of elements which can be in the collection, mustn't be inferior
78       *            to 0.
79       * @param message
80       *            the message to set in the exception if needed.
81       * @since 1.3.0
82       */
83      public static void checkNumberOfElementsInCollection(final Collection < ? > collection,
84              final int maximumNumberOfElements, final String message) {
85          if (collection.size() > maximumNumberOfElements) {
86              throw new IllegalArgumentException(message);
87          }
88      }
89  
90      /**
91       * Private constructor to prevent from instantiation.
92       *
93       * @since 1.3.0
94       */
95      private ParameterCheckerUtils() {
96          super();
97      }
98  }