1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.service.resourcecontainer;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
30 //import org.iotivity.service.resourcecontainer.server.RcsLockedAttributes;
34 * This class represents the attributes for a resource.
38 public final class RcsResourceAttributes
41 private final Map<String, RcsValue> mCache = new HashMap<>();
43 public RcsResourceAttributes() {
47 public RcsResourceAttributes(RcsResourceAttributes attrs){
48 for (final String key : attrs.keySet()) {
49 mCache.put(key, attrs.get(key));
54 * Returns a unmodifiable Set view of the keys contained in this attributes.
56 * @return an unmodifiable set view of the keys in this attributes
58 public Set<String> keySet() {
59 return Collections.unmodifiableSet(mCache.keySet());
63 * Returns the value to which the specified key is mapped, or null if this
64 * contains no mapping for the key.
67 * the key whose associated value is to be returned
69 * @return the value to which the specified key is mapped, or null if this
70 * contains no mapping for the key
72 * @throws NullPointerException
75 public RcsValue get(String key) {
76 if (key == null) throw new NullPointerException("key is null");
78 return mCache.get(key);
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.
87 * key with which the specified value is to be associated
90 * value to be associated with the specified key
92 * @throws NullPointerException
93 * if key or value is null
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");
100 mCache.put(key, value);
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.
109 * key with which the specified value is to be associated
112 * value to be associated with the specified key
114 * @throws NullPointerException
115 * if key or value is null
116 * @throws IllegalArgumentException
117 * if object is not supported type by {@link RcsValue}
119 public void put(String key, Object object) {
120 if (key == null) throw new NullPointerException("key is null");
122 put(key, new RcsValue(object));
126 * Returns true if this contains no key-value mappings.
128 * @return true if this contains no key-value mappings
130 public boolean isEmpty() {
131 return mCache.isEmpty();
135 * Returns the number of key-value mappings.
137 * @return the number of key-value mappings
140 return mCache.size();
144 * Removes the mapping for a key from this attributes if it is present.
147 * key whose mapping is to be removed
149 * @return true if the key is present and the the value mapped is removed.
151 public boolean remove(String key) {
152 if (key == null) throw new NullPointerException("key is null");
154 final boolean cacheRemove = mCache.remove(key) != null;
160 * Removes all of the mappings.
167 * Returns true if this contains a mapping for the specified key.
170 * key whose presence is to be tested
172 * @return true if this contains a mapping for the specified key.
174 * @throws NullPointerException
177 public boolean contains(String key) {
178 if (key == null) throw new NullPointerException("key is null");
180 return mCache.containsKey(key);
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);
193 public int hashCode() {
194 return mCache.hashCode();
198 * Updates all properties provided as parameter.
200 public void put(RcsResourceAttributes attrs){
201 for (final String key : attrs.keySet()) {
202 mCache.put(key, attrs.get(key));