04c2de5c8d11e517363d469d02624b9b69edcbcb
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / SimulatorResourceModel.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 /**
23  * This class represents the resource model of a resource.
24  */
25 public class SimulatorResourceModel {
26
27     private Map<String, AttributeValue>    mValues     = null;
28     private Map<String, AttributeProperty> mProperties = null;
29
30     /**
31      * Constructs new {@SimulatorResourceModel} object.
32      */
33     public SimulatorResourceModel() {
34         mValues = new HashMap<>();
35         mProperties = new HashMap<>();
36     }
37
38     /**
39      * API to add an attribute to resource model.
40      *
41      * @param attrName
42      *            Name of the attribute.
43      * @param value
44      *            Value of the attribute.
45      *
46      * @throws InvalidArgsException
47      *             This exception will be thrown on invalid input.
48      */
49     public void addAttribute(String attrName, AttributeValue value)
50             throws InvalidArgsException {
51         if (null == attrName || attrName.isEmpty())
52             throw new InvalidArgsException("Invalid attribute name!");
53
54         if (null == value)
55             throw new InvalidArgsException("Attribute value is null!");
56
57         mValues.put(attrName, value);
58     }
59
60     /**
61      * API to add an attribute to resource model.
62      *
63      * @param attribute
64      *            {@link SimulatorResourceAttribute} to be add to resource
65      *            model.
66      *
67      * @throws InvalidArgsException
68      *             This exception will be thrown on invalid input.
69      */
70     public void addAttribute(SimulatorResourceAttribute attribute)
71             throws InvalidArgsException {
72         if (null == attribute || null == attribute.name()
73                 || attribute.name().isEmpty() || null == attribute.value())
74             throw new InvalidArgsException("Invalid attribute!");
75
76         mValues.put(attribute.name(), attribute.value());
77     }
78
79     /**
80      * API to set attribute's property.
81      *
82      * @param attrName
83      *            Name of the attribute.
84      * @param property
85      *            {@link AttributeProperty} to be set for attribute.
86      *
87      * @throws InvalidArgsException
88      *             This exception will be thrown on invalid input.
89      */
90     public void setAttributeProperty(String attrName, AttributeProperty property)
91             throws InvalidArgsException {
92         if (null == attrName || attrName.isEmpty())
93             throw new InvalidArgsException("Invalid attribute!");
94
95         if (null == property)
96             throw new InvalidArgsException("Invalid attribute property!");
97
98         mProperties.put(attrName, property);
99     }
100
101     /**
102      * API to set attribute's value.
103      *
104      * @param attrName
105      *            Name of the attribute.
106      * @param value
107      *            {@link AttributeValue} to be set for attribute.
108      *
109      * @throws InvalidArgsException
110      *             This exception will be thrown on invalid input.
111      */
112     public void setAttributeValue(String attrName, AttributeValue value)
113             throws InvalidArgsException {
114         if (null == attrName || attrName.isEmpty())
115             throw new InvalidArgsException("Invalid attribute name!");
116
117         if (null == value)
118             throw new InvalidArgsException("Attribute value is null!");
119
120         mValues.put(attrName, value);
121     }
122
123     /**
124      * API to get all the attributes of resource model.
125      *
126      * @return Map of attributes with attribute name as key and its
127      *         corresponding {@link SimulatorResourceAttribute} as value.
128      */
129     public Map<String, SimulatorResourceAttribute> getAttributes() {
130         if (mValues.size() == 0)
131             return null;
132
133         Map<String, SimulatorResourceAttribute> attributes = new HashMap<>();
134         for (Map.Entry<String, AttributeValue> entry : mValues.entrySet()) {
135             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
136                     entry.getKey(), entry.getValue(), mProperties.get(entry
137                             .getKey()));
138             attributes.put(entry.getKey(), attribute);
139         }
140
141         return attributes;
142     }
143
144     /**
145      * API to get attribute by name.
146      *
147      * @param attrName
148      *            Name of the attribute.
149      *
150      * @return {@link SimulatorResourceAttribute}.
151      */
152     public SimulatorResourceAttribute getAttribute(String attrName) {
153         if (mValues.containsKey(attrName)) {
154             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
155                     attrName, mValues.get(attrName), mProperties.get(attrName));
156             return attribute;
157         }
158
159         return null;
160     }
161
162     /**
163      * API to check whether resource model has attribute.
164      *
165      * @param attrName
166      *            Name of the attribute.
167      *
168      * @return true if resource model has an attribute with given name,
169      *         otherwise false.
170      */
171     public boolean containsAttribute(String attrName) {
172         if (mValues.containsKey(attrName))
173             return true;
174         return false;
175     }
176
177     /**
178      * API to get value type information of attribute.
179      *
180      * @param attrName
181      *            Name of the attribute.
182      *
183      * @return Attribute value type information         {@AttributeValue.TypeInfo
184      * }.
185      */
186     public AttributeValue.TypeInfo getAttributeType(String attrName) {
187         if (mValues.containsKey(attrName))
188             return mValues.get(attrName).typeInfo();
189         return null;
190     }
191
192     /**
193      * API to remove attribute from resource model.
194      *
195      * @param attrName
196      *            Name of the attribute.
197      *
198      * @return true if resource model has attribute it is removed, otherwise
199      *         false.
200      */
201     public boolean removeAttribute(String attrName) {
202         if (mValues.containsKey(attrName)) {
203             mValues.remove(attrName);
204             if (mProperties.containsKey(attrName))
205                 mProperties.remove(attrName);
206             return true;
207         }
208
209         return false;
210     }
211
212     /**
213      * API to get number of attributes present in resource model.
214      *
215      * @return Number of attributes present in resource model.
216      */
217     public int size() {
218         return mValues.size();
219     }
220
221     // Methods used in native code
222     private SimulatorResourceModel(Map<String, AttributeValue> values,
223             Map<String, AttributeProperty> properties) {
224         mValues = values;
225         mProperties = properties;
226     }
227 }