[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / AttributeValue.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator;
18
19 /**
20  * This class accommodate different type of values as Object.
21  */
22 public class AttributeValue {
23
24     private Object mValue = null;
25
26     /**
27      * Enum to represent value type.
28      */
29     public enum ValueType {
30         UNKNOWN, INTEGER, DOUBLE, BOOLEAN, STRING, RESOURCEMODEL, ARRAY
31     }
32
33     /**
34      * Class which provides the value type information in detail.
35      */
36     public class TypeInfo {
37         public ValueType mType;
38         public ValueType mBaseType;
39         public int       mDepth;
40
41         /**
42          * Constructs {@link AttributeValue.TypeInfo} with default values.
43          */
44         TypeInfo() {
45             mType = ValueType.UNKNOWN;
46             mBaseType = ValueType.UNKNOWN;
47             mDepth = 0;
48         }
49
50         /**
51          * Constructs {@link AttributeValue.TypeInfo} with given
52          * {AttributeValue.ValueType}.
53          *
54          * @param type
55          *            Value type.
56          */
57         TypeInfo(ValueType type) {
58             mType = type;
59             mBaseType = type;
60             mDepth = 0;
61         }
62
63         /**
64          * Constructs {@link AttributeValue.TypeInfo} with given
65          * {AttributeValue.ValueType}s and depth information.
66          *
67          * @param type
68          *            Value type.
69          * @param baseType
70          *            Value base type. This type is useful when value is of
71          *            array type.
72          * @param depth
73          *            Depth of array type value. This is useful when value is of
74          *            array type.
75          */
76         TypeInfo(ValueType type, ValueType baseType, int depth) {
77             mType = type;
78             mBaseType = baseType;
79             mDepth = depth;
80         }
81     }
82
83     /**
84      * Constructs {@link AttributeValue} with int type value.
85      *
86      * @param value
87      *            int type value.
88      */
89     public AttributeValue(int value) {
90         mValue = new Integer(value);
91     }
92
93     /**
94      * Constructs {@link AttributeValue} with double type value.
95      *
96      * @param value
97      *            double type value.
98      */
99     public AttributeValue(double value) {
100         mValue = new Double(value);
101     }
102
103     /**
104      * Constructs {@link AttributeValue} with boolean type value.
105      *
106      * @param value
107      *            boolean type value.
108      */
109     public AttributeValue(boolean value) {
110         mValue = new Boolean(value);
111     }
112
113     /**
114      * Constructs {@link AttributeValue} with string type value.
115      *
116      * @param value
117      *            String type value.
118      */
119     public AttributeValue(String value) {
120         mValue = value;
121     }
122
123     /**
124      * Constructs {@link AttributeValue} with SimulatorResourceModel type value.
125      *
126      * @param value
127      *            SimulatorResourceModel type value.
128      */
129     public AttributeValue(SimulatorResourceModel value) {
130         mValue = value;
131     }
132
133     /**
134      * Constructs {@link AttributeValue} with array of int type values.
135      *
136      * @param values
137      *            Array of int type values.
138      */
139     public AttributeValue(int[] values) {
140         Integer[] newValues = new Integer[values.length];
141         for (int i = 0; i < values.length; i++)
142             newValues[i] = Integer.valueOf(values[i]);
143         mValue = newValues;
144     }
145
146     /**
147      * Constructs {@link AttributeValue} with array of Integer type values.
148      *
149      * @param values
150      *            Array of Integer type values.
151      */
152     public AttributeValue(Integer[] values) {
153         mValue = values;
154     }
155
156     /**
157      * Constructs {@link AttributeValue} with array of double type values.
158      *
159      * @param values
160      *            Array of double type values.
161      */
162     public AttributeValue(double[] values) {
163         Double[] newValues = new Double[values.length];
164         for (int i = 0; i < values.length; i++)
165             newValues[i] = Double.valueOf(values[i]);
166         mValue = newValues;
167     }
168
169     /**
170      * Constructs {@link AttributeValue} with array of Double type values.
171      *
172      * @param values
173      *            Array of Double type values.
174      */
175     public AttributeValue(Double[] values) {
176         mValue = values;
177     }
178
179     /**
180      * Constructs {@link AttributeValue} with array of boolean type values.
181      *
182      * @param values
183      *            Array of boolean type values.
184      */
185     public AttributeValue(boolean[] values) {
186         Boolean[] newValues = new Boolean[values.length];
187         for (int i = 0; i < values.length; i++)
188             newValues[i] = Boolean.valueOf(values[i]);
189         mValue = newValues;
190     }
191
192     /**
193      * Constructs {@link AttributeValue} with array of Boolean type values.
194      *
195      * @param values
196      *            Array of Boolean type values.
197      */
198     public AttributeValue(Boolean[] values) {
199         mValue = values;
200     }
201
202     /**
203      * Constructs {@link AttributeValue} with array of String type values.
204      *
205      * @param values
206      *            Array of String type values.
207      */
208     public AttributeValue(String[] values) {
209         mValue = values;
210     }
211
212     /**
213      * Constructs {@link AttributeValue} with array of SimulatorResourceModel
214      * type values.
215      *
216      * @param values
217      *            Array of SimulatorResourceModel type values.
218      */
219     public AttributeValue(SimulatorResourceModel[] values) {
220         mValue = values;
221     }
222
223     /**
224      * Constructs {@link AttributeValue} with 2 dimensional array of int type
225      * values.
226      *
227      * @param values
228      *            2 dimensional array of int type values.
229      */
230     public AttributeValue(int[][] values) {
231         Integer[][] newValues = new Integer[values.length][];
232         for (int i = 0; i < values.length; i++) {
233             newValues[i] = new Integer[values[i].length];
234             for (int j = 0; j < values[i].length; j++) {
235                 newValues[i][j] = Integer.valueOf(values[i][j]);
236             }
237         }
238         mValue = newValues;
239     }
240
241     /**
242      * Constructs {@link AttributeValue} with 2 dimensional array of Integer
243      * type values.
244      *
245      * @param values
246      *            2 dimensional array of Integer type values.
247      */
248     public AttributeValue(Integer[][] values) {
249         mValue = values;
250     }
251
252     /**
253      * Constructs {@link AttributeValue} with 2 dimensional array of double type
254      * values.
255      *
256      * @param values
257      *            2 dimensional array of double type values.
258      */
259     public AttributeValue(double[][] values) {
260         Double[][] newValues = new Double[values.length][];
261         for (int i = 0; i < values.length; i++) {
262             newValues[i] = new Double[values[i].length];
263             for (int j = 0; j < values[i].length; j++) {
264                 newValues[i][j] = Double.valueOf(values[i][j]);
265             }
266         }
267         mValue = newValues;
268     }
269
270     /**
271      * Constructs {@link AttributeValue} with 2 dimensional array of Double type
272      * values.
273      *
274      * @param values
275      *            2 dimensional array of Double type values.
276      */
277     public AttributeValue(Double[][] values) {
278         mValue = values;
279     }
280
281     /**
282      * Constructs {@link AttributeValue} with 2 dimensional array of boolean
283      * type values.
284      *
285      * @param values
286      *            2 dimensional array of boolean type values.
287      */
288     public AttributeValue(boolean[][] values) {
289         Boolean[][] newValues = new Boolean[values.length][];
290         for (int i = 0; i < values.length; i++) {
291             newValues[i] = new Boolean[values[i].length];
292             for (int j = 0; j < values[i].length; j++) {
293                 newValues[i][j] = Boolean.valueOf(values[i][j]);
294             }
295         }
296         mValue = newValues;
297     }
298
299     /**
300      * Constructs {@link AttributeValue} with 2 dimensional array of Boolean
301      * type values.
302      *
303      * @param values
304      *            2 dimensional array of Boolean type values.
305      */
306     public AttributeValue(Boolean[][] values) {
307         mValue = values;
308     }
309
310     /**
311      * Constructs {@link AttributeValue} with 2 dimensional array of String type
312      * values.
313      *
314      * @param values
315      *            2 dimensional array of String type values.
316      */
317     public AttributeValue(String[][] values) {
318         mValue = values;
319     }
320
321     /**
322      * Constructs {@link AttributeValue} with 2 dimensional array of
323      * SimulatorResourceModel type values.
324      *
325      * @param values
326      *            2 dimensional array of SimulatorResourceModel type values.
327      */
328     public AttributeValue(SimulatorResourceModel[][] values) {
329         mValue = values;
330     }
331
332     /**
333      * Constructs {@link AttributeValue} with 3 dimensional array of int type
334      * values.
335      *
336      * @param values
337      *            3 dimensional array of int type values.
338      */
339     public AttributeValue(int[][][] values) {
340         Integer[][][] newValues = new Integer[values.length][][];
341         for (int i = 0; i < values.length; i++) {
342             newValues[i] = new Integer[values[i].length][];
343             for (int j = 0; j < values[i].length; j++) {
344                 newValues[i][j] = new Integer[values[i][j].length];
345                 for (int k = 0; k < values[j].length; k++) {
346                     newValues[i][j][k] = Integer.valueOf(values[i][j][k]);
347                 }
348             }
349         }
350         mValue = newValues;
351     }
352
353     /**
354      * Constructs {@link AttributeValue} with 3 dimensional array of Integer
355      * type values.
356      *
357      * @param values
358      *            3 dimensional array of Integer type values.
359      */
360     public AttributeValue(Integer[][][] values) {
361         mValue = values;
362     }
363
364     /**
365      * Constructs {@link AttributeValue} with 3 dimensional array of double type
366      * values.
367      *
368      * @param values
369      *            3 dimensional array of double type values.
370      */
371     public AttributeValue(double[][][] values) {
372         Double[][][] newValues = new Double[values.length][][];
373         for (int i = 0; i < values.length; i++) {
374             newValues[i] = new Double[values[i].length][];
375             for (int j = 0; j < values[i].length; j++) {
376                 newValues[i][j] = new Double[values[i][j].length];
377                 for (int k = 0; k < values[j].length; k++) {
378                     newValues[i][j][k] = Double.valueOf(values[i][j][k]);
379                 }
380             }
381         }
382         mValue = newValues;
383     }
384
385     /**
386      * Constructs {@link AttributeValue} with 3 dimensional array of Double type
387      * values.
388      *
389      * @param values
390      *            3 dimensional array of Double type values.
391      */
392     public AttributeValue(Double[][][] values) {
393         mValue = values;
394     }
395
396     /**
397      * Constructs {@link AttributeValue} with 3 dimensional array of boolean
398      * type values.
399      *
400      * @param values
401      *            3 dimensional array of boolean type values.
402      */
403     public AttributeValue(boolean[][][] values) {
404         Boolean[][][] newValues = new Boolean[values.length][][];
405         for (int i = 0; i < values.length; i++) {
406             newValues[i] = new Boolean[values[i].length][];
407             for (int j = 0; j < values[i].length; j++) {
408                 newValues[i][j] = new Boolean[values[i][j].length];
409                 for (int k = 0; k < values[j].length; k++) {
410                     newValues[i][j][k] = Boolean.valueOf(values[i][j][k]);
411                 }
412             }
413         }
414         mValue = newValues;
415     }
416
417     /**
418      * Constructs {@link AttributeValue} with 3 dimensional array of Boolean
419      * type values.
420      *
421      * @param values
422      *            3 dimensional array of Boolean type values.
423      */
424     public AttributeValue(Boolean[][][] values) {
425         mValue = values;
426     }
427
428     /**
429      * Constructs {@link AttributeValue} with 3 dimensional array of String type
430      * values.
431      *
432      * @param values
433      *            3 dimensional array of String type values.
434      */
435     public AttributeValue(String[][][] values) {
436         mValue = values;
437     }
438
439     /**
440      * Constructs {@link AttributeValue} with 3 dimensional array of
441      * SimulatorResourceModel type values.
442      *
443      * @param values
444      *            3 dimensional array of SimulatorResourceModel type values.
445      */
446     public AttributeValue(SimulatorResourceModel[][][] values) {
447         mValue = values;
448     }
449
450     /**
451      * API to get value type information.
452      *
453      * @return {@link AttributeValue.TypeInfo}.
454      */
455     public TypeInfo typeInfo() {
456         return createTypeInfo(mValue);
457     }
458
459     /**
460      * API to get value as Object.
461      *
462      * @return Value as Object.
463      */
464     public Object get() {
465         return mValue;
466     }
467
468     private AttributeValue(Object value) {
469         mValue = value;
470     }
471
472     private TypeInfo createTypeInfo(Object value) {
473         TypeInfo typeInfo = new TypeInfo();
474         String className = value.getClass().getName();
475         if (className.contains(Integer.class.getName())) {
476             typeInfo.mBaseType = ValueType.INTEGER;
477             typeInfo.mType = ValueType.INTEGER;
478         } else if (className.contains(Double.class.getName())) {
479             typeInfo.mBaseType = ValueType.DOUBLE;
480             typeInfo.mType = ValueType.DOUBLE;
481         } else if (className.contains(Boolean.class.getName())) {
482             typeInfo.mBaseType = ValueType.BOOLEAN;
483             typeInfo.mType = ValueType.BOOLEAN;
484         } else if (className.contains(String.class.getName())) {
485             typeInfo.mBaseType = ValueType.STRING;
486             typeInfo.mType = ValueType.STRING;
487         } else if (className.contains(SimulatorResourceModel.class.getName())) {
488             typeInfo.mBaseType = ValueType.RESOURCEMODEL;
489             typeInfo.mType = ValueType.RESOURCEMODEL;
490         }
491
492         // For array types
493         if (value.getClass().isArray()) {
494             typeInfo.mType = ValueType.ARRAY;
495             for (char ch : className.toCharArray()) {
496                 if (ch == '[')
497                     typeInfo.mDepth++;
498             }
499         }
500
501         return typeInfo;
502     }
503 }