2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.oic.simulator;
20 * This class holds a set of methods for validating the attribute values of
21 * different types using the given value properties {@link AttributeProperty}.
23 public class AttributeValueValidation implements
24 AttributeValueVisitor.VisitingMethods<Boolean> {
25 private AttributeProperty mProperty = null;
28 * Constructs {@link AttributeValueValidation} with the given attribute
32 * Attribute value property.
34 public AttributeValueValidation(AttributeProperty property) {
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.
43 * {@link AttributeValue} to be validated.
44 * @return True if the given value is valid, otherwise false.
46 public boolean validate(AttributeValue value) {
47 AttributeValueVisitor visitor = new AttributeValueVisitor(value, this);
48 Boolean result = (Boolean) visitor.visit();
49 return result.booleanValue();
53 * API to validate an Integer value.
55 * @return True if the given value is valid, otherwise false.
58 public Boolean visitingValue(Integer value) {
59 if (mProperty == null)
62 if (checkRange(value.doubleValue()) || checkValueSet(value))
68 * API to validate a Double value.
70 * @return True if the given value is valid, otherwise false.
73 public Boolean visitingValue(Double value) {
74 if (mProperty == null)
77 if (checkRange(value.doubleValue()) || checkValueSet(value))
83 * API to validate a Boolean value.
85 * @return True if the given value is valid, otherwise false.
88 public Boolean visitingValue(Boolean value) {
89 if (mProperty == null)
92 if (checkValueSet(value))
98 * API to validate a String value.
100 * @return True if the given value is valid, otherwise false.
103 public Boolean visitingValue(String value) {
104 if (mProperty == null)
107 if (checkRange(value.length()) || checkValueSet(value))
113 * API to validate a {@link SimulatorResourceModel} value.
115 * @return True if the given value is valid, otherwise false.
118 public Boolean visitingValue(SimulatorResourceModel value) {
123 * API to validate an array of integer values.
125 * @return True if the given value is valid, otherwise false.
128 public Boolean visitingValue(Integer[] values) {
129 if (mProperty == null)
132 if (!checkRange(values.length))
135 if (mProperty.getChildProperty() != null) {
136 AttributeValueValidation rangeValidation = new AttributeValueValidation(
137 mProperty.getChildProperty());
138 for (Integer value : values) {
139 if (rangeValidation.visitingValue(value) == false)
148 * API to validate an array of double values.
150 * @return True if the given value is valid, otherwise false.
153 public Boolean visitingValue(Double[] values) {
154 if (mProperty == null)
157 if (!checkRange(values.length))
160 if (mProperty.getChildProperty() != null) {
161 AttributeValueValidation rangeValidation = new AttributeValueValidation(
162 mProperty.getChildProperty());
163 for (Double value : values) {
164 if (rangeValidation.visitingValue(value) == false)
173 * API to validate an array of boolean values.
175 * @return True if the given value is valid, otherwise false.
178 public Boolean visitingValue(Boolean[] values) {
179 if (mProperty == null)
182 if (!checkRange(values.length))
185 if (mProperty.getChildProperty() != null) {
186 AttributeValueValidation rangeValidation = new AttributeValueValidation(
187 mProperty.getChildProperty());
188 for (Boolean value : values) {
189 if (rangeValidation.visitingValue(value) == false)
198 * API to validate an array of string values.
200 * @return True if the given value is valid, otherwise false.
203 public Boolean visitingValue(String[] values) {
204 if (mProperty == null)
207 if (!checkRange(values.length))
210 if (mProperty.getChildProperty() != null) {
211 AttributeValueValidation rangeValidation = new AttributeValueValidation(
212 mProperty.getChildProperty());
213 for (String value : values) {
214 if (rangeValidation.visitingValue(value) == false)
223 * API to validate an array of {@link SimulatorResourceModel} values.
225 * @return True if the given value is valid, otherwise false.
228 public Boolean visitingValue(SimulatorResourceModel[] value) {
233 * API to validate a 2D array of integer values.
235 * @return True if the given value is valid, otherwise false.
238 public Boolean visitingValue(Integer[][] values) {
239 if (mProperty == null)
242 if (!checkRange(values.length))
245 if (mProperty.getChildProperty() != null) {
246 AttributeValueValidation rangeValidation = new AttributeValueValidation(
247 mProperty.getChildProperty());
248 for (Integer[] value : values) {
249 if (rangeValidation.visitingValue(value) == false)
258 * API to validate a 2D array of double values.
260 * @return True if the given value is valid, otherwise false.
263 public Boolean visitingValue(Double[][] values) {
264 if (mProperty == null)
267 if (!checkRange(values.length))
270 if (mProperty.getChildProperty() != null) {
271 AttributeValueValidation rangeValidation = new AttributeValueValidation(
272 mProperty.getChildProperty());
273 for (Double[] value : values) {
274 if (rangeValidation.visitingValue(value) == false)
283 * API to validate a 2D array of boolean values.
285 * @return True if the given value is valid, otherwise false.
288 public Boolean visitingValue(Boolean[][] values) {
289 if (mProperty == null)
292 if (!checkRange(values.length))
295 if (mProperty.getChildProperty() != null) {
296 AttributeValueValidation rangeValidation = new AttributeValueValidation(
297 mProperty.getChildProperty());
298 for (Boolean[] value : values) {
299 if (rangeValidation.visitingValue(value) == false)
308 * API to validate a 2D array of string values.
310 * @return True if the given value is valid, otherwise false.
313 public Boolean visitingValue(String[][] values) {
314 if (mProperty == null)
317 if (!checkRange(values.length))
320 if (mProperty.getChildProperty() != null) {
321 AttributeValueValidation rangeValidation = new AttributeValueValidation(
322 mProperty.getChildProperty());
323 for (String[] value : values) {
324 if (rangeValidation.visitingValue(value) == false)
333 * API to validate a 2D array of {@link SimulatorResourceModel} values.
335 * @return True if the given value is valid, otherwise false.
338 public Boolean visitingValue(SimulatorResourceModel[][] value) {
343 * API to validate a 3D array of integer values.
345 * @return True if the given value is valid, otherwise false.
348 public Boolean visitingValue(Integer[][][] values) {
349 if (mProperty == null)
352 if (!checkRange(values.length))
355 if (mProperty.getChildProperty() != null) {
356 AttributeValueValidation rangeValidation = new AttributeValueValidation(
357 mProperty.getChildProperty());
358 for (Integer[][] value : values) {
359 if (rangeValidation.visitingValue(value) == false)
368 * API to validate a 3D array of double values.
370 * @return True if the given value is valid, otherwise false.
373 public Boolean visitingValue(Double[][][] values) {
374 if (mProperty == null)
377 if (!checkRange(values.length))
380 if (mProperty.getChildProperty() != null) {
381 AttributeValueValidation rangeValidation = new AttributeValueValidation(
382 mProperty.getChildProperty());
383 for (Double[][] value : values) {
384 if (rangeValidation.visitingValue(value) == false)
393 * API to validate a 3D array of boolean values.
395 * @return True if the given value is valid, otherwise false.
398 public Boolean visitingValue(Boolean[][][] values) {
399 if (mProperty == null)
402 if (!checkRange(values.length))
405 if (mProperty.getChildProperty() != null) {
406 AttributeValueValidation rangeValidation = new AttributeValueValidation(
407 mProperty.getChildProperty());
408 for (Boolean[][] value : values) {
409 if (rangeValidation.visitingValue(value) == false)
418 * API to validate a 3D array of string values.
420 * @return True if the given value is valid, otherwise false.
423 public Boolean visitingValue(String[][][] values) {
424 if (mProperty == null)
427 if (!checkRange(values.length))
430 if (mProperty.getChildProperty() != null) {
431 AttributeValueValidation rangeValidation = new AttributeValueValidation(
432 mProperty.getChildProperty());
433 for (String[][] value : values) {
434 if (rangeValidation.visitingValue(value) == false)
443 * API to validate a 3D array of {@link SimulatorResourceModel} values.
445 * @return True if the given value is valid, otherwise false.
448 public Boolean visitingValue(SimulatorResourceModel[][][] value) {
452 private boolean checkRange(double value) {
453 if (AttributeProperty.Type.RANGE == mProperty.type()
454 && (value >= mProperty.min() && value <= mProperty.max())) {
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))