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 which provides the value type information in detail.
36 public class TypeInfo {
37 public ValueType mType;
38 public ValueType mBaseType;
42 * Constructs {@link AttributeValue.TypeInfo} with default values.
45 mType = ValueType.UNKNOWN;
46 mBaseType = ValueType.UNKNOWN;
51 * Constructs {@link AttributeValue.TypeInfo} with given
52 * {AttributeValue.ValueType}.
57 TypeInfo(ValueType type) {
64 * Constructs {@link AttributeValue.TypeInfo} with given
65 * {AttributeValue.ValueType}s and depth information.
70 * Value base type. This type is useful when value is of
73 * Depth of array type value. This is useful when value is of
76 TypeInfo(ValueType type, ValueType baseType, int depth) {
84 * Constructs {@link AttributeValue} with int type value.
89 public AttributeValue(int value) {
90 mValue = new Integer(value);
94 * Constructs {@link AttributeValue} with double type value.
99 public AttributeValue(double value) {
100 mValue = new Double(value);
104 * Constructs {@link AttributeValue} with boolean type value.
107 * boolean type value.
109 public AttributeValue(boolean value) {
110 mValue = Boolean.valueOf(value);
114 * Constructs {@link AttributeValue} with string type value.
119 public AttributeValue(String value) {
124 * Constructs {@link AttributeValue} with SimulatorResourceModel type value.
127 * SimulatorResourceModel type value.
129 public AttributeValue(SimulatorResourceModel value) {
134 * Constructs {@link AttributeValue} with array of int type values.
137 * Array of int type values.
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]);
147 * Constructs {@link AttributeValue} with array of Integer type values.
150 * Array of Integer type values.
152 public AttributeValue(Integer[] values) {
157 * Constructs {@link AttributeValue} with array of double type values.
160 * Array of double type values.
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]);
170 * Constructs {@link AttributeValue} with array of Double type values.
173 * Array of Double type values.
175 public AttributeValue(Double[] values) {
180 * Constructs {@link AttributeValue} with array of boolean type values.
183 * Array of boolean type values.
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]);
193 * Constructs {@link AttributeValue} with array of Boolean type values.
196 * Array of Boolean type values.
198 public AttributeValue(Boolean[] values) {
203 * Constructs {@link AttributeValue} with array of String type values.
206 * Array of String type values.
208 public AttributeValue(String[] values) {
213 * Constructs {@link AttributeValue} with array of SimulatorResourceModel
217 * Array of SimulatorResourceModel type values.
219 public AttributeValue(SimulatorResourceModel[] values) {
224 * Constructs {@link AttributeValue} with 2 dimensional array of int type
228 * 2 dimensional array of int type values.
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]);
242 * Constructs {@link AttributeValue} with 2 dimensional array of Integer
246 * 2 dimensional array of Integer type values.
248 public AttributeValue(Integer[][] values) {
253 * Constructs {@link AttributeValue} with 2 dimensional array of double type
257 * 2 dimensional array of double type values.
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]);
271 * Constructs {@link AttributeValue} with 2 dimensional array of Double type
275 * 2 dimensional array of Double type values.
277 public AttributeValue(Double[][] values) {
282 * Constructs {@link AttributeValue} with 2 dimensional array of boolean
286 * 2 dimensional array of boolean type values.
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]);
300 * Constructs {@link AttributeValue} with 2 dimensional array of Boolean
304 * 2 dimensional array of Boolean type values.
306 public AttributeValue(Boolean[][] values) {
311 * Constructs {@link AttributeValue} with 2 dimensional array of String type
315 * 2 dimensional array of String type values.
317 public AttributeValue(String[][] values) {
322 * Constructs {@link AttributeValue} with 2 dimensional array of
323 * SimulatorResourceModel type values.
326 * 2 dimensional array of SimulatorResourceModel type values.
328 public AttributeValue(SimulatorResourceModel[][] values) {
333 * Constructs {@link AttributeValue} with 3 dimensional array of int type
337 * 3 dimensional array of int type values.
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]);
354 * Constructs {@link AttributeValue} with 3 dimensional array of Integer
358 * 3 dimensional array of Integer type values.
360 public AttributeValue(Integer[][][] values) {
365 * Constructs {@link AttributeValue} with 3 dimensional array of double type
369 * 3 dimensional array of double type values.
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]);
386 * Constructs {@link AttributeValue} with 3 dimensional array of Double type
390 * 3 dimensional array of Double type values.
392 public AttributeValue(Double[][][] values) {
397 * Constructs {@link AttributeValue} with 3 dimensional array of boolean
401 * 3 dimensional array of boolean type values.
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]);
418 * Constructs {@link AttributeValue} with 3 dimensional array of Boolean
422 * 3 dimensional array of Boolean type values.
424 public AttributeValue(Boolean[][][] values) {
429 * Constructs {@link AttributeValue} with 3 dimensional array of String type
433 * 3 dimensional array of String type values.
435 public AttributeValue(String[][][] values) {
440 * Constructs {@link AttributeValue} with 3 dimensional array of
441 * SimulatorResourceModel type values.
444 * 3 dimensional array of SimulatorResourceModel type values.
446 public AttributeValue(SimulatorResourceModel[][][] values) {
451 * API to get value type information.
453 * @return {@link AttributeValue.TypeInfo}.
455 public TypeInfo typeInfo() {
456 return createTypeInfo(mValue);
460 * API to get value as Object.
462 * @return Value as Object.
464 public Object get() {
468 private AttributeValue(Object value) {
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;
493 if (value.getClass().isArray()) {
494 typeInfo.mType = ValueType.ARRAY;
495 for (char ch : className.toCharArray()) {