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