76bc72cd7252531afd13b1d801ce17944371d7fc
[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 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 {@AttributeValue.TypeInfo} with
43          * default values.
44          */
45         TypeInfo() {
46             mType = ValueType.UNKNOWN;
47             mBaseType = ValueType.UNKNOWN;
48             mDepth = 0;
49         }
50
51         /**
52          * Constructs {@AttributeValue.TypeInfo} with
53          * given {AttributeValue.ValueType}.
54          *
55          * @param type
56          *            Value type.
57          */
58         TypeInfo(ValueType type) {
59             mType = type;
60             mBaseType = type;
61             mDepth = 0;
62         }
63
64         /**
65          * Constructs {@AttributeValue.TypeInfo} with
66          * given {AttributeValue.ValueType}s and depth information.
67          *
68          * @param type
69          *            Value type.
70          * @param baseType
71          *            Value base type. This type is useful when vaule is of
72          *            array type.
73          * @param depth
74          *            Depth of array type value. This type is useful when vaule
75          *            is of array type.
76          */
77         TypeInfo(ValueType type, ValueType baseType, int depth) {
78             mType = type;
79             mBaseType = baseType;
80             mDepth = depth;
81         }
82     }
83
84     /**
85      * Constructs {@AttributeValue} with int type value.
86      *
87      * @param value
88      *            int type value.
89      */
90     public AttributeValue(int value) {
91         mValue = new Integer(value);
92     }
93
94     /**
95      * Constructs {@AttributeValue} with double type value.
96      *
97      * @param value
98      *            double type value.
99      */
100     public AttributeValue(double value) {
101         mValue = new Double(value);
102     }
103
104     /**
105      * Constructs {@AttributeValue} with boolean type value.
106      *
107      * @param value
108      *            boolean type value.
109      */
110     public AttributeValue(boolean value) {
111         mValue = new Boolean(value);
112     }
113
114     /**
115      * Constructs {@AttributeValue} with String type value.
116      *
117      * @param value
118      *            String type value.
119      */
120     public AttributeValue(String value) {
121         mValue = value;
122     }
123
124     /**
125      * Constructs {@AttributeValue} with SimulatorResourceModel
126      * type value.
127      *
128      * @param value
129      *            SimulatorResourceModel type value.
130      */
131     public AttributeValue(SimulatorResourceModel value) {
132         mValue = value;
133     }
134
135     /**
136      * Constructs {@AttributeValue} with array of int type
137      * values.
138      *
139      * @param values
140      *            Array of int type values.
141      */
142     public AttributeValue(int[] values) {
143         Integer[] newValues = new Integer[values.length];
144         for (int i = 0; i < values.length; i++)
145             newValues[i] = Integer.valueOf(values[i]);
146         mValue = newValues;
147     }
148
149     /**
150      * Constructs {@AttributeValue} with array of Integer type
151      * values.
152      *
153      * @param values
154      *            Array of Integer type values.
155      */
156     public AttributeValue(Integer[] values) {
157         mValue = values;
158     }
159
160     /**
161      * Constructs {@AttributeValue} with array of double type
162      * values.
163      *
164      * @param values
165      *            Array of double type values.
166      */
167     public AttributeValue(double[] values) {
168         Double[] newValues = new Double[values.length];
169         for (int i = 0; i < values.length; i++)
170             newValues[i] = Double.valueOf(values[i]);
171         mValue = newValues;
172     }
173
174     /**
175      * Constructs {@AttributeValue} with array of Double type
176      * values.
177      *
178      * @param values
179      *            Array of Double type values.
180      */
181     public AttributeValue(Double[] values) {
182         mValue = values;
183     }
184
185     /**
186      * Constructs {@AttributeValue} with array of boolean type
187      * values.
188      *
189      * @param values
190      *            Array of boolean type values.
191      */
192     public AttributeValue(boolean[] values) {
193         Boolean[] newValues = new Boolean[values.length];
194         for (int i = 0; i < values.length; i++)
195             newValues[i] = Boolean.valueOf(values[i]);
196         mValue = newValues;
197     }
198
199     /**
200      * Constructs {@AttributeValue} with array of Boolean type
201      * values.
202      *
203      * @param values
204      *            Array of Boolean type values.
205      */
206     public AttributeValue(Boolean[] values) {
207         mValue = values;
208     }
209
210     /**
211      * Constructs {@AttributeValue} with array of String type
212      * values.
213      *
214      * @param values
215      *            Array of String type values.
216      */
217     public AttributeValue(String[] values) {
218         mValue = values;
219     }
220
221     /**
222      * Constructs {@AttributeValue} with array of
223      * SimulatorResourceModel type values.
224      *
225      * @param values
226      *            Array of SimulatorResourceModel type values.
227      */
228     public AttributeValue(SimulatorResourceModel[] values) {
229         mValue = values;
230     }
231
232     /**
233      * Constructs {@AttributeValue} with 2 dimensional array of
234      * int type values.
235      *
236      * @param values
237      *            2 dimensional array of int type values.
238      */
239     public AttributeValue(int[][] values) {
240         Integer[][] newValues = new Integer[values.length][];
241         for (int i = 0; i < values.length; i++) {
242             newValues[i] = new Integer[values[i].length];
243             for (int j = 0; j < values[i].length; j++) {
244                 newValues[i][j] = Integer.valueOf(values[i][j]);
245             }
246         }
247         mValue = newValues;
248     }
249
250     /**
251      * Constructs {@AttributeValue} with 2 dimensional array of
252      * Integer type values.
253      *
254      * @param values
255      *            2 dimensional array of Integer type values.
256      */
257     public AttributeValue(Integer[][] values) {
258         mValue = values;
259     }
260
261     /**
262      * Constructs {@AttributeValue} with 2 dimensional array of
263      * double type values.
264      *
265      * @param values
266      *            2 dimensional array of double type values.
267      */
268     public AttributeValue(double[][] values) {
269         Double[][] newValues = new Double[values.length][];
270         for (int i = 0; i < values.length; i++) {
271             newValues[i] = new Double[values[i].length];
272             for (int j = 0; j < values[i].length; j++) {
273                 newValues[i][j] = Double.valueOf(values[i][j]);
274             }
275         }
276         mValue = newValues;
277     }
278
279     /**
280      * Constructs {@AttributeValue} with 2 dimensional array of
281      * Double type values.
282      *
283      * @param values
284      *            2 dimensional array of Double type values.
285      */
286     public AttributeValue(Double[][] values) {
287         mValue = values;
288     }
289
290     /**
291      * Constructs {@AttributeValue} with 2 dimensional array of
292      * boolean type values.
293      *
294      * @param values
295      *            2 dimensional array of boolean type values.
296      */
297     public AttributeValue(boolean[][] values) {
298         Boolean[][] newValues = new Boolean[values.length][];
299         for (int i = 0; i < values.length; i++) {
300             newValues[i] = new Boolean[values[i].length];
301             for (int j = 0; j < values[i].length; j++) {
302                 newValues[i][j] = Boolean.valueOf(values[i][j]);
303             }
304         }
305         mValue = newValues;
306     }
307
308     /**
309      * Constructs {@AttributeValue} with 2 dimensional array of
310      * Boolean type values.
311      *
312      * @param values
313      *            2 dimensional array of Boolean type values.
314      */
315     public AttributeValue(Boolean[][] values) {
316         mValue = values;
317     }
318
319     /**
320      * Constructs {@AttributeValue} with 2 dimensional array of
321      * String type values.
322      *
323      * @param values
324      *            2 dimensional array of String type values.
325      */
326     public AttributeValue(String[][] values) {
327         mValue = values;
328     }
329
330     /**
331      * Constructs {@AttributeValue} with 2 dimensional array of
332      * SimulatorResourceModel type values.
333      *
334      * @param values
335      *            2 dimensional array of SimulatorResourceModel type values.
336      */
337     public AttributeValue(SimulatorResourceModel[][] values) {
338         mValue = values;
339     }
340
341     /**
342      * Constructs {@AttributeValue} with 3 dimensional array of
343      * int type values.
344      *
345      * @param values
346      *            3 dimensional array of int type values.
347      */
348     public AttributeValue(int[][][] values) {
349         Integer[][][] newValues = new Integer[values.length][][];
350         for (int i = 0; i < values.length; i++) {
351             newValues[i] = new Integer[values[i].length][];
352             for (int j = 0; j < values[i].length; j++) {
353                 newValues[i][j] = new Integer[values[i][j].length];
354                 for (int k = 0; k < values[j].length; k++) {
355                     newValues[i][j][k] = Integer.valueOf(values[i][j][k]);
356                 }
357             }
358         }
359         mValue = newValues;
360     }
361
362     /**
363      * Constructs {@AttributeValue} with 3 dimensional array of
364      * Integer type values.
365      *
366      * @param values
367      *            3 dimensional array of Integer type values.
368      */
369     public AttributeValue(Integer[][][] values) {
370         mValue = values;
371     }
372
373     /**
374      * Constructs {@AttributeValue} with 3 dimensional array of
375      * double type values.
376      *
377      * @param values
378      *            3 dimensional array of double type values.
379      */
380     public AttributeValue(double[][][] values) {
381         Double[][][] newValues = new Double[values.length][][];
382         for (int i = 0; i < values.length; i++) {
383             newValues[i] = new Double[values[i].length][];
384             for (int j = 0; j < values[i].length; j++) {
385                 newValues[i][j] = new Double[values[i][j].length];
386                 for (int k = 0; k < values[j].length; k++) {
387                     newValues[i][j][k] = Double.valueOf(values[i][j][k]);
388                 }
389             }
390         }
391         mValue = newValues;
392     }
393
394     /**
395      * Constructs {@AttributeValue} with 3 dimensional array of
396      * Double type values.
397      *
398      * @param values
399      *            3 dimensional array of Double type values.
400      */
401     public AttributeValue(Double[][][] values) {
402         mValue = values;
403     }
404
405     /**
406      * Constructs {@AttributeValue} with 3 dimensional array of
407      * boolean type values.
408      *
409      * @param values
410      *            3 dimensional array of boolean type values.
411      */
412     public AttributeValue(boolean[][][] values) {
413         Boolean[][][] newValues = new Boolean[values.length][][];
414         for (int i = 0; i < values.length; i++) {
415             newValues[i] = new Boolean[values[i].length][];
416             for (int j = 0; j < values[i].length; j++) {
417                 newValues[i][j] = new Boolean[values[i][j].length];
418                 for (int k = 0; k < values[j].length; k++) {
419                     newValues[i][j][k] = Boolean.valueOf(values[i][j][k]);
420                 }
421             }
422         }
423         mValue = newValues;
424     }
425
426     /**
427      * Constructs {@AttributeValue} with 3 dimensional array of
428      * Boolean type values.
429      *
430      * @param values
431      *            3 dimensional array of Boolean type values.
432      */
433     public AttributeValue(Boolean[][][] values) {
434         mValue = values;
435     }
436
437     /**
438      * Constructs {@AttributeValue} with 3 dimensional array of
439      * String type values.
440      *
441      * @param values
442      *            3 dimensional array of String type values.
443      */
444     public AttributeValue(String[][][] values) {
445         mValue = values;
446     }
447
448     /**
449      * Constructs {@AttributeValue} with 3 dimensional array of
450      * SimulatorResourceModel type values.
451      *
452      * @param values
453      *            3 dimensional array of SimulatorResourceModel type values.
454      */
455     public AttributeValue(SimulatorResourceModel[][][] values) {
456         mValue = values;
457     }
458
459     /**
460      * API to get value type information.
461      *
462      * @return {@AttributeValue.TypeInfo}.
463      */
464     public TypeInfo typeInfo() {
465         return createTypeInfo(mValue);
466     }
467
468     /**
469      * API to get value as Object.
470      *
471      * @return Value as Object.
472      */
473     public Object get() {
474         return mValue;
475     }
476
477     private AttributeValue(Object value) {
478         mValue = value;
479     }
480
481     private TypeInfo createTypeInfo(Object value) {
482         TypeInfo typeInfo = new TypeInfo();
483         String className = value.getClass().getName();
484         if (className.contains(Integer.class.getName())) {
485             typeInfo.mBaseType = ValueType.INTEGER;
486             typeInfo.mType = ValueType.INTEGER;
487         } else if (className.contains(Double.class.getName())) {
488             typeInfo.mBaseType = ValueType.DOUBLE;
489             typeInfo.mType = ValueType.DOUBLE;
490         } else if (className.contains(Boolean.class.getName())) {
491             typeInfo.mBaseType = ValueType.BOOLEAN;
492             typeInfo.mType = ValueType.BOOLEAN;
493         } else if (className.contains(String.class.getName())) {
494             typeInfo.mBaseType = ValueType.STRING;
495             typeInfo.mType = ValueType.STRING;
496         } else if (className.contains(SimulatorResourceModel.class.getName())) {
497             typeInfo.mBaseType = ValueType.RESOURCEMODEL;
498             typeInfo.mType = ValueType.RESOURCEMODEL;
499         }
500
501         // For array types
502         if (value.getClass().isArray()) {
503             typeInfo.mType = ValueType.ARRAY;
504             for (char ch : className.toCharArray()) {
505                 if (ch == '[')
506                     typeInfo.mDepth++;
507             }
508         }
509
510         return typeInfo;
511     }
512 }