1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.util;
29
30 import java.util.Collection;
31
32 public class Args {
33
34 public static void check(final boolean expression, final String message) {
35 if (!expression) {
36 throw new IllegalArgumentException(message);
37 }
38 }
39
40 public static void check(final boolean expression, final String message, final Object... args) {
41 if (!expression) {
42 throw new IllegalArgumentException(String.format(message, args));
43 }
44 }
45
46 public static void check(final boolean expression, final String message, final Object arg) {
47 if (!expression) {
48 throw new IllegalArgumentException(String.format(message, arg));
49 }
50 }
51
52 public static <T> T notNull(final T argument, final String name) {
53 if (argument == null) {
54 throw new IllegalArgumentException(name + " may not be null");
55 }
56 return argument;
57 }
58
59 public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
60 if (argument == null) {
61 throw new IllegalArgumentException(name + " may not be null");
62 }
63 if (TextUtils.isEmpty(argument)) {
64 throw new IllegalArgumentException(name + " may not be empty");
65 }
66 return argument;
67 }
68
69 public static <T extends CharSequence> T notBlank(final T argument, final String name) {
70 if (argument == null) {
71 throw new IllegalArgumentException(name + " may not be null");
72 }
73 if (TextUtils.isBlank(argument)) {
74 throw new IllegalArgumentException(name + " may not be blank");
75 }
76 return argument;
77 }
78
79 public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
80 if (argument == null) {
81 throw new IllegalArgumentException(name + " may not be null");
82 }
83 if (argument.length() == 0) {
84 throw new IllegalArgumentException(name + " may not be empty");
85 }
86 if (TextUtils.containsBlanks(argument)) {
87 throw new IllegalArgumentException(name + " may not contain blanks");
88 }
89 return argument;
90 }
91
92 public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
93 if (argument == null) {
94 throw new IllegalArgumentException(name + " may not be null");
95 }
96 if (argument.isEmpty()) {
97 throw new IllegalArgumentException(name + " may not be empty");
98 }
99 return argument;
100 }
101
102 public static int positive(final int n, final String name) {
103 if (n <= 0) {
104 throw new IllegalArgumentException(name + " may not be negative or zero");
105 }
106 return n;
107 }
108
109 public static long positive(final long n, final String name) {
110 if (n <= 0) {
111 throw new IllegalArgumentException(name + " may not be negative or zero");
112 }
113 return n;
114 }
115
116 public static int notNegative(final int n, final String name) {
117 if (n < 0) {
118 throw new IllegalArgumentException(name + " may not be negative");
119 }
120 return n;
121 }
122
123 public static long notNegative(final long n, final String name) {
124 if (n < 0) {
125 throw new IllegalArgumentException(name + " may not be negative");
126 }
127 return n;
128 }
129
130 }