Java API implementation for changes in simulator resource model.
[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. A resource model
24  * contains a set of attributes. It also represents the complex value type of an
25  * attribute.
26  */
27 public class SimulatorResourceModel {
28
29     private Map<String, AttributeValue> mValues = null;
30
31     /**
32      * Constructs new {@link SimulatorResourceModel} object.
33      */
34     public SimulatorResourceModel() {
35         mValues = new HashMap<>();
36     }
37
38     /**
39      * API to set an attribute to resource model. If an attribute exists with
40      * the given name, then it overwrites the existing value. Otherwise creates
41      * a new attribute.
42      *
43      * @param attrName
44      *            Name of the attribute.
45      * @param value
46      *            Value of the attribute.
47      *
48      * @throws InvalidArgsException
49      *             This exception will be thrown on invalid input.
50      */
51     public void set(String attrName, AttributeValue value)
52             throws InvalidArgsException {
53         if (null == attrName || attrName.isEmpty())
54             throw new InvalidArgsException("Invalid attribute name!");
55
56         if (null == value)
57             throw new InvalidArgsException("Attribute value is null!");
58
59         mValues.put(attrName, value);
60     }
61
62     /**
63      * API to remove attribute from resource model.
64      *
65      * @param attrName
66      *            Name of the attribute.
67      *
68      * @return true if resource model has an attribute with the given name,
69      *         otherwise false.
70      */
71     public boolean remove(String attrName) {
72         if (mValues.containsKey(attrName)) {
73             mValues.remove(attrName);
74             return true;
75         }
76
77         return false;
78     }
79
80     /**
81      * API to get attribute by name.
82      *
83      * @param attrName
84      *            Name of the attribute.
85      *
86      * @return {@link SimulatorResourceAttribute}.
87      */
88     public AttributeValue get(String attrName) {
89         if (mValues.containsKey(attrName)) {
90             return mValues.get(attrName);
91         }
92
93         return null;
94     }
95
96     /**
97      * API to get all attributes of the model.
98      *
99      * @return A map of {@link AttributeValue} objects with attribute name as
100      *         the key.
101      */
102     public Map<String, AttributeValue> get() {
103         return mValues;
104     }
105
106     /**
107      * API to check whether resource model has an attribute with given name.
108      *
109      * @param attrName
110      *            Name of the attribute.
111      *
112      * @return true if resource model has an attribute with given name,
113      *         otherwise false.
114      */
115     public boolean contains(String attrName) {
116         if (mValues.containsKey(attrName))
117             return true;
118         return false;
119     }
120
121     /**
122      * API to get value type information of attribute.
123      *
124      * @param attrName
125      *            Name of the attribute.
126      *
127      * @return Attribute value type information {@link AttributeValue.TypeInfo
128      *
129      * }.
130      */
131     public AttributeValue.TypeInfo getType(String attrName) {
132         if (mValues.containsKey(attrName))
133             return mValues.get(attrName).typeInfo();
134         return null;
135     }
136
137     /**
138      * API to get number of attributes present in resource model.
139      *
140      * @return Number of attributes present in resource model.
141      */
142     public int size() {
143         return mValues.size();
144     }
145
146     /**
147      * These methods used by native layer.
148      */
149     @SuppressWarnings("unused")
150     private SimulatorResourceModel(Map<String, AttributeValue> values) {
151         mValues = values;
152     }
153 }