forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsserts.java
More file actions
55 lines (50 loc) · 2.06 KB
/
Asserts.java
File metadata and controls
55 lines (50 loc) · 2.06 KB
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
package com.auth0.utils;
import java.util.Collection;
import okhttp3.HttpUrl;
public abstract class Asserts {
/**
* Asserts that an object is not null.
*
* @param value the value to check.
* @param name the name of the parameter, used when creating the exception message.
* @throws IllegalArgumentException if the value is null
*/
public static void assertNotNull(Object value, String name) {
if (value == null) {
throw new IllegalArgumentException(String.format("'%s' cannot be null!", name));
}
}
/**
* Asserts that a value is a valid URL.
*
* @param value the value to check.
* @param name the name of the parameter, used when creating the exception message.
* @throws IllegalArgumentException if the value is null or is not a valid URL.
*/
public static void assertValidUrl(String value, String name) {
if (value == null) {
throw new IllegalArgumentException(String.format("'%s' must be a valid URL!", name));
}
boolean isValidUrl = HttpUrl.parse(value) != null;
boolean isValidCustomSchemeUrl = value.contains(":") &&
HttpUrl.parse(value.replaceFirst(value.substring(0, value.indexOf(":")), "https")) != null;
if (!isValidUrl && !isValidCustomSchemeUrl) {
throw new IllegalArgumentException(String.format("'%s' must be a valid URL!", name));
}
}
/**
* Asserts that a collection is not null and has at least one item.
*
* @param value the value to check.
* @param name the name of the parameter, used when creating the exception message.
* @throws IllegalArgumentException if the value is null or has length of zero.
*/
public static void assertNotEmpty(Collection<?> value, String name) {
if (value == null) {
throw new IllegalArgumentException(String.format("'%s' cannot be null!", name));
}
if (value.size() == 0) {
throw new IllegalArgumentException(String.format("'%s' cannot be empty!", name));
}
}
}