2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base;
25 import java.security.InvalidParameterException;
26 import java.util.Arrays;
27 import java.util.List;
32 public class OcRepresentation {
35 System.loadLibrary("oc");
36 System.loadLibrary("ocstack-jni");
39 public OcRepresentation() {
41 //Native OCRepresentation object was created using "new" and needs to be deleted
42 this.mNativeNeedsDelete = true;
45 private OcRepresentation(long nativeHandle) {
46 this.mNativeHandle = nativeHandle;
47 this.mNativeNeedsDelete = false;
50 private OcRepresentation(long nativeHandle, boolean nativeNeedsDelete) {
51 this.mNativeHandle = nativeHandle;
52 this.mNativeNeedsDelete = nativeNeedsDelete;
55 public <T> T getValue(String key) throws OcException {
56 Object obj = this.getValueN(key);
57 @SuppressWarnings("unchecked")
62 private native Object getValueN(String key);
64 public void setValue(String key, int value) throws OcException {
65 this.setValueInteger(key, value);
68 public void setValue(String key, double value) throws OcException {
69 this.setValueDouble(key, value);
72 public void setValue(String key, boolean value) throws OcException {
73 this.setValueBoolean(key, value);
76 public void setValue(String key, String value) throws OcException {
77 this.setValueStringN(key, value);
80 public void setValue(String key, OcRepresentation value) throws OcException {
81 this.setValueRepresentation(key, value);
84 public void setValue(String key, int[] value) throws OcException {
85 this.setValueIntegerArray(key, value);
88 public void setValue(String key, int[][] value) throws OcException {
89 this.setValueInteger2DArray(key, value);
92 public void setValue(String key, int[][][] value) throws OcException {
93 this.setValueInteger3DArray(key, value);
96 public void setValue(String key, double[] value) throws OcException {
97 this.setValueDoubleArray(key, value);
100 public void setValue(String key, double[][] value) throws OcException {
101 this.setValueDouble2DArray(key, value);
104 public void setValue(String key, double[][][] value) throws OcException {
105 this.setValueDouble3DArray(key, value);
108 public void setValue(String key, boolean[] value) throws OcException {
109 this.setValueBooleanArray(key, value);
112 public void setValue(String key, boolean[][] value) throws OcException {
113 this.setValueBoolean2DArray(key, value);
116 public void setValue(String key, boolean[][][] value) throws OcException {
117 this.setValueBoolean3DArray(key, value);
120 public void setValue(String key, String[] value) throws OcException {
121 this.setValueStringArray(key, value);
124 public void setValue(String key, String[][] value) throws OcException {
125 this.setValueString2DArray(key, value);
128 public void setValue(String key, String[][][] value) throws OcException {
129 this.setValueString3DArray(key, value);
132 public void setValue(String key, OcRepresentation[] value) throws OcException {
133 this.setValueRepresentationArray(key, value);
136 public void setValue(String key, OcRepresentation[][] value) throws OcException {
137 this.setValueRepresentation2DArray(key, value);
140 public void setValue(String key, OcRepresentation[][][] value) throws OcException {
141 this.setValueRepresentation3DArray(key, value);
144 private native void setValueInteger(String key, int value) throws OcException;
146 private native void setValueDouble(String key, double value) throws OcException;
148 private native void setValueBoolean(String key, boolean value) throws OcException;
150 private native void setValueStringN(String key, String value) throws OcException;
152 private native void setValueRepresentation(String key, OcRepresentation value) throws OcException;
154 private native void setValueIntegerArray(String key, int[] value) throws OcException;
156 private native void setValueInteger2DArray(String key, int[][] value) throws OcException;
158 private native void setValueInteger3DArray(String key, int[][][] value) throws OcException;
160 private native void setValueDoubleArray(String key, double[] value) throws OcException;
162 private native void setValueDouble2DArray(String key, double[][] value) throws OcException;
164 private native void setValueDouble3DArray(String key, double[][][] value) throws OcException;
166 private native void setValueBooleanArray(String key, boolean[] value) throws OcException;
168 private native void setValueBoolean2DArray(String key, boolean[][] value) throws OcException;
170 private native void setValueBoolean3DArray(String key, boolean[][][] value) throws OcException;
172 private native void setValueStringArray(String key, String[] value) throws OcException;
174 private native void setValueString2DArray(String key, String[][] value) throws OcException;
176 private native void setValueString3DArray(String key, String[][][] value) throws OcException;
178 private native void setValueRepresentationArray(String key, OcRepresentation[] value) throws OcException;
180 private native void setValueRepresentation2DArray(String key, OcRepresentation[][] value) throws OcException;
182 private native void setValueRepresentation3DArray(String key, OcRepresentation[][][] value) throws OcException;
185 * @deprecated use {@link #getValue(String key)} instead.
188 public int getValueInt(String key) {
191 value = this.getValue(key);
192 } catch (OcException e) {
193 //simply catching here for the deprecated APIs, so the older usages don't have to handle
200 * @deprecated use {@link #getValue(String key)} instead.
203 public boolean getValueBool(String key) {
204 Boolean value = false;
206 value = this.getValue(key);
207 } catch (OcException e) {
208 //simply catching here for the deprecated APIs, so the older usages don't have to handle
215 * @deprecated use {@link #getValue(String key)} instead.
218 public String getValueString(String key) {
221 value = this.getValue(key);
222 } catch (OcException e) {
223 //simply catching here for the deprecated APIs, so the older usages don't have to handle
230 * @deprecated use {@link #setValue(String key, int value)} instead.
233 public void setValueInt(String key, int value) {
235 this.setValue(key, value);
236 } catch (OcException e) {
237 //simply catching here for the deprecated APIs, so the older usages don't have to handle
243 * @deprecated use {@link #setValue(String key, boolean value)} instead.
246 public void setValueBool(String key, boolean value) {
248 this.setValue(key, value);
249 } catch (OcException e) {
250 //simply catching here for the deprecated APIs, so the older usages don't have to handle
256 * @deprecated use {@link #setValue(String key, String value)} instead.
259 public void setValueString(String key, String value) {
261 this.setValue(key, value);
262 } catch (OcException e) {
263 //simply catching here for the deprecated APIs, so the older usages don't have to handle
268 public native void addChild(OcRepresentation representation);
270 public native void clearChildren();
272 public List<OcRepresentation> getChildren() {
273 return Arrays.asList(
277 private native OcRepresentation[] getChildrenArray();
279 public native String getUri();
281 public native String getHost();
283 public native void setUri(String uri);
286 * Method to get the list of resource types
288 * @return List of resource types
290 public native List<String> getResourceTypes();
293 * Method to set the list of resource types
295 * @param resourceTypeList list of resources
297 public void setResourceTypes(List<String> resourceTypeList) {
298 if (null == resourceTypeList) {
299 throw new InvalidParameterException("resourceTypeList cannot be null");
302 this.setResourceTypeArray(
303 resourceTypeList.toArray(
304 new String[resourceTypeList.size()]));
307 private native void setResourceTypeArray(String[] resourceTypeArray);
310 * Method to get the list of resource interfaces
312 * @return List of resource interface
314 public native List<String> getResourceInterfaces();
317 * Method to set the list of resource interfaces
319 * @param resourceInterfaceList list of interfaces
321 public void setResourceInterfaces(List<String> resourceInterfaceList) {
322 if (null == resourceInterfaceList) {
323 throw new InvalidParameterException("resourceInterfaceList cannot be null");
326 this.setResourceInterfaceArray(
327 resourceInterfaceList.toArray(
328 new String[resourceInterfaceList.size()]));
331 private native void setResourceInterfaceArray(String[] resourceInterfaceArray);
333 public native boolean isEmpty();
335 public native int size();
337 public native boolean remove(String key);
339 public native boolean hasAttribute(String key);
341 public native void setNull(String key);
343 public native boolean isNull(String key);
346 protected void finalize() throws Throwable {
349 dispose(this.mNativeNeedsDelete);
352 private native void create();
354 private native void dispose(boolean needsDelete);
356 private long mNativeHandle;
357 private boolean mNativeNeedsDelete;