2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.oic.simulator;
20 * This class accommodate different type of values as Object.
22 public class AttributeValue {
24 private Object mValue = null;
27 * Enum to represent value type.
29 public enum ValueType {
30 UNKNOWN, INTEGER, DOUBLE, BOOLEAN, STRING, RESOURCEMODEL, ARRAY
34 * Class provides the value type information in detail.
36 public class TypeInfo {
37 public ValueType mType;
38 public ValueType mBaseType;
42 * Constructs {@AttributeValue.TypeInfo} with
46 mType = ValueType.UNKNOWN;
47 mBaseType = ValueType.UNKNOWN;
52 * Constructs {@AttributeValue.TypeInfo} with
53 * given {AttributeValue.ValueType}.
58 TypeInfo(ValueType type) {
65 * Constructs {@AttributeValue.TypeInfo} with
66 * given {AttributeValue.ValueType}s and depth information.
71 * Value base type. This type is useful when vaule is of
74 * Depth of array type value. This type is useful when vaule
77 TypeInfo(ValueType type, ValueType baseType, int depth) {
85 * Constructs {@AttributeValue} with int type value.
90 public AttributeValue(int value) {
91 mValue = new Integer(value);
95 * Constructs {@AttributeValue} with double type value.
100 public AttributeValue(double value) {
101 mValue = new Double(value);
105 * Constructs {@AttributeValue} with boolean type value.
108 * boolean type value.
110 public AttributeValue(boolean value) {
111 mValue = new Boolean(value);
115 * Constructs {@AttributeValue} with String type value.
120 public AttributeValue(String value) {
125 * Constructs {@AttributeValue} with SimulatorResourceModel
129 * SimulatorResourceModel type value.
131 public AttributeValue(SimulatorResourceModel value) {
136 * Constructs {@AttributeValue} with array of int type
140 * Array of int type values.
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]);
150 * Constructs {@AttributeValue} with array of Integer type
154 * Array of Integer type values.
156 public AttributeValue(Integer[] values) {
161 * Constructs {@AttributeValue} with array of double type
165 * Array of double type values.
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]);
175 * Constructs {@AttributeValue} with array of Double type
179 * Array of Double type values.
181 public AttributeValue(Double[] values) {
186 * Constructs {@AttributeValue} with array of boolean type
190 * Array of boolean type values.
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]);
200 * Constructs {@AttributeValue} with array of Boolean type
204 * Array of Boolean type values.
206 public AttributeValue(Boolean[] values) {
211 * Constructs {@AttributeValue} with array of String type
215 * Array of String type values.
217 public AttributeValue(String[] values) {
222 * Constructs {@AttributeValue} with array of
223 * SimulatorResourceModel type values.
226 * Array of SimulatorResourceModel type values.
228 public AttributeValue(SimulatorResourceModel[] values) {
233 * Constructs {@AttributeValue} with 2 dimensional array of
237 * 2 dimensional array of int type values.
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]);
251 * Constructs {@AttributeValue} with 2 dimensional array of
252 * Integer type values.
255 * 2 dimensional array of Integer type values.
257 public AttributeValue(Integer[][] values) {
262 * Constructs {@AttributeValue} with 2 dimensional array of
263 * double type values.
266 * 2 dimensional array of double type values.
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]);
280 * Constructs {@AttributeValue} with 2 dimensional array of
281 * Double type values.
284 * 2 dimensional array of Double type values.
286 public AttributeValue(Double[][] values) {
291 * Constructs {@AttributeValue} with 2 dimensional array of
292 * boolean type values.
295 * 2 dimensional array of boolean type values.
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]);
309 * Constructs {@AttributeValue} with 2 dimensional array of
310 * Boolean type values.
313 * 2 dimensional array of Boolean type values.
315 public AttributeValue(Boolean[][] values) {
320 * Constructs {@AttributeValue} with 2 dimensional array of
321 * String type values.
324 * 2 dimensional array of String type values.
326 public AttributeValue(String[][] values) {
331 * Constructs {@AttributeValue} with 2 dimensional array of
332 * SimulatorResourceModel type values.
335 * 2 dimensional array of SimulatorResourceModel type values.
337 public AttributeValue(SimulatorResourceModel[][] values) {
342 * Constructs {@AttributeValue} with 3 dimensional array of
346 * 3 dimensional array of int type values.
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]);
363 * Constructs {@AttributeValue} with 3 dimensional array of
364 * Integer type values.
367 * 3 dimensional array of Integer type values.
369 public AttributeValue(Integer[][][] values) {
374 * Constructs {@AttributeValue} with 3 dimensional array of
375 * double type values.
378 * 3 dimensional array of double type values.
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]);
395 * Constructs {@AttributeValue} with 3 dimensional array of
396 * Double type values.
399 * 3 dimensional array of Double type values.
401 public AttributeValue(Double[][][] values) {
406 * Constructs {@AttributeValue} with 3 dimensional array of
407 * boolean type values.
410 * 3 dimensional array of boolean type values.
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]);
427 * Constructs {@AttributeValue} with 3 dimensional array of
428 * Boolean type values.
431 * 3 dimensional array of Boolean type values.
433 public AttributeValue(Boolean[][][] values) {
438 * Constructs {@AttributeValue} with 3 dimensional array of
439 * String type values.
442 * 3 dimensional array of String type values.
444 public AttributeValue(String[][][] values) {
449 * Constructs {@AttributeValue} with 3 dimensional array of
450 * SimulatorResourceModel type values.
453 * 3 dimensional array of SimulatorResourceModel type values.
455 public AttributeValue(SimulatorResourceModel[][][] values) {
460 * API to get value type information.
462 * @return {@AttributeValue.TypeInfo}.
464 public TypeInfo typeInfo() {
465 return createTypeInfo(mValue);
469 * API to get value as Object.
471 * @return Value as Object.
473 public Object get() {
477 private AttributeValue(Object value) {
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;
502 if (value.getClass().isArray()) {
503 typeInfo.mType = ValueType.ARRAY;
504 for (char ch : className.toCharArray()) {