1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }