• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #460

25 Jan 2025 06:36PM UTC coverage: 50.047% (-0.3%) from 50.372%
#460

push

github

aalmiray
chore(announce): Annotate deprecated announcers

25196 of 50345 relevant lines covered (50.05%)

0.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.78
/api/jreleaser-utils/src/main/java/org/jreleaser/util/ObjectUtils.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.util;
19

20
import java.util.Collection;
21
import java.util.Map;
22

23
import static java.util.Objects.requireNonNull;
24
import static org.jreleaser.util.StringUtils.requireNonBlank;
25

26
/**
27
 * @author Andres Almiray
28
 * @since 0.2.0
29
 */
30
public final class ObjectUtils {
31
    private static final String MESSAGE = "message";
32

33
    private ObjectUtils() {
34
        // noop
35
    }
36

37
    /**
38
     * Checks that the specified condition is met. This method is designed
39
     * primarily for doing parameter validation in methods and constructors,
40
     * as demonstrated below:
41
     * <blockquote><pre>
42
     * public Foo(int[] array) {
43
     *     GriffonClassUtils.requireState(array.length &gt; 0);
44
     * }
45
     * </pre></blockquote>
46
     *
47
     * @param condition the condition to check
48
     * @throws IllegalStateException if {@code condition} evaluates to false
49
     */
50
    public static void requireState(boolean condition) {
51
        if (!condition) {
×
52
            throw new IllegalStateException();
×
53
        }
54
    }
×
55

56
    /**
57
     * Checks that the specified condition is met and throws a customized
58
     * {@link IllegalStateException} if it is. This method is designed primarily
59
     * for doing parameter validation in methods and constructors with multiple
60
     * parameters, as demonstrated below:
61
     * <blockquote><pre>
62
     * public Foo(int[] array) {
63
     *     GriffonClassUtils.requireState(array.length &gt; 0, "array must not be empty");
64
     * }
65
     * </pre></blockquote>
66
     *
67
     * @param condition the condition to check
68
     * @param message   detail message to be used in the event that a {@code
69
     *                  IllegalStateException} is thrown
70
     * @throws IllegalStateException if {@code condition} evaluates to false
71
     */
72
    public static void requireState(boolean condition, String message) {
73
        if (!condition) {
1✔
74
            throw new IllegalStateException(message);
×
75
        }
76
    }
1✔
77

78
    /**
79
     * Checks that the specified array is not empty, throwing a
80
     * {@link IllegalStateException} if it is.
81
     *
82
     * @param array the array to check
83
     * @throws NullPointerException  if {@code array} is null
84
     * @throws IllegalStateException if {@code array} is empty
85
     */
86
    public static byte[] requireNonEmpty(byte[] array) {
87
        requireNonNull(array);
×
88
        requireState(array.length != 0);
×
89
        return array;
×
90
    }
91

92
    /**
93
     * Checks that the specified array is not empty, throwing a customized
94
     * {@link IllegalStateException} if it is.
95
     *
96
     * @param array   the array to check
97
     * @param message detail message to be used in the event that a {@code
98
     *                IllegalStateException} is thrown
99
     * @throws NullPointerException     if {@code array} is null
100
     * @throws IllegalArgumentException if {@code message} is {@code blank}
101
     * @throws IllegalStateException    if {@code array} is empty
102
     */
103
    public static byte[] requireNonEmpty(byte[] array, String message) {
104
        requireNonNull(array);
×
105
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
106
        return array;
×
107
    }
108

109
    /**
110
     * Checks that the specified array is not empty, throwing a
111
     * {@link IllegalStateException} if it is.
112
     *
113
     * @param array the array to check
114
     * @throws NullPointerException  if {@code array} is null
115
     * @throws IllegalStateException if {@code array} is empty
116
     */
117
    public static short[] requireNonEmpty(short[] array) {
118
        requireNonNull(array);
×
119
        requireState(array.length != 0);
×
120
        return array;
×
121
    }
122

123
    /**
124
     * Checks that the specified array is not empty, throwing a customized
125
     * {@link IllegalStateException} if it is.
126
     *
127
     * @param array   the array to check
128
     * @param message detail message to be used in the event that a {@code
129
     *                IllegalStateException} is thrown
130
     * @throws NullPointerException     if {@code array} is null
131
     * @throws IllegalArgumentException if {@code message} is {@code blank}
132
     * @throws IllegalStateException    if {@code array} is empty
133
     */
134
    public static short[] requireNonEmpty(short[] array, String message) {
135
        requireNonNull(array);
×
136
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
137
        return array;
×
138
    }
139

140
    /**
141
     * Checks that the specified array is not empty, throwing a
142
     * {@link IllegalStateException} if it is.
143
     *
144
     * @param array the array to check
145
     * @throws NullPointerException  if {@code array} is null
146
     * @throws IllegalStateException if {@code array} is empty
147
     */
148
    public static int[] requireNonEmpty(int[] array) {
149
        requireNonNull(array);
×
150
        requireState(array.length != 0);
×
151
        return array;
×
152
    }
153

154
    /**
155
     * Checks that the specified array is not empty, throwing a customized
156
     * {@link IllegalStateException} if it is.
157
     *
158
     * @param array   the array to check
159
     * @param message detail message to be used in the event that a {@code
160
     *                IllegalStateException} is thrown
161
     * @throws NullPointerException     if {@code array} is null
162
     * @throws IllegalArgumentException if {@code message} is {@code blank}
163
     * @throws IllegalStateException    if {@code array} is empty
164
     */
165
    public static int[] requireNonEmpty(int[] array, String message) {
166
        requireNonNull(array);
×
167
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
168
        return array;
×
169
    }
170

171
    /**
172
     * Checks that the specified array is not empty, throwing a
173
     * {@link IllegalStateException} if it is.
174
     *
175
     * @param array the array to check
176
     * @throws NullPointerException  if {@code array} is null
177
     * @throws IllegalStateException if {@code array} is empty
178
     */
179
    public static long[] requireNonEmpty(long[] array) {
180
        requireNonNull(array);
×
181
        requireState(array.length != 0);
×
182
        return array;
×
183
    }
184

185
    /**
186
     * Checks that the specified array is not empty, throwing a customized
187
     * {@link IllegalStateException} if it is.
188
     *
189
     * @param array   the array to check
190
     * @param message detail message to be used in the event that a {@code
191
     *                IllegalStateException} is thrown
192
     * @throws NullPointerException     if {@code array} is null
193
     * @throws IllegalArgumentException if {@code message} is {@code blank}
194
     * @throws IllegalStateException    if {@code array} is empty
195
     */
196
    public static long[] requireNonEmpty(long[] array, String message) {
197
        requireNonNull(array);
×
198
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
199
        return array;
×
200
    }
201

202
    /**
203
     * Checks that the specified array is not empty, throwing a
204
     * {@link IllegalStateException} if it is.
205
     *
206
     * @param array the array to check
207
     * @throws NullPointerException  if {@code array} is null
208
     * @throws IllegalStateException if {@code array} is empty
209
     */
210
    public static float[] requireNonEmpty(float[] array) {
211
        requireNonNull(array);
×
212
        requireState(array.length != 0);
×
213
        return array;
×
214
    }
215

216
    /**
217
     * Checks that the specified array is not empty, throwing a customized
218
     * {@link IllegalStateException} if it is.
219
     *
220
     * @param array   the array to check
221
     * @param message detail message to be used in the event that a {@code
222
     *                IllegalStateException} is thrown
223
     * @throws NullPointerException     if {@code array} is null
224
     * @throws IllegalArgumentException if {@code message} is {@code blank}
225
     * @throws IllegalStateException    if {@code array} is empty
226
     */
227
    public static float[] requireNonEmpty(float[] array, String message) {
228
        requireNonNull(array);
×
229
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
230
        return array;
×
231
    }
232

233
    /**
234
     * Checks that the specified array is not empty, throwing a
235
     * {@link IllegalStateException} if it is.
236
     *
237
     * @param array the array to check
238
     * @throws NullPointerException  if {@code array} is null
239
     * @throws IllegalStateException if {@code array} is empty
240
     */
241
    public static double[] requireNonEmpty(double[] array) {
242
        requireNonNull(array);
×
243
        requireState(array.length != 0);
×
244
        return array;
×
245
    }
246

247
    /**
248
     * Checks that the specified array is not empty, throwing a customized
249
     * {@link IllegalStateException} if it is.
250
     *
251
     * @param array   the array to check
252
     * @param message detail message to be used in the event that a {@code
253
     *                IllegalStateException} is thrown
254
     * @throws NullPointerException     if {@code array} is null
255
     * @throws IllegalArgumentException if {@code message} is {@code blank}
256
     * @throws IllegalStateException    if {@code array} is empty
257
     */
258
    public static double[] requireNonEmpty(double[] array, String message) {
259
        requireNonNull(array);
×
260
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
261
        return array;
×
262
    }
263

264
    /**
265
     * Checks that the specified array is not empty, throwing a
266
     * {@link IllegalStateException} if it is.
267
     *
268
     * @param array the array to check
269
     * @throws NullPointerException  if {@code array} is null
270
     * @throws IllegalStateException if {@code array} is empty
271
     */
272
    public static char[] requireNonEmpty(char[] array) {
273
        requireNonNull(array);
×
274
        requireState(array.length != 0);
×
275
        return array;
×
276
    }
277

278
    /**
279
     * Checks that the specified array is not empty, throwing a customized
280
     * {@link IllegalStateException} if it is.
281
     *
282
     * @param array   the array to check
283
     * @param message detail message to be used in the event that a {@code
284
     *                IllegalStateException} is thrown
285
     * @throws NullPointerException     if {@code array} is null
286
     * @throws IllegalArgumentException if {@code message} is {@code blank}
287
     * @throws IllegalStateException    if {@code array} is empty
288
     */
289
    public static char[] requireNonEmpty(char[] array, String message) {
290
        requireNonNull(array);
×
291
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
292
        return array;
×
293
    }
294

295
    /**
296
     * Checks that the specified array is not empty, throwing a
297
     * {@link IllegalStateException} if it is.
298
     *
299
     * @param array the array to check
300
     * @throws NullPointerException  if {@code array} is null
301
     * @throws IllegalStateException if {@code array} is empty
302
     */
303
    public static boolean[] requireNonEmpty(boolean[] array) {
304
        requireNonNull(array);
×
305
        requireState(array.length != 0);
×
306
        return array;
×
307
    }
308

309
    /**
310
     * Checks that the specified array is not empty, throwing a customized
311
     * {@link IllegalStateException} if it is.
312
     *
313
     * @param array   the array to check
314
     * @param message detail message to be used in the event that a {@code
315
     *                IllegalStateException} is thrown
316
     * @throws NullPointerException     if {@code array} is null
317
     * @throws IllegalArgumentException if {@code message} is {@code blank}
318
     * @throws IllegalStateException    if {@code array} is empty
319
     */
320
    public static boolean[] requireNonEmpty(boolean[] array, String message) {
321
        requireNonNull(array);
×
322
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
323
        return array;
×
324
    }
325

326
    /**
327
     * Checks that the specified array is not empty, throwing a
328
     * {@link IllegalStateException} if it is.
329
     *
330
     * @param array the array to check
331
     * @throws NullPointerException  if {@code array} is null
332
     * @throws IllegalStateException if {@code array} is empty
333
     */
334
    public static <E> E[] requireNonEmpty(E[] array) {
335
        requireNonNull(array);
×
336
        requireState(array.length != 0);
×
337
        return array;
×
338
    }
339

340
    /**
341
     * Checks that the specified array is not empty, throwing a customized
342
     * {@link IllegalStateException} if it is.
343
     *
344
     * @param array   the array to check
345
     * @param message detail message to be used in the event that a {@code
346
     *                IllegalStateException} is thrown
347
     * @throws NullPointerException     if {@code array} is null
348
     * @throws IllegalArgumentException if {@code message} is {@code blank}
349
     * @throws IllegalStateException    if {@code array} is empty
350
     */
351
    public static <E> E[] requireNonEmpty(E[] array, String message) {
352
        requireNonNull(array);
×
353
        requireState(array.length != 0, requireNonBlank(message, MESSAGE));
×
354
        return array;
×
355
    }
356

357
    /**
358
     * Checks that the specified collection is not empty, throwing a
359
     * {@link IllegalStateException} if it is.
360
     *
361
     * @param collection the collection to check
362
     * @throws NullPointerException  if {@code collection} is null
363
     * @throws IllegalStateException if {@code collection} is empty
364
     */
365
    public static Collection<?> requireNonEmpty(Collection<?> collection) {
366
        requireNonNull(collection);
×
367
        requireState(!collection.isEmpty());
×
368
        return collection;
×
369
    }
370

371
    /**
372
     * Checks that the specified collection is not empty, throwing a customized
373
     * {@link IllegalStateException} if it is.
374
     *
375
     * @param collection the collection to check
376
     * @param message    detail message to be used in the event that a {@code
377
     *                   IllegalStateException} is thrown
378
     * @throws NullPointerException     if {@code collection} is null
379
     * @throws IllegalArgumentException if {@code message} is {@code blank}
380
     * @throws IllegalStateException    if {@code collection} is empty
381
     */
382
    public static Collection<?> requireNonEmpty(Collection<?> collection, String message) {
383
        requireNonNull(collection);
×
384
        requireState(!collection.isEmpty(), requireNonBlank(message, MESSAGE));
×
385
        return collection;
×
386
    }
387

388
    /**
389
     * Checks that the specified map is not empty, throwing a
390
     * {@link IllegalStateException} if it is.
391
     *
392
     * @param map the map to check
393
     * @throws NullPointerException  if {@code map} is null
394
     * @throws IllegalStateException if {@code map} is empty
395
     */
396
    public static Map<?, ?> requireNonEmpty(Map<?, ?> map) {
397
        requireNonNull(map);
×
398
        requireState(!map.isEmpty());
×
399
        return map;
×
400
    }
401

402
    /**
403
     * Checks that the specified map is not empty, throwing a customized
404
     * {@link IllegalStateException} if it is.
405
     *
406
     * @param map     the map to check
407
     * @param message detail message to be used in the event that a {@code
408
     *                IllegalStateException} is thrown
409
     * @throws NullPointerException     if {@code map} is null
410
     * @throws IllegalArgumentException if {@code message} is {@code blank}
411
     * @throws IllegalStateException    if {@code map} is empty
412
     */
413
    public static Map<?, ?> requireNonEmpty(Map<?, ?> map, String message) {
414
        requireNonNull(map);
×
415
        requireState(!map.isEmpty(), requireNonBlank(message, MESSAGE));
×
416
        return map;
×
417
    }
418
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc