1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 package org.iotivity.service;
23 import java.util.Collections;
24 import java.util.HashMap;
28 * Value holds a value among various types at a time.
30 * Type helps identify type information of Value.
32 * @see RCSResourceAttributes
35 public final class RcsValue {
37 private static class NullType {
39 public String toString() {
45 * Identifiers for types of Value.
49 public static enum TypeId {
50 NULL, BOOLEAN, INTEGER, DOUBLE, STRING, ATTRIBUTES, ARRAY;
54 * A Helper class to identify types of Value.
56 * @see RCSResourceAttributes
60 public static class Type {
61 private final TypeId mTypeId;
68 * Returns type identifier.
70 * @return Identifier of type
74 public final TypeId getTypeId() {
78 protected TypeId getBaseTypeId() {
82 protected int getDepth() {
87 * Returns the type identifier of a base type of sequence.
89 * For non sequence types, it is equivalent to calling getId.
91 * @return identifier of type
96 public static TypeId getBaseTypeId(Type t) {
97 return t.getBaseTypeId();
101 * Returns the depth of a type.
103 * The return will be zero for non sequence types.
107 public static int getDepth(Type t) {
112 * Factory method to create Type instance from an object.
114 * @return An instance that has TypeId for obj.
116 * @note object must be a supported type by Value.
118 * @throws NullPointerException
120 * @throws IllegalArgumentException
121 * if obj is not supported type.
124 public static Type typeOf(Object obj) {
126 throw new NullPointerException("object is null");
129 return typeOf(obj.getClass());
133 * Factory method to create Type instance from a clss.
135 * @return An instance that has TypeId for class.
137 * @note class must be a supported type by Value.
139 * @throws NullPointerException
141 * @throws IllegalArgumentException
142 * if cls is not supported type.
145 public static Type typeOf(Class<?> cls) {
147 throw new NullPointerException("class is null");
150 if (sTypes.containsKey(cls)) return sTypes.get(cls);
152 throw new IllegalArgumentException(
153 cls.getSimpleName() + " is not supported type.");
157 private static class ArrayType extends Type {
158 private final TypeId mBaseTypeId;
159 private final int mDimension;
161 ArrayType(TypeId baseTypeId, int dimension) {
164 mBaseTypeId = baseTypeId;
165 mDimension = dimension;
169 protected TypeId getBaseTypeId() {
174 protected int getDepth() {
179 private static final NullType sNullValue = new NullType();
181 private static Map<Class<?>, Type> sTypes;
183 private final Object mObject;
187 final Map<Class<?>, Type> types = new HashMap<Class<?>, Type>();
189 types.put(NullType.class, new Type(TypeId.NULL));
190 types.put(Boolean.class, new Type(TypeId.BOOLEAN));
191 types.put(Integer.class, new Type(TypeId.INTEGER));
192 types.put(Double.class, new Type(TypeId.DOUBLE));
193 types.put(String.class, new Type(TypeId.STRING));
194 types.put(RcsResourceAttributes.class, new Type(TypeId.ATTRIBUTES));
196 types.put(boolean[].class, new ArrayType(TypeId.BOOLEAN, 1));
197 types.put(int[].class, new ArrayType(TypeId.INTEGER, 1));
198 types.put(double[].class, new ArrayType(TypeId.DOUBLE, 1));
199 types.put(String[].class, new ArrayType(TypeId.STRING, 1));
200 types.put(RcsResourceAttributes[].class,
201 new ArrayType(TypeId.ATTRIBUTES, 1));
203 types.put(boolean[][].class, new ArrayType(TypeId.BOOLEAN, 2));
204 types.put(int[][].class, new ArrayType(TypeId.INTEGER, 2));
205 types.put(double[][].class, new ArrayType(TypeId.DOUBLE, 2));
206 types.put(String[][].class, new ArrayType(TypeId.STRING, 2));
207 types.put(RcsResourceAttributes[][].class,
208 new ArrayType(TypeId.ATTRIBUTES, 2));
210 types.put(boolean[][][].class, new ArrayType(TypeId.BOOLEAN, 3));
211 types.put(int[][][].class, new ArrayType(TypeId.INTEGER, 3));
212 types.put(double[][][].class, new ArrayType(TypeId.DOUBLE, 3));
213 types.put(String[][][].class, new ArrayType(TypeId.STRING, 3));
214 types.put(RcsResourceAttributes[][][].class,
215 new ArrayType(TypeId.ATTRIBUTES, 3));
217 sTypes = Collections.unmodifiableMap(types);
221 static boolean isSupportedType(Class<?> cls) {
222 return sTypes.containsKey(cls);
225 static void verifySupportedType(Class<?> cls) {
226 if (!isSupportedType(cls)) {
227 throw new IllegalArgumentException(
228 cls.getSimpleName() + " is not supported type.");
233 * Constructs a new value with an object.
238 * @throws NullPointerException
240 * @throws IllegalArgumentException
241 * if value is not supported type.
243 public RcsValue(Object value) {
244 if (value == null) throw new NullPointerException("value is null!");
246 verifySupportedType(value.getClass());
252 * Constructs a new value that holds null value.
260 * Constructs a new value that holds null value.
265 public RcsValue(boolean value) {
266 this((Object) value);
270 * Constructs a new value that holds an int value.
275 public RcsValue(int value) {
276 this((Object) value);
280 * Constructs a new value that holds a double value.
285 public RcsValue(double value) {
286 this((Object) value);
290 * Constructs a new value that holds a String value.
295 * @throws NullPointerException
298 public RcsValue(String value) {
299 this((Object) value);
303 * Constructs a new value that holds a RcsResourceAttributes value.
306 * a RcsResourceAttributes
308 * @throws NullPointerException
311 public RcsValue(RcsResourceAttributes value) {
312 this((Object) value);
316 * Constructs a new value that holds a boolean array.
321 * @throws NullPointerException
324 public RcsValue(boolean[] value) {
325 this((Object) value);
329 * Constructs a new value that holds a two-dimensional boolean array.
332 * a two-dimensional boolean array
334 * @throws NullPointerException
337 public RcsValue(boolean[][] value) {
338 this((Object) value);
342 * Constructs a new value that holds a three-dimensional boolean array.
345 * a three-dimensional boolean array
347 * @throws NullPointerException
350 public RcsValue(boolean[][][] value) {
351 this((Object) value);
355 * Constructs a new value that holds an int array.
360 * @throws NullPointerException
363 public RcsValue(int[] value) {
364 this((Object) value);
368 * Constructs a new value that holds a two-dimensional int array.
371 * a two-dimensional int array
373 * @throws NullPointerException
376 public RcsValue(int[][] value) {
377 this((Object) value);
381 * Constructs a new value that holds a three-dimensional int array.
384 * a three-dimensional int array
386 * @throws NullPointerException
389 public RcsValue(int[][][] value) {
390 this((Object) value);
394 * Constructs a new value that holds a double array.
399 * @throws NullPointerException
402 public RcsValue(double[] value) {
403 this((Object) value);
407 * Constructs a new value that holds a two-dimensional double array.
410 * a two-dimensional double array
412 * @throws NullPointerException
415 public RcsValue(double[][] value) {
416 this((Object) value);
420 * Constructs a new value that holds a three-dimensional double array.
423 * a three-dimensional double array
425 * @throws NullPointerException
428 public RcsValue(double[][][] value) {
429 this((Object) value);
433 * Constructs a new value that holds a String array.
438 * @throws NullPointerException
441 public RcsValue(String[] value) {
442 this((Object) value);
446 * Constructs a new value that holds a two-dimensional String array.
449 * a two-dimensional String array
451 * @throws NullPointerException
454 public RcsValue(String[][] value) {
455 this((Object) value);
459 * Constructs a new value that holds a three-dimensional String array.
462 * a three-dimensional String array
464 * @throws NullPointerException
467 public RcsValue(String[][][] value) {
468 this((Object) value);
472 * Constructs a new value that holds a RcsResourceAttributes array.
475 * a RcsResourceAttributes array
477 * @throws NullPointerException
480 public RcsValue(RcsResourceAttributes[] value) {
481 this((Object) value);
485 * Constructs a new value that holds a two-dimensional RcsResourceAttributes
489 * a two-dimensional RcsResourceAttributes array
491 * @throws NullPointerException
494 public RcsValue(RcsResourceAttributes[][] value) {
495 this((Object) value);
499 * Constructs a new value that holds a three-dimensional
500 * RcsResourceAttributes array.
503 * a three-dimensional RcsResourceAttributes array
505 * @throws NullPointerException
508 public RcsValue(RcsResourceAttributes[][][] value) {
509 this((Object) value);
513 * Returns the value it represents is null.
515 * @return true if the value is null.
517 public boolean isNull() {
518 return isNullObject(mObject);
522 * Returns the object represents null for RcsValue.
525 * an object to be tested
527 * @return true if the object represents null.
529 public static boolean isNullObject(Object o) {
530 return o == sNullValue;
534 * Returns type information.
536 * @return type information for the value.
538 public Type getType() {
539 if (mType == null) mType = Type.typeOf(mObject);
544 * Returns the value as T.
546 * @return a value as T
548 * @throws ClassCastException
549 * if the value is not of T.
551 @SuppressWarnings("unchecked")
556 @SuppressWarnings("unchecked")
557 private <T> T getOrNull() {
560 } catch (final ClassCastException e) {
566 * Returns the value as an Object.
570 public Object asObject() {
575 * Returns the value as a boolean, false if the value is not the desired
578 * @return a boolean value
581 public boolean asBoolean() {
582 return asBoolean(false);
586 * Returns the value as a boolean.
588 * @param defaultValue
589 * value to return if the value is not boolean.
591 * @return a boolean value
594 public boolean asBoolean(boolean defaultValue) {
597 } catch (final ClassCastException e) {
603 * Returns the value as an int, 0 if the value is not the desired type.
605 * @return an int value
613 * Returns the value as an int.
615 * @param defaultValue
616 * value to return if the value is not int.
618 * @return an int value
621 public int asInt(int defaultValue) {
624 } catch (final ClassCastException e) {
630 * Returns the value as a double, 0 if the value is not the desired type.
632 * @return a double value
635 public double asDouble() {
640 * Returns the value as a double.
642 * @param defaultValue
643 * value to return if the value is not double.
645 * @return a double value
648 public double asDouble(double defaultValue) {
651 } catch (final ClassCastException e) {
657 * Returns the value as a string, null if the value is not the desired type.
659 * @return a string value
662 public String asString() {
663 return asString(null);
667 * Returns the value as a String.
669 * @param defaultValue
670 * value to return if the value is not String.
672 * @return a String value
675 public String asString(String defaultValue) {
678 } catch (final ClassCastException e) {
684 * Returns the value as a RcsResourceAttributes,
685 * null if the value is not the desired type.
687 * @return a RcsResourceAttributes value
690 public RcsResourceAttributes asAttributes() {
695 * Returns the value as a boolean array, null if the value is not the
698 * @return a boolean array
701 public boolean[] asBooleanArray() {
706 * Returns the value as a two-dimensional boolean array, null if the value
707 * is not the desired type.
709 * @return a two-dimensional boolean array
712 public boolean[][] asBoolean2DArray() {
717 * Returns the value as a three-dimensional boolean array, null if the value
718 * is not the desired type.
720 * @return a three-dimensional boolean array
723 public boolean[][][] asBoolean3DArray() {
728 * Returns the value as an int array, null if the value is not the
731 * @return an int array
734 public int[] asIntArray() {
739 * Returns the value as a two-dimensional int array, null if the value
740 * is not the desired type.
742 * @return a two-dimensional int array
745 public int[][] asInt2DArray() {
750 * Returns the value as a three-dimensional int array, null if the value
751 * is not the desired type.
753 * @return a three-dimensional int array
756 public int[][][] asInt3DArray() {
761 * Returns the value as a double array, null if the value is not the
764 * @return a double array
767 public double[] asDoubleArray() {
772 * Returns the value as a two-dimensional double array, null if the value
773 * is not the desired type.
775 * @return a two-dimensional double array
778 public double[][] asDouble2DArray() {
783 * Returns the value as a three-dimensional double array, null if the value
784 * is not the desired type.
786 * @return a three-dimensional double array
789 public double[][][] asDouble3DArray() {
794 * Returns the value as a string array, null if the value is not the
797 * @return a string array
800 public String[] asStringArray() {
805 * Returns the value as a two-dimensional string array, null if the value
806 * is not the desired type.
808 * @return a two-dimensional string array
811 public String[][] asString2DArray() {
816 * Returns the value as a three-dimensional string array, null if the value
817 * is not the desired type.
819 * @return a three-dimensional string array
822 public String[][][] asString3DArray() {
827 * Returns the value as an attributes array, null if the value is not the
830 * @return an attributes array
833 public RcsResourceAttributes[] asAttributesArray() {
838 * Returns the value as a two-dimensional attributes array, null if the
839 * value is not the desired type.
841 * @return a two-dimensional attributes array
844 public RcsResourceAttributes[][] asAttributes2DArray() {
849 * Returns the value as a three-dimensional attributes array, null if the
850 * value is not the desired type.
852 * @return a three-dimensional attributes array
855 public RcsResourceAttributes[][][] asAttributes3DArray() {
860 public boolean equals(Object o) {
861 if (o == this) return true;
862 if (!(o instanceof RcsValue)) return false;
864 final RcsValue rhs = (RcsValue) o;
866 return mObject.equals(rhs.mObject);
870 public int hashCode() {
871 return mObject.hashCode();
875 public String toString() {
876 return mObject.toString();