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