-
Notifications
You must be signed in to change notification settings - Fork 71
/
PioneerPreconditions.java
108 lines (94 loc) · 3.08 KB
/
PioneerPreconditions.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2016-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.internal;
import java.util.Collection;
import java.util.function.Supplier;
import org.junit.platform.commons.PreconditionViolationException;
/**
* Pioneer-internal utility class to handle preconditions.
* DO NOT USE THIS CLASS - IT MAY CHANGE SIGNIFICANTLY IN ANY MINOR UPDATE.
*/
public class PioneerPreconditions {
private PioneerPreconditions() {
// private constructor to prevent instantiation of utility class
}
/**
* Asserts that the supplied string is not blank.
* @param str the string to check
* @param message the precondition violation message
* @return the supplied string
*/
public static String notBlank(String str, String message) {
if (str == null || str.isBlank()) {
throw new PreconditionViolationException(message);
}
return str;
}
/**
* Asserts that the supplied string is not blank.
* @param str the string to check
* @param messageSupplier the precondition violation message supplier
* @return the supplied string
*/
public static String notBlank(String str, Supplier<String> messageSupplier) {
if (str == null || str.isBlank()) {
throw new PreconditionViolationException(messageSupplier.get());
}
return str;
}
/**
* Asserts that the supplied object is not null.
* @param object the object to check
* @param message the precondition violation message
* @return the supplied object
*/
public static <T> T notNull(T object, String message) {
if (object == null) {
throw new PreconditionViolationException(message);
}
return object;
}
/**
* Asserts that the supplied object is not null.
* @param object the object to check
* @param messageSupplier the precondition violation message supplier
* @return the supplied object
*/
public static <T> T notNull(T object, Supplier<String> messageSupplier) {
if (object == null) {
throw new PreconditionViolationException(messageSupplier.get());
}
return object;
}
/**
* Asserts that the supplied collection is not empty.
* @param collection the collection to check
* @param message the precondition violation message
* @return the supplied string
*/
public static <T extends Collection<?>> T notEmpty(T collection, String message) {
if (collection == null || collection.isEmpty()) {
throw new PreconditionViolationException(message);
}
return collection;
}
/**
* Asserts that the supplied collection is not empty.
* @param collection the collection to check
* @param messageSupplier the precondition violation message supplier
* @return the supplied string
*/
public static <T extends Collection<?>> T notEmpty(T collection, Supplier<String> messageSupplier) {
if (collection == null || collection.isEmpty()) {
throw new PreconditionViolationException(messageSupplier.get());
}
return collection;
}
}