Class Data
- java.lang.Object
-
- com.google.api.client.util.Data
-
public class Data extends java.lang.ObjectUtilities for working with key/value data based on theKeyannotation.- Since:
- 1.4
-
-
Field Summary
Fields Modifier and Type Field Description static java.math.BigDecimalNULL_BIG_DECIMALThe single instance of the magic null object for aBigDecimal.static java.math.BigIntegerNULL_BIG_INTEGERThe single instance of the magic null object for aBigInteger.static java.lang.BooleanNULL_BOOLEANThe single instance of the magic null object for aBoolean.static java.lang.ByteNULL_BYTEThe single instance of the magic null object for aByte.private static java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,java.lang.Object>NULL_CACHECache of the magic null object for the given Java class.static java.lang.CharacterNULL_CHARACTERThe single instance of the magic null object for aCharacter.static DateTimeNULL_DATE_TIMEThe single instance of the magic null object for aDateTime.static java.lang.DoubleNULL_DOUBLEThe single instance of the magic null object for aDouble.static java.lang.FloatNULL_FLOATThe single instance of the magic null object for aFloat.static java.lang.IntegerNULL_INTEGERThe single instance of the magic null object for aInteger.static java.lang.LongNULL_LONGThe single instance of the magic null object for aLong.static java.lang.ShortNULL_SHORTThe single instance of the magic null object for aShort.static java.lang.StringNULL_STRINGThe single instance of the magic null object for aString.
-
Constructor Summary
Constructors Constructor Description Data()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tclone(T data)Returns a deep clone of the given key/value data, such that the result is a completely independent copy.static voiddeepCopy(java.lang.Object src, java.lang.Object dest)Makes a deep copy of the given source object into the destination object that is assumed to be constructed usingObject.clone().static booleanisNull(java.lang.Object object)Returns whether the given object is the magic object that represents the null value of its class.static booleanisPrimitive(java.lang.reflect.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 booleanisValueOfPrimitiveType(java.lang.Object fieldValue)Returns whether to given value isnullor its class is primitive as defined byisPrimitive(Type).static java.util.Map<java.lang.String,java.lang.Object>mapOf(java.lang.Object data)Returns the map to use for the given data that is treated as a map from string key to some value.static java.util.Collection<java.lang.Object>newCollectionInstance(java.lang.reflect.Type type)Returns a new collection instance for the given type.static java.util.Map<java.lang.String,java.lang.Object>newMapInstance(java.lang.Class<?> mapClass)Returns a new instance of a map based on the given field class.static <T> TnullOf(java.lang.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 java.lang.ObjectparsePrimitiveValue(java.lang.reflect.Type type, java.lang.String stringValue)Parses the given string value based on the given primitive type.static java.lang.reflect.TyperesolveWildcardTypeOrTypeVariable(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.Type type)Aggressively resolves the given type in such a way that the resolved type is not a wildcard type or a type variable, returningObject.classif the type variable cannot be resolved.
-
-
-
Field Detail
-
NULL_BOOLEAN
public static final java.lang.Boolean NULL_BOOLEAN
The single instance of the magic null object for aBoolean.
-
NULL_STRING
public static final java.lang.String NULL_STRING
The single instance of the magic null object for aString.
-
NULL_CHARACTER
public static final java.lang.Character NULL_CHARACTER
The single instance of the magic null object for aCharacter.
-
NULL_BYTE
public static final java.lang.Byte NULL_BYTE
The single instance of the magic null object for aByte.
-
NULL_SHORT
public static final java.lang.Short NULL_SHORT
The single instance of the magic null object for aShort.
-
NULL_INTEGER
public static final java.lang.Integer NULL_INTEGER
The single instance of the magic null object for aInteger.
-
NULL_FLOAT
public static final java.lang.Float NULL_FLOAT
The single instance of the magic null object for aFloat.
-
NULL_LONG
public static final java.lang.Long NULL_LONG
The single instance of the magic null object for aLong.
-
NULL_DOUBLE
public static final java.lang.Double NULL_DOUBLE
The single instance of the magic null object for aDouble.
-
NULL_BIG_INTEGER
public static final java.math.BigInteger NULL_BIG_INTEGER
The single instance of the magic null object for aBigInteger.
-
NULL_BIG_DECIMAL
public static final java.math.BigDecimal NULL_BIG_DECIMAL
The single instance of the magic null object for aBigDecimal.
-
NULL_DATE_TIME
public static final DateTime NULL_DATE_TIME
The single instance of the magic null object for aDateTime.
-
NULL_CACHE
private static final java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,java.lang.Object> NULL_CACHE
Cache of the magic null object for the given Java class.
-
-
Method Detail
-
nullOf
public static <T> T nullOf(java.lang.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:
java.lang.IllegalArgumentException- if unable to create a new instance
-
isNull
public static boolean isNull(java.lang.Object object)
Returns whether the given object is the magic object that represents the null value of its class.- Parameters:
object- object ornull- Returns:
- whether it is the magic null value or
falsefornullinput
-
mapOf
public static java.util.Map<java.lang.String,java.lang.Object> mapOf(java.lang.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 theKeyannotation, and null is not a possible map value, although the magic null instance is possible (seenullOf(Class)andisNull(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 exampleMap.size()must check the number of non-null fields.- Parameters:
data- any key value data, represented by an object or a map, ornull- 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 usedeepCopy(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 ornullfor anullreturn value- Returns:
- deep clone or
nullfornullinput
-
deepCopy
public static void deepCopy(java.lang.Object src, java.lang.Object dest)Makes a deep copy of the given source object into the destination object that is assumed to be constructed usingObject.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 objectdest- destination object of identical type as source object, and any contained arrays must be the same length
-
isPrimitive
public static boolean isPrimitive(java.lang.reflect.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.A primitive class is any class for whom
Class.isPrimitive()is true, as well as any classes of type:Character,String,Integer,Long,Short,Byte,Float,Double,BigInteger,BigDecimal,Boolean, andDateTime.- Parameters:
type- type ornullforfalseresult- Returns:
- whether it is a primitive
-
isValueOfPrimitiveType
public static boolean isValueOfPrimitiveType(java.lang.Object fieldValue)
Returns whether to given value isnullor its class is primitive as defined byisPrimitive(Type).
-
parsePrimitiveValue
public static java.lang.Object parsePrimitiveValue(java.lang.reflect.Type type, java.lang.String stringValue)Parses the given string value based on the given primitive type.Types are parsed as follows:
Void: nullnullor is assignable fromString(likeObject): no parsingcharorCharacter:String.charAt(0) (requires length to be exactly 1)booleanorBoolean:Boolean.valueOf(String)byteorByte:Byte.valueOf(String)shortorShort:Short.valueOf(String)intorInteger:Integer.valueOf(String)longorLong:Long.valueOf(String)floatorFloat:Float.valueOf(String)doubleorDouble:Double.valueOf(String)BigInteger:BigInteger(String)BigDecimal:BigDecimal(String)DateTime:DateTime.parseRfc3339(String)
Note that this may not be the right behavior for some use cases.
- Parameters:
type- primitive type ornullto parse as a stringstringValue- string value to parse ornullfornullresult- Returns:
- parsed object or
nullfornullinput - Throws:
java.lang.IllegalArgumentException- if the given class is not a primitive class
-
newCollectionInstance
public static java.util.Collection<java.lang.Object> newCollectionInstance(java.lang.reflect.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:
nullor an array or assignable fromArrayList(likeListorCollectionorObject): returns anArrayList- assignable from
HashSet: returns aHashSet - assignable from
TreeSet: returns aTreeSet - else: calls
Types.newInstance(Class)
- Parameters:
type- type ornullforArrayList.- Returns:
- new collection instance
- Throws:
java.lang.ClassCastException- if result is does not extendCollection
-
newMapInstance
public static java.util.Map<java.lang.String,java.lang.Object> newMapInstance(java.lang.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:
nullor assignable fromArrayMap(likeMaporObject): returns anArrayMap- assignable from
TreeMap(likeSortedMap): returns aTreeMap - else: calls
Types.newInstance(Class)
- Parameters:
mapClass- field class- Throws:
java.lang.ClassCastException- if result is does not extendMap
-
resolveWildcardTypeOrTypeVariable
public static java.lang.reflect.Type resolveWildcardTypeOrTypeVariable(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.Type type)Aggressively resolves the given type in such a way that the resolved type is not a wildcard type or a type variable, returningObject.classif 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 fieldtype- type ornullfornullresult- Returns:
- resolved type (which may be class, parameterized type, or generic array type, but not
wildcard type or type variable) or
nullfornullinput
-
-