Java API implementation for changes in simulator resource model.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / ModelProperty.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 import java.util.HashMap;
20 import java.util.Map;
21
22 public class ModelProperty extends AttributeProperty {
23
24     private Map<String, Boolean>           mRequiredAttributes;
25     private Map<String, AttributeProperty> mChildProperties;
26
27     ModelProperty() {
28         super(Type.MODEL);
29         mRequiredAttributes = new HashMap<>();
30         mChildProperties = new HashMap<>();
31     }
32
33     @Override
34     public boolean isModel() {
35         return true;
36     }
37
38     @Override
39     public ModelProperty asModel() {
40         return this;
41     }
42
43     public boolean add(String name, AttributeProperty property, boolean required) {
44         if (null == name || null == property) {
45             return false;
46         }
47
48         mChildProperties.put(name, property);
49         mRequiredAttributes.put(name, required);
50         return true;
51     }
52
53     public void remove(String name) {
54         mChildProperties.remove(name);
55         mRequiredAttributes.remove(name);
56     }
57
58     public AttributeProperty get(String name) {
59         return mChildProperties.get(name);
60     }
61
62     public Map<String, AttributeProperty> getChildProperties() {
63         return mChildProperties;
64     }
65
66     public boolean isRequired(String name) {
67         return mRequiredAttributes.get(name);
68     }
69
70     @Override
71     public boolean validate(AttributeValue value) {
72         return new ModelValueValidator(this).validate(value);
73     }
74
75     public boolean validate(SimulatorResourceModel model) {
76         for (Map.Entry<String, AttributeValue> attributeEntry : model.get()
77                 .entrySet()) {
78             AttributeProperty childProperty = get(attributeEntry.getKey());
79             if (null != childProperty
80                     && !childProperty.validate(attributeEntry.getValue())) {
81                 return false;
82             }
83         }
84
85         return true;
86     }
87 }