com.google.api.client.util
Class Data

java.lang.Object
  extended by com.google.api.client.util.Data

public class Data
extends Object

Utilities for working with key/value data based on the Key annotation.

Since:
1.4
Author:
Yaniv Inbar

Field Summary
static BigDecimal NULL_BIG_DECIMAL
          The single instance of the magic null object for a BigDecimal.
static BigInteger NULL_BIG_INTEGER
          The single instance of the magic null object for a BigInteger.
static Boolean NULL_BOOLEAN
          The single instance of the magic null object for a Boolean.
static Byte NULL_BYTE
          The single instance of the magic null object for a Byte.
static Character NULL_CHARACTER
          The single instance of the magic null object for a Character.
static DateTime NULL_DATE_TIME
          The single instance of the magic null object for a DateTime.
static Double NULL_DOUBLE
          The single instance of the magic null object for a Double.
static Float NULL_FLOAT
          The single instance of the magic null object for a Float.
static Integer NULL_INTEGER
          The single instance of the magic null object for a Integer.
static Long NULL_LONG
          The single instance of the magic null object for a Long.
static Short NULL_SHORT
          The single instance of the magic null object for a Short.
static String NULL_STRING
          The single instance of the magic null object for a String.
 
Constructor Summary
Data()
           
 
Method Summary
static
<T> T
clone(T data)
          Returns a deep clone of the given key/value data, such that the result is a completely independent copy.
static void deepCopy(Object src, Object dest)
          Makes a deep copy of the given source object into the destination object that is assumed to be constructed using Object.clone().
static boolean isNull(Object object)
          Returns whether the given object is the magic object that represents the null value of its class.
static boolean isPrimitive(Type type)
          Returns whether the given type is one of the supported primitive classes like number and date/time, or is a wildcard of one.
static boolean isValueOfPrimitiveType(Object fieldValue)
          Returns whether to given value is null or its class is primitive as defined by isPrimitive(Type).
static Map<String,Object> mapOf(Object data)
          Returns the map to use for the given data that is treated as a map from string key to some value.
static Collection<Object> newCollectionInstance(Type type)
          Returns a new collection instance for the given type.
static Map<String,Object> newMapInstance(Class<?> mapClass)
          Returns a new instance of a map based on the given field class.
static
<T> T
nullOf(Class<?> objClass)
          Returns the single instance of the magic object that represents the "null" value for the given Java class (including array or enum).
static Object parsePrimitiveValue(Type type, String stringValue)
          Parses the given string value based on the given primitive type.
static Type resolveWildcardTypeOrTypeVariable(List<Type> context, Type type)
          Aggressively resolves the given type in such a way that the resolved type is not a wildcard type or a type variable, returning Object.class if the type variable cannot be resolved.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL_BOOLEAN

public static final Boolean NULL_BOOLEAN
The single instance of the magic null object for a Boolean.


NULL_STRING

public static final String NULL_STRING
The single instance of the magic null object for a String.


NULL_CHARACTER

public static final Character NULL_CHARACTER
The single instance of the magic null object for a Character.


NULL_BYTE

public static final Byte NULL_BYTE
The single instance of the magic null object for a Byte.


NULL_SHORT

public static final Short NULL_SHORT
The single instance of the magic null object for a Short.


NULL_INTEGER

public static final Integer NULL_INTEGER
The single instance of the magic null object for a Integer.


NULL_FLOAT

public static final Float NULL_FLOAT
The single instance of the magic null object for a Float.


NULL_LONG

public static final Long NULL_LONG
The single instance of the magic null object for a Long.


NULL_DOUBLE

public static final Double NULL_DOUBLE
The single instance of the magic null object for a Double.


NULL_BIG_INTEGER

public static final BigInteger NULL_BIG_INTEGER
The single instance of the magic null object for a BigInteger.


NULL_BIG_DECIMAL

public static final BigDecimal NULL_BIG_DECIMAL
The single instance of the magic null object for a BigDecimal.


NULL_DATE_TIME

public static final DateTime NULL_DATE_TIME
The single instance of the magic null object for a DateTime.

Constructor Detail

Data

public Data()
Method Detail

nullOf

public static <T> T nullOf(Class<?> objClass)
Returns the single instance of the magic object that represents the "null" value for the given Java class (including array or enum).

Parameters:
objClass - class of the object needed
Returns:
magic object instance that represents the "null" value (not Java null)
Throws:
IllegalArgumentException - if unable to create a new instance

isNull

public static boolean isNull(Object object)
Returns whether the given object is the magic object that represents the null value of its class.

Parameters:
object - object or null
Returns:
whether it is the magic null value or false for null input

mapOf

public static Map<String,Object> mapOf(Object data)
Returns the map to use for the given data that is treated as a map from string key to some value.

If the input is null, it returns an empty map. If the input is a map, it simply returns the input. Otherwise, it will create a map view using reflection that is backed by the object, so that any changes to the map will be reflected on the object. The map keys of that map view are based on the Key annotation, and null is not a possible map value, although the magic null instance is possible (see nullOf(Class) and isNull(Object)). Iteration order of the data keys is based on the sorted (ascending) key names of the declared fields. Note that since the map view is backed by the object, and that the object may change, many methods in the map view must recompute the field values using reflection, for example Map.size() must check the number of non-null fields.

Parameters:
data - any key value data, represented by an object or a map, or null
Returns:
key/value map to use

clone

public static <T> T clone(T data)
Returns a deep clone of the given key/value data, such that the result is a completely independent copy.

This should not be used directly in the implementation of Object.clone(). Instead use deepCopy(Object, Object) for that purpose.

Final fields cannot be changed and therefore their value won't be copied.

Parameters:
data - key/value data object or map to clone or null for a null return value
Returns:
deep clone or null for null input

deepCopy

public static void deepCopy(Object src,
                            Object dest)
Makes a deep copy of the given source object into the destination object that is assumed to be constructed using Object.clone().

Example usage of this method in Object.clone():

  @Override
  public MyObject clone() {
    try {
      @SuppressWarnings("unchecked")
      MyObject result = (MyObject) super.clone();
      Data.deepCopy(this, result);
      return result;
    } catch (CloneNotSupportedException e) {
      throw new IllegalStateException(e);
    }
  }
 

Final fields cannot be changed and therefore their value won't be copied.

Parameters:
src - source object (non-primitive as defined by isPrimitive(Type)
dest - destination object of identical type as source object, and any contained arrays must be the same length

isPrimitive

public static boolean isPrimitive(Type type)
Returns whether the given type is one of the supported primitive classes like number and date/time, or is a wildcard of one.

Parameters:
type - type or null for false result
Returns:
whether it is a primitive

isValueOfPrimitiveType

public static boolean isValueOfPrimitiveType(Object fieldValue)
Returns whether to given value is null or its class is primitive as defined by isPrimitive(Type).


parsePrimitiveValue

public static Object parsePrimitiveValue(Type type,
                                         String stringValue)
Parses the given string value based on the given primitive type.

Types are parsed as follows:

Note that this may not be the right behavior for some use cases.

Parameters:
type - primitive type or null to parse as a string
stringValue - string value to parse or null for null result
Returns:
parsed object or null for null input
Throws:
IllegalArgumentException - if the given class is not a primitive class

newCollectionInstance

public static Collection<Object> newCollectionInstance(Type type)
Returns a new collection instance for the given type.

Creates a new collection instance specified for the first input collection class that matches as follows:

Parameters:
type - type or null for ArrayList.
Returns:
new collection instance
Throws:
ClassCastException - if result is does not extend Collection

newMapInstance

public static Map<String,Object> newMapInstance(Class<?> mapClass)
Returns a new instance of a map based on the given field class.

Creates a new map instance specified for the first input map class that matches as follows:

Parameters:
mapClass - field class
Throws:
ClassCastException - if result is does not extend Map

resolveWildcardTypeOrTypeVariable

public static Type resolveWildcardTypeOrTypeVariable(List<Type> context,
                                                     Type type)
Aggressively resolves the given type in such a way that the resolved type is not a wildcard type or a type variable, returning Object.class if the type variable cannot be resolved.

Parameters:
context - context list, ordering from least specific to most specific type context, for example container class and then its field
type - type or null for null result
Returns:
resolved type (which may be class, parameterized type, or generic array type, but not wildcard type or type variable) or null for null input


Copyright © 2011 Google. All Rights Reserved.