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.hc.core5.util;
29
30 import java.lang.reflect.Array;
31 import java.util.Collection;
32 import java.util.Map;
33 import java.util.Objects;
34
35 import org.apache.hc.core5.http.EntityDetails;
36
37 public class Args {
38
39 public static void check(final boolean expression, final String message) {
40 if (!expression) {
41 throw new IllegalArgumentException(message);
42 }
43 }
44
45 public static void check(final boolean expression, final String message, final Object... args) {
46 if (!expression) {
47 throw new IllegalArgumentException(String.format(message, args));
48 }
49 }
50
51 public static void check(final boolean expression, final String message, final Object arg) {
52 if (!expression) {
53 throw new IllegalArgumentException(String.format(message, arg));
54 }
55 }
56
57 public static long checkContentLength(final EntityDetails entityDetails) {
58
59
60
61 return checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
62 "HTTP entity too large to be buffered in memory)");
63 }
64
65 public static int checkRange(final int value, final int lowInclusive, final int highInclusive,
66 final String message) {
67 if (value < lowInclusive || value > highInclusive) {
68 throw illegalArgumentException("%s: %d is out of range [%d, %d]", message, value,
69 lowInclusive, highInclusive);
70 }
71 return value;
72 }
73
74 public static long checkRange(final long value, final long lowInclusive, final long highInclusive,
75 final String message) {
76 if (value < lowInclusive || value > highInclusive) {
77 throw illegalArgumentException("%s: %d is out of range [%d, %d]", message, value,
78 lowInclusive, highInclusive);
79 }
80 return value;
81 }
82
83 public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
84 notNull(argument, name);
85 if (isEmpty(argument)) {
86 throw illegalArgumentExceptionNotEmpty(name);
87 }
88 if (TextUtils.containsBlanks(argument)) {
89 throw new IllegalArgumentException(name + " must not contain blanks");
90 }
91 return argument;
92 }
93
94 private static IllegalArgumentException illegalArgumentException(final String format, final Object... args) {
95 return new IllegalArgumentException(String.format(format, args));
96 }
97
98 private static IllegalArgumentException illegalArgumentExceptionNotEmpty(final String name) {
99 return new IllegalArgumentException(name + " must not be empty");
100 }
101
102 private static NullPointerException NullPointerException(final String name) {
103 return new NullPointerException(name + " must not be null");
104 }
105
106 public static <T extends CharSequence> T notBlank(final T argument, final String name) {
107 notNull(argument, name);
108 if (TextUtils.isBlank(argument)) {
109 throw new IllegalArgumentException(name + " must not be blank");
110 }
111 return argument;
112 }
113
114 public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
115 notNull(argument, name);
116 if (isEmpty(argument)) {
117 throw illegalArgumentExceptionNotEmpty(name);
118 }
119 return argument;
120 }
121
122 public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
123 notNull(argument, name);
124 if (isEmpty(argument)) {
125 throw illegalArgumentExceptionNotEmpty(name);
126 }
127 return argument;
128 }
129
130 public static <T> T notEmpty(final T argument, final String name) {
131 notNull(argument, name);
132 if (isEmpty(argument)) {
133 throw illegalArgumentExceptionNotEmpty(name);
134 }
135 return argument;
136 }
137
138 public static int notNegative(final int n, final String name) {
139 if (n < 0) {
140 throw illegalArgumentException("%s must not be negative: %d", name, n);
141 }
142 return n;
143 }
144
145 public static long notNegative(final long n, final String name) {
146 if (n < 0) {
147 throw illegalArgumentException("%s must not be negative: %d", name, n);
148 }
149 return n;
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public static <T> T notNull(final T argument, final String name) {
165 return Objects.requireNonNull(argument, name);
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static boolean isEmpty(final Object object) {
194 if (object == null) {
195 return true;
196 }
197 if (object instanceof CharSequence) {
198 return ((CharSequence) object).length() == 0;
199 }
200 if (object.getClass().isArray()) {
201 return Array.getLength(object) == 0;
202 }
203 if (object instanceof Collection<?>) {
204 return ((Collection<?>) object).isEmpty();
205 }
206 if (object instanceof Map<?, ?>) {
207 return ((Map<?, ?>) object).isEmpty();
208 }
209 return false;
210 }
211
212 public static int positive(final int n, final String name) {
213 if (n <= 0) {
214 throw illegalArgumentException("%s must not be negative or zero: %d", name, n);
215 }
216 return n;
217 }
218
219 public static long positive(final long n, final String name) {
220 if (n <= 0) {
221 throw illegalArgumentException("%s must not be negative or zero: %d", name, n);
222 }
223 return n;
224 }
225
226 public static <T extends TimeValue> T positive(final T timeValue, final String name) {
227 positive(timeValue.getDuration(), name);
228 return timeValue;
229 }
230
231
232
233
234
235 private Args() {
236 }
237
238 }