219a9a6510de61a61542577aad24168d547b4378
[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 package org.iotivity.service.resourcecontainer;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
30
31 /**
32  *
33  * This class represents the attributes for a resource.
34  *
35  * @see RcsValue
36  */
37 public final class RcsResourceAttributes extends RcsObject {
38
39     private native boolean nativeIsEmpty();
40
41     private native int nativeSize();
42
43     private native boolean nativeRemove(String key);
44
45     private native void nativeClear();
46
47     private native boolean nativeContains(String key);
48
49     private native void nativeAddKeys(Set<String> set);
50
51     private native RcsValue nativeExtract(String key);
52
53     private native void nativeExtractAll(Map<String, RcsValue> map);
54
55     private final Map<String, RcsValue> mCache = new HashMap<>();
56
57     public RcsResourceAttributes() {
58     }
59
60     public RcsResourceAttributes(RcsLockedAttributes lockedAttrs)
61             throws RcsException {
62         for (final String key : lockedAttrs.keySet()) {
63             mCache.put(key, lockedAttrs.get(key));
64         }
65     }
66     
67     public RcsResourceAttributes(RcsResourceAttributes attrs){
68         for (final String key : attrs.keySet()) {
69             mCache.put(key, attrs.get(key));
70         }
71     }
72
73     /**
74      * Returns a unmodifiable Set view of the keys contained in this attributes.
75      *
76      * @return an unmodifiable set view of the keys in this attributes
77      */
78     public Set<String> keySet() {
79         if (hasHandle()) {
80             final Set<String> keySet = new HashSet<>(mCache.keySet());
81
82             nativeAddKeys(keySet);
83
84             return Collections.unmodifiableSet(keySet);
85         }
86
87         return Collections.unmodifiableSet(mCache.keySet());
88     }
89
90     /**
91      * Returns the value to which the specified key is mapped, or null if this
92      * contains no mapping for the key.
93      *
94      * @param key
95      *            the key whose associated value is to be returned
96      *
97      * @return the value to which the specified key is mapped, or null if this
98      *         contains no mapping for the key
99      *
100      * @throws NullPointerException
101      *             if key is null
102      */
103     public RcsValue get(String key) {
104         if (key == null) throw new NullPointerException("key is null");
105
106         if (!mCache.containsKey(key) && hasHandle() && nativeContains(key)) {
107             mCache.put(key, nativeExtract(key));
108         }
109         return mCache.get(key);
110     }
111
112     /**
113      * Sets the specified value with the specified key.
114      * If the object previously contained a mapping for the key, the old value
115      * is replaced by the specified value.
116      *
117      * @param key
118      *            key with which the specified value is to be associated
119      *
120      * @param value
121      *            value to be associated with the specified key
122      *
123      * @throws NullPointerException
124      *             if key or value is null
125      *
126      */
127     public void put(String key, RcsValue value) {
128         if (key == null) throw new NullPointerException("key is null");
129         if (value == null) throw new NullPointerException("value is null");
130
131         mCache.put(key, value);
132         if (hasHandle()) nativeRemove(key);
133     }
134
135     /**
136      * Sets the specified value with the specified key.
137      * If the object previously contained a mapping for the key, the old value
138      * is replaced by the specified value.
139      *
140      * @param key
141      *            key with which the specified value is to be associated
142      *
143      * @param value
144      *            value to be associated with the specified key
145      *
146      * @throws NullPointerException
147      *             if key or value is null
148      * @throws IllegalArgumentException
149      *             if object is not supported type by {@link RcsValue}
150      */
151     public void put(String key, Object object) {
152         if (key == null) throw new NullPointerException("key is null");
153
154         put(key, new RcsValue(object));
155     }
156
157     /**
158      * Returns true if this contains no key-value mappings.
159      *
160      * @return true if this contains no key-value mappings
161      */
162     public boolean isEmpty() {
163         return mCache.isEmpty() && (!hasHandle() || nativeIsEmpty());
164     }
165
166     /**
167      * Returns the number of key-value mappings.
168      *
169      * @return the number of key-value mappings
170      */
171     public int size() {
172         if (hasHandle()) return mCache.size() + nativeSize();
173         return mCache.size();
174     }
175
176     /**
177      * Removes the mapping for a key from this attributes if it is present.
178      *
179      * @param key
180      *            key whose mapping is to be removed
181      *
182      * @return true if the key is present and the the value mapped is removed.
183      */
184     public boolean remove(String key) {
185         if (key == null) throw new NullPointerException("key is null");
186
187         // XXX make sure both cache and native values to be removed.
188         final boolean cacheRemove = mCache.remove(key) != null;
189         final boolean nativeRemove = hasHandle() && nativeRemove(key);
190
191         return cacheRemove || nativeRemove;
192     }
193
194     /**
195      * Removes all of the mappings.
196      */
197     public void clear() {
198         mCache.clear();
199         nativeClear();
200     }
201
202     /**
203      * Returns true if this contains a mapping for the specified key.
204      *
205      * @param key
206      *            key whose presence is to be tested
207      *
208      * @return true if this contains a mapping for the specified key.
209      *
210      * @throws NullPointerException
211      *             if key is null
212      */
213     public boolean contains(String key) {
214         if (key == null) throw new NullPointerException("key is null");
215
216         return mCache.containsKey(key) || nativeContains(key);
217     }
218
219     private void esnureAllExtracted() {
220         if (hasHandle()) nativeExtractAll(mCache);
221     }
222
223     @Override
224     public boolean equals(Object o) {
225         if (o == this) return true;
226         if (!(o instanceof RcsResourceAttributes)) return false;
227
228         final RcsResourceAttributes rhs = (RcsResourceAttributes) o;
229
230         esnureAllExtracted();
231         rhs.esnureAllExtracted();
232
233         return mCache.equals(rhs.mCache);
234     }
235
236     @Override
237     public int hashCode() {
238         esnureAllExtracted();
239         return mCache.hashCode();
240     }
241     
242     /**
243      * Updates all properties provided as parameter.
244      */
245     public void put(RcsResourceAttributes attrs){
246         for (final String key : attrs.keySet()) {
247             mCache.put(key, attrs.get(key));
248         }
249     }
250
251 }