forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.java
More file actions
63 lines (57 loc) · 1.99 KB
/
Validate.java
File metadata and controls
63 lines (57 loc) · 1.99 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
56
57
58
59
60
61
62
63
package com.gooddata;
import java.util.Collection;
import java.util.Iterator;
/**
* Validation utils
*/
public abstract class Validate {
/**
* Throws IllegalArgumentException if the value is null, otherwise returns the value.
*
* @param value input value
* @param argumentName the name of input argument
* @param <T> the type of the argument
* @return the value
*/
public static <T> T notNull(T value, String argumentName) {
if (value == null) {
throw new IllegalArgumentException(argumentName + " can't be null");
}
return value;
}
/**
* Throws IllegalArgumentException if the char sequence is empty, otherwise returns the char sequence.
*
* @param value input char sequence
* @param argumentName the name of input argument
* @param <T> the type of char sequence
* @return the char sequence
*/
public static <T extends CharSequence> T notEmpty(T value, String argumentName) {
notNull(value, argumentName);
if (value.toString().trim().length() == 0) {
throw new IllegalArgumentException(argumentName + " can't be empty");
}
return value;
}
/**
*
* Throws IllegalArgumentException if the collection contains null elements (or is null), otherwise returns
* the collection.
*
* @param collection input collection
* @param argumentName the name of input argument
* @param <T> the type of the collection
* @return the collection
*/
public static <T extends Collection> T noNullElements(T collection, String argumentName) {
notNull(collection, argumentName);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new IllegalArgumentException(argumentName + " contains null element at index: " + i);
}
}
return collection;
}
}