Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / base / OcRepresentation.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base;
24
25 import java.security.InvalidParameterException;
26 import java.util.Arrays;
27 import java.util.List;
28
29 /**
30  *
31  */
32 public class OcRepresentation {
33
34     static {
35         System.loadLibrary("oc");
36         System.loadLibrary("ocstack-jni");
37     }
38
39     public OcRepresentation() {
40         create();
41         //Native OCRepresentation object was created using "new" and needs to be deleted
42         this.mNativeNeedsDelete = true;
43     }
44
45     private OcRepresentation(long nativeHandle) {
46         this.mNativeHandle = nativeHandle;
47         this.mNativeNeedsDelete = false;
48     }
49
50     private OcRepresentation(long nativeHandle, boolean nativeNeedsDelete) {
51         this.mNativeHandle = nativeHandle;
52         this.mNativeNeedsDelete = nativeNeedsDelete;
53     }
54
55     public <T> T getValue(String key) throws OcException {
56         Object obj = this.getValueN(key);
57         @SuppressWarnings("unchecked")
58         T t = (T) obj;
59         return t;
60     }
61
62     private native Object getValueN(String key);
63
64     public void setValue(String key, int value) throws OcException {
65         this.setValueInteger(key, value);
66     }
67
68     public void setValue(String key, double value) throws OcException {
69         this.setValueDouble(key, value);
70     }
71
72     public void setValue(String key, boolean value) throws OcException {
73         this.setValueBoolean(key, value);
74     }
75
76     public void setValue(String key, String value) throws OcException {
77         this.setValueStringN(key, value);
78     }
79
80     public void setValue(String key, OcRepresentation value) throws OcException {
81         this.setValueRepresentation(key, value);
82     }
83
84     public void setValue(String key, int[] value) throws OcException {
85         this.setValueIntegerArray(key, value);
86     }
87
88     public void setValue(String key, int[][] value) throws OcException {
89         this.setValueInteger2DArray(key, value);
90     }
91
92     public void setValue(String key, int[][][] value) throws OcException {
93         this.setValueInteger3DArray(key, value);
94     }
95
96     public void setValue(String key, double[] value) throws OcException {
97         this.setValueDoubleArray(key, value);
98     }
99
100     public void setValue(String key, double[][] value) throws OcException {
101         this.setValueDouble2DArray(key, value);
102     }
103
104     public void setValue(String key, double[][][] value) throws OcException {
105         this.setValueDouble3DArray(key, value);
106     }
107
108     public void setValue(String key, boolean[] value) throws OcException {
109         this.setValueBooleanArray(key, value);
110     }
111
112     public void setValue(String key, boolean[][] value) throws OcException {
113         this.setValueBoolean2DArray(key, value);
114     }
115
116     public void setValue(String key, boolean[][][] value) throws OcException {
117         this.setValueBoolean3DArray(key, value);
118     }
119
120     public void setValue(String key, String[] value) throws OcException {
121         this.setValueStringArray(key, value);
122     }
123
124     public void setValue(String key, String[][] value) throws OcException {
125         this.setValueString2DArray(key, value);
126     }
127
128     public void setValue(String key, String[][][] value) throws OcException {
129         this.setValueString3DArray(key, value);
130     }
131
132     public void setValue(String key, OcRepresentation[] value) throws OcException {
133         this.setValueRepresentationArray(key, value);
134     }
135
136     public void setValue(String key, OcRepresentation[][] value) throws OcException {
137         this.setValueRepresentation2DArray(key, value);
138     }
139
140     public void setValue(String key, OcRepresentation[][][] value) throws OcException {
141         this.setValueRepresentation3DArray(key, value);
142     }
143
144     private native void setValueInteger(String key, int value) throws OcException;
145
146     private native void setValueDouble(String key, double value) throws OcException;
147
148     private native void setValueBoolean(String key, boolean value) throws OcException;
149
150     private native void setValueStringN(String key, String value) throws OcException;
151
152     private native void setValueRepresentation(String key, OcRepresentation value) throws OcException;
153
154     private native void setValueIntegerArray(String key, int[] value) throws OcException;
155
156     private native void setValueInteger2DArray(String key, int[][] value) throws OcException;
157
158     private native void setValueInteger3DArray(String key, int[][][] value) throws OcException;
159
160     private native void setValueDoubleArray(String key, double[] value) throws OcException;
161
162     private native void setValueDouble2DArray(String key, double[][] value) throws OcException;
163
164     private native void setValueDouble3DArray(String key, double[][][] value) throws OcException;
165
166     private native void setValueBooleanArray(String key, boolean[] value) throws OcException;
167
168     private native void setValueBoolean2DArray(String key, boolean[][] value) throws OcException;
169
170     private native void setValueBoolean3DArray(String key, boolean[][][] value) throws OcException;
171
172     private native void setValueStringArray(String key, String[] value) throws OcException;
173
174     private native void setValueString2DArray(String key, String[][] value) throws OcException;
175
176     private native void setValueString3DArray(String key, String[][][] value) throws OcException;
177
178     private native void setValueRepresentationArray(String key, OcRepresentation[] value) throws OcException;
179
180     private native void setValueRepresentation2DArray(String key, OcRepresentation[][] value) throws OcException;
181
182     private native void setValueRepresentation3DArray(String key, OcRepresentation[][][] value) throws OcException;
183
184     /**
185      * @deprecated use {@link #getValue(String key)} instead.
186      */
187     @Deprecated
188     public int getValueInt(String key) {
189         Integer value = 0;
190         try {
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
194             //it in the code
195         }
196         return value;
197     }
198
199     /**
200      * @deprecated use {@link #getValue(String key)} instead.
201      */
202     @Deprecated
203     public boolean getValueBool(String key) {
204         Boolean value = false;
205         try {
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
209             //it in the code
210         }
211         return value;
212     }
213
214     /**
215      * @deprecated use {@link #getValue(String key)} instead.
216      */
217     @Deprecated
218     public String getValueString(String key) {
219         String value = "";
220         try {
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
224             //it in the code
225         }
226         return value;
227     }
228
229     /**
230      * @deprecated use {@link #setValue(String key, int value)} instead.
231      */
232     @Deprecated
233     public void setValueInt(String key, int value) {
234         try {
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
238             //it in the code
239         }
240     }
241
242     /**
243      * @deprecated use {@link #setValue(String key, boolean value)} instead.
244      */
245     @Deprecated
246     public void setValueBool(String key, boolean value) {
247         try {
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
251             //it in the code
252         }
253     }
254
255     /**
256      * @deprecated use {@link #setValue(String key, String value)} instead.
257      */
258     @Deprecated
259     public void setValueString(String key, String value) {
260         try {
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
264             //it in the code
265         }
266     }
267
268     public native void addChild(OcRepresentation representation);
269
270     public native void clearChildren();
271
272     public List<OcRepresentation> getChildren() {
273         return Arrays.asList(
274                 getChildrenArray());
275     }
276
277     private native OcRepresentation[] getChildrenArray();
278
279     public native String getUri();
280
281     public native void setUri(String uri);
282
283     /**
284      * Method to get the list of resource types
285      *
286      * @return List of resource types
287      */
288     public native List<String> getResourceTypes();
289
290     /**
291      * Method to set the list of resource types
292      *
293      * @param resourceTypeList list of resources
294      */
295     public void setResourceTypes(List<String> resourceTypeList) {
296         if (null == resourceTypeList) {
297             throw new InvalidParameterException("resourceTypeList cannot be null");
298         }
299
300         this.setResourceTypeArray(
301                 resourceTypeList.toArray(
302                         new String[resourceTypeList.size()]));
303     }
304
305     private native void setResourceTypeArray(String[] resourceTypeArray);
306
307     /**
308      * Method to get the list of resource interfaces
309      *
310      * @return List of resource interface
311      */
312     public native List<String> getResourceInterfaces();
313
314     /**
315      * Method to set the list of resource interfaces
316      *
317      * @param resourceInterfaceList list of interfaces
318      */
319     public void setResourceInterfaces(List<String> resourceInterfaceList) {
320         if (null == resourceInterfaceList) {
321             throw new InvalidParameterException("resourceInterfaceList cannot be null");
322         }
323
324         this.setResourceInterfaceArray(
325                 resourceInterfaceList.toArray(
326                         new String[resourceInterfaceList.size()]));
327     }
328
329     private native void setResourceInterfaceArray(String[] resourceInterfaceArray);
330
331     public native boolean isEmpty();
332
333     public native int size();
334
335     public native boolean remove(String key);
336
337     public native boolean hasAttribute(String key);
338
339     public native void setNull(String key);
340
341     public native boolean isNull(String key);
342
343     @Override
344     protected void finalize() throws Throwable {
345         super.finalize();
346
347         dispose(this.mNativeNeedsDelete);
348     }
349
350     private native void create();
351
352     private native void dispose(boolean needsDelete);
353
354     private long mNativeHandle;
355     private boolean mNativeNeedsDelete;
356 }