1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 package org.iotivity.service.resourcecontainer;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
29 import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
33 * This class represents the attributes for a resource.
37 public final class RcsResourceAttributes extends RcsObject {
39 private native boolean nativeIsEmpty();
41 private native int nativeSize();
43 private native boolean nativeRemove(String key);
45 private native void nativeClear();
47 private native boolean nativeContains(String key);
49 private native void nativeAddKeys(Set<String> set);
51 private native RcsValue nativeExtract(String key);
53 private native void nativeExtractAll(Map<String, RcsValue> map);
55 private final Map<String, RcsValue> mCache = new HashMap<>();
57 public RcsResourceAttributes() {
60 public RcsResourceAttributes(RcsLockedAttributes lockedAttrs)
62 for (final String key : lockedAttrs.keySet()) {
63 mCache.put(key, lockedAttrs.get(key));
67 public RcsResourceAttributes(RcsResourceAttributes attrs){
68 for (final String key : attrs.keySet()) {
69 mCache.put(key, attrs.get(key));
74 * Returns a unmodifiable Set view of the keys contained in this attributes.
76 * @return an unmodifiable set view of the keys in this attributes
78 public Set<String> keySet() {
80 final Set<String> keySet = new HashSet<>(mCache.keySet());
82 nativeAddKeys(keySet);
84 return Collections.unmodifiableSet(keySet);
87 return Collections.unmodifiableSet(mCache.keySet());
91 * Returns the value to which the specified key is mapped, or null if this
92 * contains no mapping for the key.
95 * the key whose associated value is to be returned
97 * @return the value to which the specified key is mapped, or null if this
98 * contains no mapping for the key
100 * @throws NullPointerException
103 public RcsValue get(String key) {
104 if (key == null) throw new NullPointerException("key is null");
106 if (!mCache.containsKey(key) && hasHandle() && nativeContains(key)) {
107 mCache.put(key, nativeExtract(key));
109 return mCache.get(key);
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.
118 * key with which the specified value is to be associated
121 * value to be associated with the specified key
123 * @throws NullPointerException
124 * if key or value is null
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");
131 mCache.put(key, value);
132 if (hasHandle()) nativeRemove(key);
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.
141 * key with which the specified value is to be associated
144 * value to be associated with the specified key
146 * @throws NullPointerException
147 * if key or value is null
148 * @throws IllegalArgumentException
149 * if object is not supported type by {@link RcsValue}
151 public void put(String key, Object object) {
152 if (key == null) throw new NullPointerException("key is null");
154 put(key, new RcsValue(object));
158 * Returns true if this contains no key-value mappings.
160 * @return true if this contains no key-value mappings
162 public boolean isEmpty() {
163 return mCache.isEmpty() && (!hasHandle() || nativeIsEmpty());
167 * Returns the number of key-value mappings.
169 * @return the number of key-value mappings
172 if (hasHandle()) return mCache.size() + nativeSize();
173 return mCache.size();
177 * Removes the mapping for a key from this attributes if it is present.
180 * key whose mapping is to be removed
182 * @return true if the key is present and the the value mapped is removed.
184 public boolean remove(String key) {
185 if (key == null) throw new NullPointerException("key is null");
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);
191 return cacheRemove || nativeRemove;
195 * Removes all of the mappings.
197 public void clear() {
203 * Returns true if this contains a mapping for the specified key.
206 * key whose presence is to be tested
208 * @return true if this contains a mapping for the specified key.
210 * @throws NullPointerException
213 public boolean contains(String key) {
214 if (key == null) throw new NullPointerException("key is null");
216 return mCache.containsKey(key) || nativeContains(key);
219 private void esnureAllExtracted() {
220 if (hasHandle()) nativeExtractAll(mCache);
224 public boolean equals(Object o) {
225 if (o == this) return true;
226 if (!(o instanceof RcsResourceAttributes)) return false;
228 final RcsResourceAttributes rhs = (RcsResourceAttributes) o;
230 esnureAllExtracted();
231 rhs.esnureAllExtracted();
233 return mCache.equals(rhs.mCache);
237 public int hashCode() {
238 esnureAllExtracted();
239 return mCache.hashCode();
243 * Updates all properties provided as parameter.
245 public void put(RcsResourceAttributes attrs){
246 for (final String key : attrs.keySet()) {
247 mCache.put(key, attrs.get(key));