-
Notifications
You must be signed in to change notification settings - Fork 86
/
IsInteger.java
49 lines (39 loc) · 1.22 KB
/
IsInteger.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
package edu.cornell.mannlib.vitro.webapp.dynapi.components.validators;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.ArrayView;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.Data;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.JsonContainerView;
public class IsInteger extends IsNotBlank {
private static final Log log = LogFactory.getLog(IsInteger.class);
@Override
public boolean isValid(String name, Data data) {
if (!super.isValid(name, data)) {
return false;
}
if (JsonContainerView.isJsonArray(data.getParam())) {
List array = ArrayView.getArray(data);
for (Object value : array) {
if (!isInteger(value.toString())) {
return false;
}
}
} else {
if (!isInteger(data.getSerializedValue())) {
return false;
}
}
return true;
}
private boolean isInteger(String string) {
try {
Integer.parseInt(string);
return true;
} catch(Exception e) {
log.debug(e,e);
}
log.debug("Value is not a number. Validation failed.");
return false;
}
}