View Javadoc

1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package com.tomgibara.pronto.util;
19  
20  /**
21   * Holds static methods for performing common checks on method parameters.
22   * 
23   * @author Tom Gibara
24   */
25  
26  public final class Arguments {
27  
28      /**
29       * Tests whether the supplied parameter is null.
30       * 
31       * @param obj
32       *            the parameter
33       * @param name
34       *            the name of the parameter
35       * @throws IllegalArgumentException
36       *             if the parameter is null
37       */
38  
39      public static void notNull(final Object obj, final String name) throws IllegalArgumentException {
40          if (obj == null) throw new IllegalArgumentException(String.format("null %s", name));
41      }
42  
43      /**
44       * Tests whether the supplied string parameter is not null and not the empty
45       * string.
46       * 
47       * @param str
48       *            the string parameter
49       * @param name
50       *            the name of the parameter
51       * @throws IllegalArgumentException
52       *             if the parameter is null or has zero length
53       */
54  
55      public static void notEmpty(final String str, final String name) throws IllegalArgumentException {
56          notNull(str, name);
57          if (str.length() == 0) throw new IllegalArgumentException(String.format("empty string %s", name));
58      }
59  
60      /**
61       * Tests whether the supplied long parameter is not negative.
62       * 
63       * @param num
64       *            the long parameter
65       * @param name
66       *            the name of the parameter
67       * @throws IllegalArgumentException
68       *             if the parameter is less than zero
69       */
70  
71      public static void notNegative(final long num, final String name) throws IllegalArgumentException {
72          if (num < 0) throw new IllegalArgumentException(String.format("negative %s: %d", name, num));
73      }
74  
75      private Arguments() { }
76  }