Unit tests for android extension of resource contianer
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / java / org / iotivity / service / resourcecontainer / RcsResourceAttributes.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 package org.iotivity.service.resourcecontainer;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29
30 //import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
31
32 /**
33  *
34  * This class represents the attributes for a resource.
35  *
36  * @see RcsValue
37  */
38 public final class RcsResourceAttributes 
39 {
40
41     private final Map<String, RcsValue> mCache = new HashMap<>();
42
43     public RcsResourceAttributes() {
44     }
45
46     
47     public RcsResourceAttributes(RcsResourceAttributes attrs){
48         for (final String key : attrs.keySet()) {
49             mCache.put(key, attrs.get(key));
50         }
51     }
52
53     /**
54      * Returns a unmodifiable Set view of the keys contained in this attributes.
55      *
56      * @return an unmodifiable set view of the keys in this attributes
57      */
58     public Set<String> keySet() {
59         return Collections.unmodifiableSet(mCache.keySet());
60     }
61
62     /**
63      * Returns the value to which the specified key is mapped, or null if this
64      * contains no mapping for the key.
65      *
66      * @param key
67      *            the key whose associated value is to be returned
68      *
69      * @return the value to which the specified key is mapped, or null if this
70      *         contains no mapping for the key
71      *
72      * @throws NullPointerException
73      *             if key is null
74      */
75     public RcsValue get(String key) {
76         if (key == null) throw new NullPointerException("key is null");
77         
78         return mCache.get(key);
79     }
80
81     /**
82      * Sets the specified value with the specified key.
83      * If the object previously contained a mapping for the key, the old value
84      * is replaced by the specified value.
85      *
86      * @param key
87      *            key with which the specified value is to be associated
88      *
89      * @param value
90      *            value to be associated with the specified key
91      *
92      * @throws NullPointerException
93      *             if key or value is null
94      *
95      */
96     public void put(String key, RcsValue value) {
97         if (key == null) throw new NullPointerException("key is null");
98         if (value == null) throw new NullPointerException("value is null");
99
100         mCache.put(key, value);
101     }
102
103     /**
104      * Sets the specified value with the specified key.
105      * If the object previously contained a mapping for the key, the old value
106      * is replaced by the specified value.
107      *
108      * @param key
109      *            key with which the specified value is to be associated
110      *
111      * @param value
112      *            value to be associated with the specified key
113      *
114      * @throws NullPointerException
115      *             if key or value is null
116      * @throws IllegalArgumentException
117      *             if object is not supported type by {@link RcsValue}
118      */
119     public void put(String key, Object object) {
120         if (key == null) throw new NullPointerException("key is null");
121
122         put(key, new RcsValue(object));
123     }
124
125     /**
126      * Returns true if this contains no key-value mappings.
127      *
128      * @return true if this contains no key-value mappings
129      */
130     public boolean isEmpty() {
131         return mCache.isEmpty();
132     }
133
134     /**
135      * Returns the number of key-value mappings.
136      *
137      * @return the number of key-value mappings
138      */
139     public int size() {
140         return mCache.size();
141     }
142
143     /**
144      * Removes the mapping for a key from this attributes if it is present.
145      *
146      * @param key
147      *            key whose mapping is to be removed
148      *
149      * @return true if the key is present and the the value mapped is removed.
150      */
151     public boolean remove(String key) {
152         if (key == null) throw new NullPointerException("key is null");
153       
154         final boolean cacheRemove = mCache.remove(key) != null;      
155
156         return cacheRemove;
157     }
158
159     /**
160      * Removes all of the mappings.
161      */
162     public void clear(){
163         mCache.clear();
164     }
165
166     /**
167      * Returns true if this contains a mapping for the specified key.
168      *
169      * @param key
170      *            key whose presence is to be tested
171      *
172      * @return true if this contains a mapping for the specified key.
173      *
174      * @throws NullPointerException
175      *             if key is null
176      */
177     public boolean contains(String key) {
178         if (key == null) throw new NullPointerException("key is null");
179
180         return mCache.containsKey(key);
181     }
182     
183
184     @Override
185     public boolean equals(Object o) {
186         if (o == this) return true;
187         if (!(o instanceof RcsResourceAttributes)) return false;        
188         RcsResourceAttributes rhs = (RcsResourceAttributes) o;
189         return mCache.equals(rhs.mCache);
190     }
191
192     @Override
193     public int hashCode() {
194         return mCache.hashCode();
195     }
196     
197     /**
198      * Updates all properties provided as parameter.
199      */
200     public void put(RcsResourceAttributes attrs){
201         for (final String key : attrs.keySet()) {
202             mCache.put(key, attrs.get(key));
203         }
204     }
205
206 }