5
5
*/
6
6
package org .jaqpot .core .service .validator ;
7
7
8
+ import com .fasterxml .jackson .databind .deser .std .NumberDeserializers ;
8
9
import org .apache .commons .lang3 .StringUtils ;
9
10
import org .jaqpot .core .data .serialize .JSONSerializer ;
10
11
import org .jaqpot .core .model .Parameter ;
@@ -27,8 +28,10 @@ public class ParameterValidator {
27
28
public enum Type {
28
29
NUMERIC ,
29
30
STRING ,
31
+ INTEGER ,
30
32
NUMERIC_ARRAY ,
31
33
STRING_ARRAY ,
34
+ INTEGER_ARRAY ,
32
35
UNDEFINED
33
36
}
34
37
@@ -77,7 +80,9 @@ public void validate(String input, Set<Parameter> parameters) throws ParameterTy
77
80
78
81
//Get type of algorithm's parameter
79
82
Type typeOfParameter = Type .UNDEFINED ;
80
- if (isNumeric (parameter .getValue ().toString ()))
83
+ if (isInteger (parameter .getValue ().toString ()))
84
+ typeOfParameter = Type .INTEGER ;
85
+ else if (isNumeric (parameter .getValue ().toString ()))
81
86
typeOfParameter = Type .NUMERIC ;
82
87
else if (StringUtils .isAlphanumericSpace (parameter .getValue ().toString ()))
83
88
typeOfParameter = Type .STRING ;
@@ -89,6 +94,18 @@ else if (parameter.getValue() instanceof Collection)
89
94
90
95
//Get type of user's value and validate
91
96
switch (typeOfParameter ) {
97
+ case INTEGER :
98
+ if (isInteger (value .toString ())) {
99
+ if (parameter .getAllowedValues ()!=null )
100
+ checkAllowedValues (parameterId ,Integer .parseInt (value .toString ()), (List <Integer >) (List <?>) parameter .getAllowedValues ());
101
+ if (parameter .getMinValue ()!=null && isInteger (parameter .getMinValue ().toString ()))
102
+ checkIsLessThan (parameterId ,Integer .parseInt (value .toString ()), (Integer .parseInt (parameter .getMinValue ().toString ())));
103
+ if (parameter .getMaxValue ()!=null && isInteger (parameter .getMaxValue ().toString ()))
104
+ checkIsGreaterThan (parameterId ,Integer .parseInt (value .toString ()), (Integer .parseInt (parameter .getMaxValue ().toString ())));
105
+ }
106
+ else
107
+ throw new ParameterTypeException ("Parameter with id:" + parameterId + " must be a whole number" );
108
+ break ;
92
109
case NUMERIC :
93
110
if (isNumeric (value .toString ())) {
94
111
if (parameter .getAllowedValues ()!=null )
@@ -115,6 +132,20 @@ else if (parameter.getValue() instanceof Collection)
115
132
else
116
133
throw new ParameterTypeException ("Parameter with id: '" + parameterId + "' must be Alphanumeric" );
117
134
break ;
135
+ case INTEGER_ARRAY :
136
+ if ((value instanceof Collection && getTypeOfCollection ((Collection ) value ) == Type .INTEGER_ARRAY )) {
137
+ checkAllowedValues (parameterId ,value , parameter .getAllowedValues ());
138
+ checkMinMaxSize (parameterId ,(Collection ) value , parameter .getMinArraySize (), parameter .getMaxArraySize ());
139
+ for (Object o : (Collection ) value ) {
140
+ if (parameter .getMinValue ()!=null && isInteger (parameter .getMinValue ().toString ()))
141
+ checkIsLessThan (parameterId ,Integer .parseInt (o .toString ()), (Integer .parseInt (parameter .getMinValue ().toString ())));
142
+ if (parameter .getMaxValue ()!=null && isInteger (parameter .getMaxValue ().toString ()))
143
+ checkIsGreaterThan (parameterId ,Integer .parseInt (o .toString ()), (Integer .parseInt (parameter .getMaxValue ().toString ())));
144
+ }
145
+ }
146
+ else
147
+ throw new ParameterTypeException ("Parameter with id: '" + parameterId + "' must be Array of integer values" );
148
+ break ;
118
149
case NUMERIC_ARRAY :
119
150
if ((value instanceof Collection && getTypeOfCollection ((Collection ) value ) == Type .NUMERIC_ARRAY )) {
120
151
checkAllowedValues (parameterId ,value , parameter .getAllowedValues ());
@@ -149,7 +180,7 @@ else if (parameter.getValue() instanceof Collection)
149
180
}
150
181
}
151
182
152
- static <T > boolean checkAllowedValues (String parameterId , T value , List <T > elements ) throws ParameterRangeException {
183
+ private static <T > boolean checkAllowedValues (String parameterId , T value , List <T > elements ) throws ParameterRangeException {
153
184
if (value !=null )
154
185
if (elements !=null ) {
155
186
for (T o : elements ) {
@@ -161,21 +192,21 @@ static <T> boolean checkAllowedValues(String parameterId, T value, List<T> elem
161
192
return true ;
162
193
}
163
194
164
- static <T extends Comparable <? super T >> Boolean checkIsLessThan (String parameterId , T value , T minimum ) throws ParameterRangeException {
195
+ private static <T extends Comparable <? super T >> Boolean checkIsLessThan (String parameterId , T value , T minimum ) throws ParameterRangeException {
165
196
if (minimum != null && isNumeric (minimum .toString ()))
166
197
if (value .compareTo (minimum )<0 )
167
198
throw new ParameterRangeException ("Parameter with id: '" + parameterId + "' has a value less than the parameter's allowed minimum" );
168
199
return true ;
169
200
}
170
201
171
- static <T extends Comparable <? super T >> Boolean checkIsGreaterThan (String parameterId , T value , T maximum ) throws ParameterRangeException {
202
+ private static <T extends Comparable <? super T >> Boolean checkIsGreaterThan (String parameterId , T value , T maximum ) throws ParameterRangeException {
172
203
if (maximum != null )
173
204
if (value .compareTo (maximum )>0 )
174
205
throw new ParameterRangeException ("Parameter with id: '" + parameterId + "' has a value greater than the parameter's allowed maximum" );
175
206
return true ;
176
207
}
177
208
178
- static Boolean checkMinMaxSize (String parameterId , Collection collection , Integer minSize , Integer maxSize ) throws ParameterRangeException {
209
+ private static Boolean checkMinMaxSize (String parameterId , Collection collection , Integer minSize , Integer maxSize ) throws ParameterRangeException {
179
210
if (minSize !=null && isNumeric (minSize .toString ()))
180
211
if (collection .size ()<minSize )
181
212
throw new ParameterRangeException ("Parameter with id: '" + parameterId + "' has an array size lees than the parameter's allowed minimum array size" );
@@ -187,31 +218,23 @@ static Boolean checkMinMaxSize(String parameterId, Collection collection, Intege
187
218
188
219
//Returns if array is a (consistent) collection of Strings (Type.STRING) or Numbers (Type.NUMERIC).
189
220
//else returns Type.UNDEFINED
190
- static Type getTypeOfCollection (Collection collection )
191
- {
221
+ private static Type getTypeOfCollection (Collection collection ) {
192
222
Type content = null ;
193
- for (Object value :collection )
194
- {
195
- if (!isNumeric (value .toString ()))
196
- if (!StringUtils .isAlphanumericSpace (value .toString ()))
197
- return Type .UNDEFINED ;
198
- else
199
- if (content == Type .NUMERIC_ARRAY )
200
- return Type .UNDEFINED ;
201
- else
202
- content =Type .STRING_ARRAY ;
203
- else
204
- if (content == Type .STRING_ARRAY )
205
- return Type .UNDEFINED ;
206
- else
207
- content =Type .NUMERIC_ARRAY ;
223
+ for (Object value : collection ) {
224
+ if (isInteger (value .toString ()) && (content == Type .INTEGER_ARRAY || content == null ))
225
+ content = Type .INTEGER_ARRAY ;
226
+ else if (isNumeric (value .toString ()) && (content == Type .NUMERIC_ARRAY || content == Type .INTEGER_ARRAY || content == null ))
227
+ content = Type .NUMERIC_ARRAY ;
228
+ else if (StringUtils .isAlphanumericSpace (value .toString ()) && (content == Type .STRING_ARRAY || content == null ))
229
+ content = Type .STRING_ARRAY ;
230
+ else return Type .UNDEFINED ;
208
231
}
209
232
return content ;
210
233
}
211
234
212
235
//Probably most performant solution to check for isNumeric, according to discussion here
213
236
//http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/1102916#1102916
214
- static boolean isNumeric (String str )
237
+ private static boolean isNumeric (String str )
215
238
{
216
239
try
217
240
{
@@ -223,4 +246,12 @@ static boolean isNumeric(String str)
223
246
}
224
247
return true ;
225
248
}
249
+
250
+ private static boolean isInteger (String str )
251
+ {
252
+ if (isNumeric (str ))
253
+ if (str .contains ("." ))
254
+ return false ;
255
+ return true ;
256
+ }
226
257
}
0 commit comments