[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / AttributeValueValidation.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator;
18
19 /**
20  * This class holds a set of methods for validating the attribute values of
21  * different types using the given value properties {@link AttributeProperty}.
22  */
23 public class AttributeValueValidation implements
24         AttributeValueVisitor.VisitingMethods<Boolean> {
25     private AttributeProperty mProperty = null;
26
27     /**
28      * Constructs {@link AttributeValueValidation} with the given attribute
29      * value property.
30      *
31      * @param property
32      *            Attribute value property.
33      */
34     public AttributeValueValidation(AttributeProperty property) {
35         mProperty = property;
36     }
37
38     /**
39      * API to validate the given attribute value. The given value is said to be
40      * valid if it matches with the values given in the value property.
41      *
42      * @param value
43      *            {@link AttributeValue} to be validated.
44      * @return True if the given value is valid, otherwise false.
45      */
46     public boolean validate(AttributeValue value) {
47         AttributeValueVisitor visitor = new AttributeValueVisitor(value, this);
48         Boolean result = (Boolean) visitor.visit();
49         return result.booleanValue();
50     }
51
52     /**
53      * API to validate an Integer value.
54      *
55      * @return True if the given value is valid, otherwise false.
56      */
57     @Override
58     public Boolean visitingValue(Integer value) {
59         if (mProperty == null)
60             return false;
61
62         if (checkRange(value.doubleValue()) || checkValueSet(value))
63             return true;
64         return false;
65     }
66
67     /**
68      * API to validate a Double value.
69      *
70      * @return True if the given value is valid, otherwise false.
71      */
72     @Override
73     public Boolean visitingValue(Double value) {
74         if (mProperty == null)
75             return false;
76
77         if (checkRange(value.doubleValue()) || checkValueSet(value))
78             return true;
79         return false;
80     }
81
82     /**
83      * API to validate a Boolean value.
84      *
85      * @return True if the given value is valid, otherwise false.
86      */
87     @Override
88     public Boolean visitingValue(Boolean value) {
89         if (mProperty == null)
90             return false;
91
92         if (checkValueSet(value))
93             return true;
94         return false;
95     }
96
97     /**
98      * API to validate a String value.
99      *
100      * @return True if the given value is valid, otherwise false.
101      */
102     @Override
103     public Boolean visitingValue(String value) {
104         if (mProperty == null)
105             return false;
106
107         if (checkRange(value.length()) || checkValueSet(value))
108             return true;
109         return false;
110     }
111
112     /**
113      * API to validate a {@link SimulatorResourceModel} value.
114      *
115      * @return True if the given value is valid, otherwise false.
116      */
117     @Override
118     public Boolean visitingValue(SimulatorResourceModel value) {
119         return false;
120     }
121
122     /**
123      * API to validate an array of integer values.
124      *
125      * @return True if the given value is valid, otherwise false.
126      */
127     @Override
128     public Boolean visitingValue(Integer[] values) {
129         if (mProperty == null)
130             return false;
131
132         if (!checkRange(values.length))
133             return false;
134
135         if (mProperty.getChildProperty() != null) {
136             AttributeValueValidation rangeValidation = new AttributeValueValidation(
137                     mProperty.getChildProperty());
138             for (Integer value : values) {
139                 if (rangeValidation.visitingValue(value) == false)
140                     return false;
141             }
142         }
143
144         return true;
145     }
146
147     /**
148      * API to validate an array of double values.
149      *
150      * @return True if the given value is valid, otherwise false.
151      */
152     @Override
153     public Boolean visitingValue(Double[] values) {
154         if (mProperty == null)
155             return false;
156
157         if (!checkRange(values.length))
158             return false;
159
160         if (mProperty.getChildProperty() != null) {
161             AttributeValueValidation rangeValidation = new AttributeValueValidation(
162                     mProperty.getChildProperty());
163             for (Double value : values) {
164                 if (rangeValidation.visitingValue(value) == false)
165                     return false;
166             }
167         }
168
169         return true;
170     }
171
172     /**
173      * API to validate an array of boolean values.
174      *
175      * @return True if the given value is valid, otherwise false.
176      */
177     @Override
178     public Boolean visitingValue(Boolean[] values) {
179         if (mProperty == null)
180             return false;
181
182         if (!checkRange(values.length))
183             return false;
184
185         if (mProperty.getChildProperty() != null) {
186             AttributeValueValidation rangeValidation = new AttributeValueValidation(
187                     mProperty.getChildProperty());
188             for (Boolean value : values) {
189                 if (rangeValidation.visitingValue(value) == false)
190                     return false;
191             }
192         }
193
194         return true;
195     }
196
197     /**
198      * API to validate an array of string values.
199      *
200      * @return True if the given value is valid, otherwise false.
201      */
202     @Override
203     public Boolean visitingValue(String[] values) {
204         if (mProperty == null)
205             return false;
206
207         if (!checkRange(values.length))
208             return false;
209
210         if (mProperty.getChildProperty() != null) {
211             AttributeValueValidation rangeValidation = new AttributeValueValidation(
212                     mProperty.getChildProperty());
213             for (String value : values) {
214                 if (rangeValidation.visitingValue(value) == false)
215                     return false;
216             }
217         }
218
219         return true;
220     }
221
222     /**
223      * API to validate an array of {@link SimulatorResourceModel} values.
224      *
225      * @return True if the given value is valid, otherwise false.
226      */
227     @Override
228     public Boolean visitingValue(SimulatorResourceModel[] value) {
229         return false;
230     }
231
232     /**
233      * API to validate a 2D array of integer values.
234      *
235      * @return True if the given value is valid, otherwise false.
236      */
237     @Override
238     public Boolean visitingValue(Integer[][] values) {
239         if (mProperty == null)
240             return false;
241
242         if (!checkRange(values.length))
243             return false;
244
245         if (mProperty.getChildProperty() != null) {
246             AttributeValueValidation rangeValidation = new AttributeValueValidation(
247                     mProperty.getChildProperty());
248             for (Integer[] value : values) {
249                 if (rangeValidation.visitingValue(value) == false)
250                     return false;
251             }
252         }
253
254         return true;
255     }
256
257     /**
258      * API to validate a 2D array of double values.
259      *
260      * @return True if the given value is valid, otherwise false.
261      */
262     @Override
263     public Boolean visitingValue(Double[][] values) {
264         if (mProperty == null)
265             return false;
266
267         if (!checkRange(values.length))
268             return false;
269
270         if (mProperty.getChildProperty() != null) {
271             AttributeValueValidation rangeValidation = new AttributeValueValidation(
272                     mProperty.getChildProperty());
273             for (Double[] value : values) {
274                 if (rangeValidation.visitingValue(value) == false)
275                     return false;
276             }
277         }
278
279         return true;
280     }
281
282     /**
283      * API to validate a 2D array of boolean values.
284      *
285      * @return True if the given value is valid, otherwise false.
286      */
287     @Override
288     public Boolean visitingValue(Boolean[][] values) {
289         if (mProperty == null)
290             return false;
291
292         if (!checkRange(values.length))
293             return false;
294
295         if (mProperty.getChildProperty() != null) {
296             AttributeValueValidation rangeValidation = new AttributeValueValidation(
297                     mProperty.getChildProperty());
298             for (Boolean[] value : values) {
299                 if (rangeValidation.visitingValue(value) == false)
300                     return false;
301             }
302         }
303
304         return true;
305     }
306
307     /**
308      * API to validate a 2D array of string values.
309      *
310      * @return True if the given value is valid, otherwise false.
311      */
312     @Override
313     public Boolean visitingValue(String[][] values) {
314         if (mProperty == null)
315             return false;
316
317         if (!checkRange(values.length))
318             return false;
319
320         if (mProperty.getChildProperty() != null) {
321             AttributeValueValidation rangeValidation = new AttributeValueValidation(
322                     mProperty.getChildProperty());
323             for (String[] value : values) {
324                 if (rangeValidation.visitingValue(value) == false)
325                     return false;
326             }
327         }
328
329         return true;
330     }
331
332     /**
333      * API to validate a 2D array of {@link SimulatorResourceModel} values.
334      *
335      * @return True if the given value is valid, otherwise false.
336      */
337     @Override
338     public Boolean visitingValue(SimulatorResourceModel[][] value) {
339         return false;
340     }
341
342     /**
343      * API to validate a 3D array of integer values.
344      *
345      * @return True if the given value is valid, otherwise false.
346      */
347     @Override
348     public Boolean visitingValue(Integer[][][] values) {
349         if (mProperty == null)
350             return false;
351
352         if (!checkRange(values.length))
353             return false;
354
355         if (mProperty.getChildProperty() != null) {
356             AttributeValueValidation rangeValidation = new AttributeValueValidation(
357                     mProperty.getChildProperty());
358             for (Integer[][] value : values) {
359                 if (rangeValidation.visitingValue(value) == false)
360                     return false;
361             }
362         }
363
364         return true;
365     }
366
367     /**
368      * API to validate a 3D array of double values.
369      *
370      * @return True if the given value is valid, otherwise false.
371      */
372     @Override
373     public Boolean visitingValue(Double[][][] values) {
374         if (mProperty == null)
375             return false;
376
377         if (!checkRange(values.length))
378             return false;
379
380         if (mProperty.getChildProperty() != null) {
381             AttributeValueValidation rangeValidation = new AttributeValueValidation(
382                     mProperty.getChildProperty());
383             for (Double[][] value : values) {
384                 if (rangeValidation.visitingValue(value) == false)
385                     return false;
386             }
387         }
388
389         return true;
390     }
391
392     /**
393      * API to validate a 3D array of boolean values.
394      *
395      * @return True if the given value is valid, otherwise false.
396      */
397     @Override
398     public Boolean visitingValue(Boolean[][][] values) {
399         if (mProperty == null)
400             return false;
401
402         if (!checkRange(values.length))
403             return false;
404
405         if (mProperty.getChildProperty() != null) {
406             AttributeValueValidation rangeValidation = new AttributeValueValidation(
407                     mProperty.getChildProperty());
408             for (Boolean[][] value : values) {
409                 if (rangeValidation.visitingValue(value) == false)
410                     return false;
411             }
412         }
413
414         return true;
415     }
416
417     /**
418      * API to validate a 3D array of string values.
419      *
420      * @return True if the given value is valid, otherwise false.
421      */
422     @Override
423     public Boolean visitingValue(String[][][] values) {
424         if (mProperty == null)
425             return false;
426
427         if (!checkRange(values.length))
428             return false;
429
430         if (mProperty.getChildProperty() != null) {
431             AttributeValueValidation rangeValidation = new AttributeValueValidation(
432                     mProperty.getChildProperty());
433             for (String[][] value : values) {
434                 if (rangeValidation.visitingValue(value) == false)
435                     return false;
436             }
437         }
438
439         return true;
440     }
441
442     /**
443      * API to validate a 3D array of {@link SimulatorResourceModel} values.
444      *
445      * @return True if the given value is valid, otherwise false.
446      */
447     @Override
448     public Boolean visitingValue(SimulatorResourceModel[][][] value) {
449         return false;
450     }
451
452     private boolean checkRange(double value) {
453         if (AttributeProperty.Type.RANGE == mProperty.type()
454                 && (value >= mProperty.min() && value <= mProperty.max())) {
455             return true;
456         }
457
458         return false;
459     }
460
461     private <T> boolean checkValueSet(T value) {
462         if (AttributeProperty.Type.VALUESET == mProperty.type()
463                 && null != mProperty.valueSet()) {
464             for (AttributeValue allowedValue : mProperty.valueSet()) {
465                 if (allowedValue.get().equals(value))
466                     return true;
467             }
468         }
469
470         return false;
471     }
472 }