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