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
72 * @see #getBaseTypeId(RcsValue.Type)
74 public final TypeId getId() {
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 {@link #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.
113 * Note that object must be a supported type by RcsValue.
115 * @return An instance that has TypeId for obj.
117 * @throws NullPointerException
119 * @throws IllegalArgumentException
120 * if obj is not supported type.
123 public static Type typeOf(Object obj) {
125 throw new NullPointerException("object is null");
128 return typeOf(obj.getClass());
132 * Factory method to create Type instance from a class.
133 * Note that class must be a supported type by RcsValue.
135 * @return An instance that has TypeId for class.
137 * @throws NullPointerException
139 * @throws IllegalArgumentException
140 * if cls is not supported type.
143 public static Type typeOf(Class<?> cls) {
145 throw new NullPointerException("class is null");
148 if (sTypes.containsKey(cls)) return sTypes.get(cls);
150 throw new IllegalArgumentException(
151 cls.getSimpleName() + " is not supported type.");
155 private static class ArrayType extends Type {
156 private final TypeId mBaseTypeId;
157 private final int mDimension;
159 ArrayType(TypeId baseTypeId, int dimension) {
162 mBaseTypeId = baseTypeId;
163 mDimension = dimension;
167 protected TypeId getBaseTypeId() {
172 protected int getDepth() {
177 private static final NullType sNullValue = new NullType();
179 private static Map<Class<?>, Type> sTypes;
181 private final Object mObject;
185 final Map<Class<?>, Type> types = new HashMap<Class<?>, Type>();
187 types.put(NullType.class, new Type(TypeId.NULL));
188 types.put(Boolean.class, new Type(TypeId.BOOLEAN));
189 types.put(Integer.class, new Type(TypeId.INTEGER));
190 types.put(Double.class, new Type(TypeId.DOUBLE));
191 types.put(String.class, new Type(TypeId.STRING));
192 types.put(RcsResourceAttributes.class, new Type(TypeId.ATTRIBUTES));
194 types.put(boolean[].class, new ArrayType(TypeId.BOOLEAN, 1));
195 types.put(int[].class, new ArrayType(TypeId.INTEGER, 1));
196 types.put(double[].class, new ArrayType(TypeId.DOUBLE, 1));
197 types.put(String[].class, new ArrayType(TypeId.STRING, 1));
198 types.put(RcsResourceAttributes[].class,
199 new ArrayType(TypeId.ATTRIBUTES, 1));
201 types.put(boolean[][].class, new ArrayType(TypeId.BOOLEAN, 2));
202 types.put(int[][].class, new ArrayType(TypeId.INTEGER, 2));
203 types.put(double[][].class, new ArrayType(TypeId.DOUBLE, 2));
204 types.put(String[][].class, new ArrayType(TypeId.STRING, 2));
205 types.put(RcsResourceAttributes[][].class,
206 new ArrayType(TypeId.ATTRIBUTES, 2));
208 types.put(boolean[][][].class, new ArrayType(TypeId.BOOLEAN, 3));
209 types.put(int[][][].class, new ArrayType(TypeId.INTEGER, 3));
210 types.put(double[][][].class, new ArrayType(TypeId.DOUBLE, 3));
211 types.put(String[][][].class, new ArrayType(TypeId.STRING, 3));
212 types.put(RcsResourceAttributes[][][].class,
213 new ArrayType(TypeId.ATTRIBUTES, 3));
215 sTypes = Collections.unmodifiableMap(types);
219 static boolean isSupportedType(Class<?> cls) {
220 return sTypes.containsKey(cls);
223 static void verifySupportedType(Class<?> cls) {
224 if (!isSupportedType(cls)) {
225 throw new IllegalArgumentException(
226 cls.getSimpleName() + " is not supported type.");
231 * Constructs a new value with an object.
236 * @throws NullPointerException
238 * @throws IllegalArgumentException
239 * if value is not supported type.
241 public RcsValue(Object value) {
242 if (value == null) throw new NullPointerException("value is null!");
244 verifySupportedType(value.getClass());
250 * Constructs a new value that holds null value.
258 * Constructs a new value that holds a boolean value.
263 public RcsValue(boolean value) {
264 this((Object) value);
268 * Constructs a new value that holds an int value.
273 public RcsValue(int value) {
274 this((Object) value);
278 * Constructs a new value that holds a double value.
283 public RcsValue(double value) {
284 this((Object) value);
288 * Constructs a new value that holds a String value.
293 * @throws NullPointerException
296 public RcsValue(String value) {
297 this((Object) value);
301 * Constructs a new value that holds a RcsResourceAttributes value.
304 * a RcsResourceAttributes
306 * @throws NullPointerException
309 public RcsValue(RcsResourceAttributes value) {
310 this((Object) value);
314 * Constructs a new value that holds a boolean array.
319 * @throws NullPointerException
322 public RcsValue(boolean[] value) {
323 this((Object) value);
327 * Constructs a new value that holds a two-dimensional boolean array.
330 * a two-dimensional boolean array
332 * @throws NullPointerException
335 public RcsValue(boolean[][] value) {
336 this((Object) value);
340 * Constructs a new value that holds a three-dimensional boolean array.
343 * a three-dimensional boolean array
345 * @throws NullPointerException
348 public RcsValue(boolean[][][] value) {
349 this((Object) value);
353 * Constructs a new value that holds an int array.
358 * @throws NullPointerException
361 public RcsValue(int[] value) {
362 this((Object) value);
366 * Constructs a new value that holds a two-dimensional int array.
369 * a two-dimensional int array
371 * @throws NullPointerException
374 public RcsValue(int[][] value) {
375 this((Object) value);
379 * Constructs a new value that holds a three-dimensional int array.
382 * a three-dimensional int array
384 * @throws NullPointerException
387 public RcsValue(int[][][] value) {
388 this((Object) value);
392 * Constructs a new value that holds a double array.
397 * @throws NullPointerException
400 public RcsValue(double[] value) {
401 this((Object) value);
405 * Constructs a new value that holds a two-dimensional double array.
408 * a two-dimensional double array
410 * @throws NullPointerException
413 public RcsValue(double[][] value) {
414 this((Object) value);
418 * Constructs a new value that holds a three-dimensional double array.
421 * a three-dimensional double array
423 * @throws NullPointerException
426 public RcsValue(double[][][] value) {
427 this((Object) value);
431 * Constructs a new value that holds a String array.
436 * @throws NullPointerException
439 public RcsValue(String[] value) {
440 this((Object) value);
444 * Constructs a new value that holds a two-dimensional String array.
447 * a two-dimensional String array
449 * @throws NullPointerException
452 public RcsValue(String[][] value) {
453 this((Object) value);
457 * Constructs a new value that holds a three-dimensional String array.
460 * a three-dimensional String array
462 * @throws NullPointerException
465 public RcsValue(String[][][] value) {
466 this((Object) value);
470 * Constructs a new value that holds a RcsResourceAttributes array.
473 * a RcsResourceAttributes array
475 * @throws NullPointerException
478 public RcsValue(RcsResourceAttributes[] value) {
479 this((Object) value);
483 * Constructs a new value that holds a two-dimensional RcsResourceAttributes
487 * a two-dimensional RcsResourceAttributes array
489 * @throws NullPointerException
492 public RcsValue(RcsResourceAttributes[][] value) {
493 this((Object) value);
497 * Constructs a new value that holds a three-dimensional
498 * RcsResourceAttributes array.
501 * a three-dimensional RcsResourceAttributes array
503 * @throws NullPointerException
506 public RcsValue(RcsResourceAttributes[][][] value) {
507 this((Object) value);
511 * Returns whether the value is null.
513 * @return true if the value is null.
515 public boolean isNull() {
516 return isNullObject(mObject);
520 * Returns whether the object represents null for RcsValue.
523 * an object to be tested
525 * @return true if the object represents null.
527 public static boolean isNullObject(Object o) {
528 return o == sNullValue;
532 * Returns type information.
534 * @return type information for the value.
536 public Type getType() {
537 if (mType == null) mType = Type.typeOf(mObject);
542 * Returns the value as T.
544 * @return a value as T
546 * @throws ClassCastException
547 * if the value is not of T.
549 @SuppressWarnings("unchecked")
554 @SuppressWarnings("unchecked")
555 private <T> T getOrNull() {
558 } catch (final ClassCastException e) {
564 * Returns the value as an Object.
568 public Object asObject() {
573 * Returns the value as a boolean, false if the value is not the desired
576 * @return a boolean value
579 public boolean asBoolean() {
580 return asBoolean(false);
584 * Returns the value as a boolean.
586 * @param defaultValue
587 * value to return if the value is not boolean.
589 * @return a boolean value
592 public boolean asBoolean(boolean defaultValue) {
595 } catch (final ClassCastException e) {
601 * Returns the value as an int, 0 if the value is not the desired type.
603 * @return an int value
611 * Returns the value as an int.
613 * @param defaultValue
614 * value to return if the value is not int.
616 * @return an int value
619 public int asInt(int defaultValue) {
622 } catch (final ClassCastException e) {
628 * Returns the value as a double, 0 if the value is not the desired type.
630 * @return a double value
633 public double asDouble() {
638 * Returns the value as a double.
640 * @param defaultValue
641 * value to return if the value is not double.
643 * @return a double value
646 public double asDouble(double defaultValue) {
649 } catch (final ClassCastException e) {
655 * Returns the value as a string, null if the value is not the desired type.
657 * @return a string value
660 public String asString() {
661 return asString(null);
665 * Returns the value as a String.
667 * @param defaultValue
668 * value to return if the value is not String.
670 * @return a String value
673 public String asString(String defaultValue) {
676 } catch (final ClassCastException e) {
682 * Returns the value as a RcsResourceAttributes,
683 * null if the value is not the desired type.
685 * @return a RcsResourceAttributes value
688 public RcsResourceAttributes asAttributes() {
693 * Returns the value as a boolean array, null if the value is not the
696 * @return a boolean array
699 public boolean[] asBooleanArray() {
704 * Returns the value as a two-dimensional boolean array, null if the value
705 * is not the desired type.
707 * @return a two-dimensional boolean array
710 public boolean[][] asBoolean2DArray() {
715 * Returns the value as a three-dimensional boolean array, null if the value
716 * is not the desired type.
718 * @return a three-dimensional boolean array
721 public boolean[][][] asBoolean3DArray() {
726 * Returns the value as an int array, null if the value is not the
729 * @return an int array
732 public int[] asIntArray() {
737 * Returns the value as a two-dimensional int array, null if the value
738 * is not the desired type.
740 * @return a two-dimensional int array
743 public int[][] asInt2DArray() {
748 * Returns the value as a three-dimensional int array, null if the value
749 * is not the desired type.
751 * @return a three-dimensional int array
754 public int[][][] asInt3DArray() {
759 * Returns the value as a double array, null if the value is not the
762 * @return a double array
765 public double[] asDoubleArray() {
770 * Returns the value as a two-dimensional double array, null if the value
771 * is not the desired type.
773 * @return a two-dimensional double array
776 public double[][] asDouble2DArray() {
781 * Returns the value as a three-dimensional double array, null if the value
782 * is not the desired type.
784 * @return a three-dimensional double array
787 public double[][][] asDouble3DArray() {
792 * Returns the value as a string array, null if the value is not the
795 * @return a string array
798 public String[] asStringArray() {
803 * Returns the value as a two-dimensional string array, null if the value
804 * is not the desired type.
806 * @return a two-dimensional string array
809 public String[][] asString2DArray() {
814 * Returns the value as a three-dimensional string array, null if the value
815 * is not the desired type.
817 * @return a three-dimensional string array
820 public String[][][] asString3DArray() {
825 * Returns the value as an attributes array, null if the value is not the
828 * @return an attributes array
831 public RcsResourceAttributes[] asAttributesArray() {
836 * Returns the value as a two-dimensional attributes array, null if the
837 * value is not the desired type.
839 * @return a two-dimensional attributes array
842 public RcsResourceAttributes[][] asAttributes2DArray() {
847 * Returns the value as a three-dimensional attributes array, null if the
848 * value is not the desired type.
850 * @return a three-dimensional attributes array
853 public RcsResourceAttributes[][][] asAttributes3DArray() {
858 public boolean equals(Object o) {
859 if (o == this) return true;
860 if (!(o instanceof RcsValue)) return false;
862 final RcsValue rhs = (RcsValue) o;
864 return mObject.equals(rhs.mObject);
868 public int hashCode() {
869 return mObject.hashCode();
873 public String toString() {
874 return mObject.toString();