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.
25 public class SimulatorResourceModel {
27 private Map<String, AttributeValue> mValues = null;
28 private Map<String, AttributeProperty> mProperties = null;
31 * Constructs new {@SimulatorResourceModel} object.
33 public SimulatorResourceModel() {
34 mValues = new HashMap<>();
35 mProperties = new HashMap<>();
39 * API to add an attribute to resource model.
42 * Name of the attribute.
44 * Value of the attribute.
46 * @throws InvalidArgsException
47 * This exception will be thrown on invalid input.
49 public void addAttribute(String attrName, AttributeValue value)
50 throws InvalidArgsException {
51 if (null == attrName || attrName.isEmpty())
52 throw new InvalidArgsException("Invalid attribute name!");
55 throw new InvalidArgsException("Attribute value is null!");
57 mValues.put(attrName, value);
61 * API to add an attribute to resource model.
64 * {@link SimulatorResourceAttribute} to be add to resource
67 * @throws InvalidArgsException
68 * This exception will be thrown on invalid input.
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!");
76 mValues.put(attribute.name(), attribute.value());
80 * API to set attribute's property.
83 * Name of the attribute.
85 * {@link AttributeProperty} to be set for attribute.
87 * @throws InvalidArgsException
88 * This exception will be thrown on invalid input.
90 public void setAttributeProperty(String attrName, AttributeProperty property)
91 throws InvalidArgsException {
92 if (null == attrName || attrName.isEmpty())
93 throw new InvalidArgsException("Invalid attribute!");
96 throw new InvalidArgsException("Invalid attribute property!");
98 mProperties.put(attrName, property);
102 * API to set attribute's value.
105 * Name of the attribute.
107 * {@link AttributeValue} to be set for attribute.
109 * @throws InvalidArgsException
110 * This exception will be thrown on invalid input.
112 public void setAttributeValue(String attrName, AttributeValue value)
113 throws InvalidArgsException {
114 if (null == attrName || attrName.isEmpty())
115 throw new InvalidArgsException("Invalid attribute name!");
118 throw new InvalidArgsException("Attribute value is null!");
120 mValues.put(attrName, value);
124 * API to get all the attributes of resource model.
126 * @return Map of attributes with attribute name as key and its
127 * corresponding {@link SimulatorResourceAttribute} as value.
129 public Map<String, SimulatorResourceAttribute> getAttributes() {
130 if (mValues.size() == 0)
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
138 attributes.put(entry.getKey(), attribute);
145 * API to get attribute by name.
148 * Name of the attribute.
150 * @return {@link SimulatorResourceAttribute}.
152 public SimulatorResourceAttribute getAttribute(String attrName) {
153 if (mValues.containsKey(attrName)) {
154 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
155 attrName, mValues.get(attrName), mProperties.get(attrName));
163 * API to check whether resource model has attribute.
166 * Name of the attribute.
168 * @return true if resource model has an attribute with given name,
171 public boolean containsAttribute(String attrName) {
172 if (mValues.containsKey(attrName))
178 * API to get value type information of attribute.
181 * Name of the attribute.
183 * @return Attribute value type information {@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 attribute it is removed, otherwise
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();
221 // Methods used in native code
222 private SimulatorResourceModel(Map<String, AttributeValue> values,
223 Map<String, AttributeProperty> properties) {
225 mProperties = properties;