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;
19 import java.util.HashMap;
23 * This class represents the resource model of a resource. A resource model
24 * contains a set of attributes and their properties. It represents the complex
25 * value type of an attribute.
27 public class SimulatorResourceModel {
29 private Map<String, AttributeValue> mValues = null;
30 private Map<String, AttributeProperty> mProperties = null;
33 * Constructs new {@link SimulatorResourceModel} object.
35 public SimulatorResourceModel() {
36 mValues = new HashMap<>();
37 mProperties = new HashMap<>();
41 * API to add an attribute to resource model.
44 * Name of the attribute.
46 * Value of the attribute.
48 * @throws InvalidArgsException
49 * This exception will be thrown on invalid input.
51 public void addAttribute(String attrName, AttributeValue value)
52 throws InvalidArgsException {
53 if (null == attrName || attrName.isEmpty())
54 throw new InvalidArgsException("Invalid attribute name!");
57 throw new InvalidArgsException("Attribute value is null!");
59 mValues.put(attrName, value);
63 * API to add an attribute to resource model.
66 * {@link SimulatorResourceAttribute} to be added to resource
69 * @throws InvalidArgsException
70 * This exception will be thrown on invalid input.
72 public void addAttribute(SimulatorResourceAttribute attribute)
73 throws InvalidArgsException {
74 if (null == attribute || null == attribute.name()
75 || attribute.name().isEmpty() || null == attribute.value())
76 throw new InvalidArgsException("Invalid attribute!");
78 mValues.put(attribute.name(), attribute.value());
82 * API to set attribute's property.
85 * Name of the attribute.
87 * {@link AttributeProperty} to be set for attribute.
89 * @throws InvalidArgsException
90 * This exception will be thrown on invalid input.
92 public void setAttributeProperty(String attrName, AttributeProperty property)
93 throws InvalidArgsException {
94 if (null == attrName || attrName.isEmpty())
95 throw new InvalidArgsException("Invalid attribute!");
98 throw new InvalidArgsException("Invalid attribute property!");
100 mProperties.put(attrName, property);
104 * API to set attribute's value.
107 * Name of the attribute.
109 * {@link AttributeValue} to be set for attribute.
111 * @throws InvalidArgsException
112 * This exception will be thrown on invalid input.
114 public void setAttributeValue(String attrName, AttributeValue value)
115 throws InvalidArgsException {
116 if (null == attrName || attrName.isEmpty())
117 throw new InvalidArgsException("Invalid attribute name!");
120 throw new InvalidArgsException("Attribute value is null!");
122 mValues.put(attrName, value);
126 * API to get all the attributes of resource model.
128 * @return Map of attributes with attribute name as key and its
129 * corresponding {@link SimulatorResourceAttribute} as value.
131 public Map<String, SimulatorResourceAttribute> getAttributes() {
132 Map<String, SimulatorResourceAttribute> attributes = new HashMap<>();
133 for (Map.Entry<String, AttributeValue> entry : mValues.entrySet()) {
134 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
135 entry.getKey(), entry.getValue(), mProperties.get(entry
137 attributes.put(entry.getKey(), attribute);
144 * API to get attribute by name.
147 * Name of the attribute.
149 * @return {@link SimulatorResourceAttribute}.
151 public SimulatorResourceAttribute getAttribute(String attrName) {
152 if (mValues.containsKey(attrName)) {
153 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
154 attrName, mValues.get(attrName), mProperties.get(attrName));
162 * API to check whether resource model has an attribute with given name.
165 * Name of the attribute.
167 * @return true if resource model has an attribute with given name,
170 public boolean containsAttribute(String attrName) {
171 if (mValues.containsKey(attrName))
177 * API to get value type information of attribute.
180 * Name of the attribute.
182 * @return Attribute value type information {@link AttributeValue.TypeInfo
186 public AttributeValue.TypeInfo getAttributeType(String attrName) {
187 if (mValues.containsKey(attrName))
188 return mValues.get(attrName).typeInfo();
193 * API to remove attribute from resource model.
196 * Name of the attribute.
198 * @return true if resource model has an attribute with the given name,
201 public boolean removeAttribute(String attrName) {
202 if (mValues.containsKey(attrName)) {
203 mValues.remove(attrName);
204 if (mProperties.containsKey(attrName))
205 mProperties.remove(attrName);
213 * API to get number of attributes present in resource model.
215 * @return Number of attributes present in resource model.
218 return mValues.size();
222 * API to update the attribute values from given
223 * {@link SimulatorResourceModel}.
225 * @param resourceModel
226 * {@link SimulatorResourceModel} holding the new attribute
229 public void update(SimulatorResourceModel resourceModel) {
230 if (null == resourceModel || 0 == resourceModel.size())
233 for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
234 .getAttributes().entrySet()) {
235 SimulatorResourceAttribute newAttribute = entry.getValue();
236 SimulatorResourceAttribute attribute = getAttribute(entry.getKey());
237 if (null != newAttribute && null != attribute) {
238 if (null != attribute.property()) {
239 AttributeValueValidation validation = new AttributeValueValidation(
240 attribute.property());
241 if (!validation.validate(newAttribute.value())) {
242 mValues.put(entry.getKey(), newAttribute.value());
245 mValues.put(entry.getKey(), newAttribute.value());
251 private SimulatorResourceModel(Map<String, AttributeValue> values,
252 Map<String, AttributeProperty> properties) {
254 mProperties = properties;