6200 |
08 Apr 21 |
nicklas |
1 |
package net.sf.basedb.reggie.plugins.cmd; |
6200 |
08 Apr 21 |
nicklas |
2 |
|
6201 |
09 Apr 21 |
nicklas |
3 |
import java.util.Arrays; |
6201 |
09 Apr 21 |
nicklas |
4 |
import java.util.List; |
6200 |
08 Apr 21 |
nicklas |
5 |
|
6201 |
09 Apr 21 |
nicklas |
6 |
import net.sf.basedb.core.DbControl; |
6201 |
09 Apr 21 |
nicklas |
7 |
|
6200 |
08 Apr 21 |
nicklas |
8 |
/** |
6203 |
09 Apr 21 |
nicklas |
Validator for Integer values. It supports both numeric and string |
6203 |
09 Apr 21 |
nicklas |
values in the JSON. String values are parsed with Integer.parseInt(). |
6200 |
08 Apr 21 |
nicklas |
@since 4.32 |
6200 |
08 Apr 21 |
nicklas |
12 |
*/ |
6200 |
08 Apr 21 |
nicklas |
13 |
public class IntValidator |
6206 |
12 Apr 21 |
nicklas |
14 |
implements ValueValidator<Object, Integer>, Cloneable |
6200 |
08 Apr 21 |
nicklas |
15 |
{ |
6201 |
09 Apr 21 |
nicklas |
16 |
/** |
6201 |
09 Apr 21 |
nicklas |
Allow all integers. |
6201 |
09 Apr 21 |
nicklas |
18 |
*/ |
6201 |
09 Apr 21 |
nicklas |
19 |
public static final IntValidator ALL = new IntValidator(); |
6200 |
08 Apr 21 |
nicklas |
20 |
|
6201 |
09 Apr 21 |
nicklas |
21 |
/** |
6201 |
09 Apr 21 |
nicklas |
All positive integers > 0. |
6201 |
09 Apr 21 |
nicklas |
23 |
*/ |
6201 |
09 Apr 21 |
nicklas |
24 |
public static final IntValidator POSITIVE = new IntValidator(1, null); |
6201 |
09 Apr 21 |
nicklas |
25 |
|
6201 |
09 Apr 21 |
nicklas |
26 |
/** |
6201 |
09 Apr 21 |
nicklas |
Allow flow cells with size 2 or 4. |
6201 |
09 Apr 21 |
nicklas |
28 |
*/ |
6201 |
09 Apr 21 |
nicklas |
29 |
public static final IntValidator FLOWCELL_SIZE_2_OR_4 = new IntValidator(Arrays.asList(2, 4)); |
6201 |
09 Apr 21 |
nicklas |
30 |
|
6201 |
09 Apr 21 |
nicklas |
31 |
private Integer maxValue; |
6201 |
09 Apr 21 |
nicklas |
32 |
private Integer minValue; |
6201 |
09 Apr 21 |
nicklas |
33 |
private List<Integer> allowed; |
6201 |
09 Apr 21 |
nicklas |
34 |
|
6206 |
12 Apr 21 |
nicklas |
35 |
private Integer softMax; |
6206 |
12 Apr 21 |
nicklas |
36 |
private Integer softMin; |
6206 |
12 Apr 21 |
nicklas |
37 |
|
6201 |
09 Apr 21 |
nicklas |
38 |
/** |
6201 |
09 Apr 21 |
nicklas |
Allow all integers. |
6201 |
09 Apr 21 |
nicklas |
40 |
*/ |
6200 |
08 Apr 21 |
nicklas |
41 |
public IntValidator() |
6200 |
08 Apr 21 |
nicklas |
42 |
{} |
6200 |
08 Apr 21 |
nicklas |
43 |
|
6201 |
09 Apr 21 |
nicklas |
44 |
/** |
6201 |
09 Apr 21 |
nicklas |
Allow all integers between min and max (inclusive). |
6201 |
09 Apr 21 |
nicklas |
Null values are allowed and mean no limit. |
6206 |
12 Apr 21 |
nicklas |
Report either as error or warnings. |
6201 |
09 Apr 21 |
nicklas |
48 |
*/ |
6201 |
09 Apr 21 |
nicklas |
49 |
public IntValidator(Integer min, Integer max) |
6201 |
09 Apr 21 |
nicklas |
50 |
{ |
6201 |
09 Apr 21 |
nicklas |
51 |
this.minValue = min; |
6201 |
09 Apr 21 |
nicklas |
52 |
this.maxValue = max; |
6201 |
09 Apr 21 |
nicklas |
53 |
} |
6201 |
09 Apr 21 |
nicklas |
54 |
|
6201 |
09 Apr 21 |
nicklas |
55 |
/** |
6201 |
09 Apr 21 |
nicklas |
Only allow integers in the specified list of values. |
6201 |
09 Apr 21 |
nicklas |
57 |
*/ |
6201 |
09 Apr 21 |
nicklas |
58 |
public IntValidator(List<Integer> allowed) |
6201 |
09 Apr 21 |
nicklas |
59 |
{ |
6201 |
09 Apr 21 |
nicklas |
60 |
this.allowed = allowed; |
6201 |
09 Apr 21 |
nicklas |
61 |
} |
6201 |
09 Apr 21 |
nicklas |
62 |
|
6217 |
19 Apr 21 |
nicklas |
63 |
@Override |
6217 |
19 Apr 21 |
nicklas |
64 |
protected IntValidator clone() |
6217 |
19 Apr 21 |
nicklas |
65 |
{ |
6217 |
19 Apr 21 |
nicklas |
66 |
try |
6217 |
19 Apr 21 |
nicklas |
67 |
{ |
6217 |
19 Apr 21 |
nicklas |
68 |
return (IntValidator)super.clone(); |
6217 |
19 Apr 21 |
nicklas |
69 |
} |
6217 |
19 Apr 21 |
nicklas |
70 |
catch (CloneNotSupportedException e) |
6217 |
19 Apr 21 |
nicklas |
71 |
{ |
6217 |
19 Apr 21 |
nicklas |
// Should never happen |
6217 |
19 Apr 21 |
nicklas |
73 |
throw new UnsupportedOperationException("clone()"); |
6217 |
19 Apr 21 |
nicklas |
74 |
} |
6217 |
19 Apr 21 |
nicklas |
75 |
} |
6217 |
19 Apr 21 |
nicklas |
76 |
|
6217 |
19 Apr 21 |
nicklas |
77 |
|
6206 |
12 Apr 21 |
nicklas |
78 |
/** |
6206 |
12 Apr 21 |
nicklas |
Wrap this validator with a validator that create warnings |
6206 |
12 Apr 21 |
nicklas |
if the value is below the softMin or above the softMax limits. |
6206 |
12 Apr 21 |
nicklas |
81 |
*/ |
6206 |
12 Apr 21 |
nicklas |
82 |
public IntValidator warnIf(Integer softMin, Integer softMax) |
6206 |
12 Apr 21 |
nicklas |
83 |
{ |
6217 |
19 Apr 21 |
nicklas |
84 |
IntValidator wrap = clone(); |
6217 |
19 Apr 21 |
nicklas |
85 |
wrap.softMax = softMax; |
6217 |
19 Apr 21 |
nicklas |
86 |
wrap.softMin = softMin; |
6206 |
12 Apr 21 |
nicklas |
87 |
return wrap; |
6206 |
12 Apr 21 |
nicklas |
88 |
} |
6206 |
12 Apr 21 |
nicklas |
89 |
|
6200 |
08 Apr 21 |
nicklas |
90 |
@Override |
6201 |
09 Apr 21 |
nicklas |
91 |
public Integer isValid(DbControl dc, Object value, JsonSection section, String entryKey) |
6200 |
08 Apr 21 |
nicklas |
92 |
{ |
6201 |
09 Apr 21 |
nicklas |
93 |
Integer result = null; |
6200 |
08 Apr 21 |
nicklas |
94 |
if (value instanceof Number) |
6200 |
08 Apr 21 |
nicklas |
95 |
{ |
6201 |
09 Apr 21 |
nicklas |
96 |
result = ((Number)value).intValue(); |
6200 |
08 Apr 21 |
nicklas |
97 |
} |
6200 |
08 Apr 21 |
nicklas |
98 |
else |
6200 |
08 Apr 21 |
nicklas |
99 |
{ |
6200 |
08 Apr 21 |
nicklas |
100 |
try |
6200 |
08 Apr 21 |
nicklas |
101 |
{ |
6201 |
09 Apr 21 |
nicklas |
102 |
result = Integer.parseInt(value.toString()); |
6200 |
08 Apr 21 |
nicklas |
103 |
} |
6200 |
08 Apr 21 |
nicklas |
104 |
catch (Exception ex) |
6200 |
08 Apr 21 |
nicklas |
105 |
{ |
6474 |
04 Nov 21 |
nicklas |
106 |
section.addErrorMessage("Invalid "+entryKey+" in JSON: "+value); |
6200 |
08 Apr 21 |
nicklas |
107 |
} |
6200 |
08 Apr 21 |
nicklas |
108 |
} |
6201 |
09 Apr 21 |
nicklas |
109 |
if (result != null) |
6201 |
09 Apr 21 |
nicklas |
110 |
{ |
6201 |
09 Apr 21 |
nicklas |
111 |
if (minValue != null && result < minValue) |
6201 |
09 Apr 21 |
nicklas |
112 |
{ |
6474 |
04 Nov 21 |
nicklas |
113 |
section.addErrorMessage("Invalid "+entryKey+" in JSON: "+value+" (expected >="+minValue+")"); |
6201 |
09 Apr 21 |
nicklas |
114 |
result = null; |
6201 |
09 Apr 21 |
nicklas |
115 |
} |
6201 |
09 Apr 21 |
nicklas |
116 |
if (maxValue != null && result > maxValue) |
6201 |
09 Apr 21 |
nicklas |
117 |
{ |
6474 |
04 Nov 21 |
nicklas |
118 |
section.addErrorMessage("Invalid "+entryKey+" in JSON: "+value+" (expected <="+maxValue+")"); |
6201 |
09 Apr 21 |
nicklas |
119 |
result = null; |
6201 |
09 Apr 21 |
nicklas |
120 |
} |
6201 |
09 Apr 21 |
nicklas |
121 |
if (allowed != null) |
6201 |
09 Apr 21 |
nicklas |
122 |
{ |
6201 |
09 Apr 21 |
nicklas |
123 |
if (allowed.indexOf(result) == -1) |
6201 |
09 Apr 21 |
nicklas |
124 |
{ |
6474 |
04 Nov 21 |
nicklas |
125 |
section.addErrorMessage("Invalid "+entryKey+" in JSON: "+value+" (expected one of "+allowed+")"); |
6201 |
09 Apr 21 |
nicklas |
126 |
result = null; |
6201 |
09 Apr 21 |
nicklas |
127 |
} |
6201 |
09 Apr 21 |
nicklas |
128 |
} |
6201 |
09 Apr 21 |
nicklas |
129 |
} |
6206 |
12 Apr 21 |
nicklas |
130 |
if (result != null) |
6206 |
12 Apr 21 |
nicklas |
131 |
{ |
6206 |
12 Apr 21 |
nicklas |
132 |
if (softMin != null && result < softMin) |
6206 |
12 Apr 21 |
nicklas |
133 |
{ |
6474 |
04 Nov 21 |
nicklas |
134 |
section.addWarningMessage("Low value for "+entryKey+" in JSON: "+value+" (expected >="+softMin+")"); |
6206 |
12 Apr 21 |
nicklas |
135 |
} |
6206 |
12 Apr 21 |
nicklas |
136 |
if (softMax != null && result > softMax) |
6206 |
12 Apr 21 |
nicklas |
137 |
{ |
6474 |
04 Nov 21 |
nicklas |
138 |
section.addWarningMessage("High value for "+entryKey+" in JSON: "+value+" (expected <="+softMax+")"); |
6206 |
12 Apr 21 |
nicklas |
139 |
} |
6206 |
12 Apr 21 |
nicklas |
140 |
} |
6201 |
09 Apr 21 |
nicklas |
141 |
return result; |
6200 |
08 Apr 21 |
nicklas |
142 |
} |
6200 |
08 Apr 21 |
nicklas |
143 |
|
6200 |
08 Apr 21 |
nicklas |
144 |
@Override |
6200 |
08 Apr 21 |
nicklas |
145 |
public Class<Object> getExpectedClass() |
6200 |
08 Apr 21 |
nicklas |
146 |
{ |
6200 |
08 Apr 21 |
nicklas |
147 |
return Object.class; |
6200 |
08 Apr 21 |
nicklas |
148 |
} |
6200 |
08 Apr 21 |
nicklas |
149 |
} |