-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathPreconditions.java
66 lines (59 loc) · 2.27 KB
/
Preconditions.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
package org.influxdb.impl;
import java.util.regex.Pattern;
/**
* Functions for parameter validation.
*
* @author Simon Legner
*/
public final class Preconditions {
private static final String DURATION_REGULAR_EXPRESSION = "(\\d+[wdmhs])+|inf";
private Preconditions() {
}
/**
* Enforces that the string is {@linkplain String#isEmpty() not empty}.
* @param string the string to test
* @param name variable name for reporting
* @return {@code string}
* @throws IllegalArgumentException if the string is empty
*/
public static String checkNonEmptyString(final String string, final String name) throws IllegalArgumentException {
if (string == null || string.isEmpty()) {
throw new IllegalArgumentException("Expecting a non-empty string for " + name);
}
return string;
}
/**
* Enforces that the number is larger than 0.
* @param number the number to test
* @param name variable name for reporting
* @throws IllegalArgumentException if the number is less or equal to 0
*/
public static void checkPositiveNumber(final Number number, final String name) throws IllegalArgumentException {
if (number == null || number.doubleValue() <= 0) {
throw new IllegalArgumentException("Expecting a positive number for " + name);
}
}
/**
* Enforces that the number is not negative.
* @param number the number to test
* @param name variable name for reporting
* @throws IllegalArgumentException if the number is less or equal to 0
*/
public static void checkNotNegativeNumber(final Number number, final String name) throws IllegalArgumentException {
if (number == null || number.doubleValue() < 0) {
throw new IllegalArgumentException("Expecting a positive or zero number for " + name);
}
}
/**
* Enforces that the duration is a valid influxDB duration.
* @param duration the duration to test
* @param name variable name for reporting
* @throws IllegalArgumentException if the given duration is not valid.
*/
public static void checkDuration(final String duration, final String name) throws IllegalArgumentException {
if (!duration.matches(DURATION_REGULAR_EXPRESSION)) {
throw new IllegalArgumentException("Invalid InfluxDB duration: " + duration
+ " for " + name);
}
}
}