public class Data
extends java.lang.Object
Key
annotation.Modifier and Type | Field and Description |
---|---|
static java.math.BigDecimal |
NULL_BIG_DECIMAL
The single instance of the magic null object for a
BigDecimal . |
static java.math.BigInteger |
NULL_BIG_INTEGER
The single instance of the magic null object for a
BigInteger . |
static java.lang.Boolean |
NULL_BOOLEAN
The single instance of the magic null object for a
Boolean . |
static java.lang.Byte |
NULL_BYTE
The single instance of the magic null object for a
Byte . |
static java.lang.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 java.lang.Double |
NULL_DOUBLE
The single instance of the magic null object for a
Double . |
static java.lang.Float |
NULL_FLOAT
The single instance of the magic null object for a
Float . |
static java.lang.Integer |
NULL_INTEGER
The single instance of the magic null object for a
Integer . |
static java.lang.Long |
NULL_LONG
The single instance of the magic null object for a
Long . |
static java.lang.Short |
NULL_SHORT
The single instance of the magic null object for a
Short . |
static java.lang.String |
NULL_STRING
The single instance of the magic null object for a
String . |
Constructor and Description |
---|
Data() |
Modifier and Type | Method and Description |
---|---|
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(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 using
Object.clone() . |
static boolean |
isNull(java.lang.Object object)
Returns whether the given object is the magic object that represents the null value of its
class.
|
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.
|
static boolean |
isValueOfPrimitiveType(java.lang.Object fieldValue)
Returns whether to given value is
null or its class is primitive as defined by
isPrimitive(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> 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).
|
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.
|
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, returning
Object.class if the type variable cannot be
resolved. |
public static final java.lang.Boolean NULL_BOOLEAN
Boolean
.public static final java.lang.String NULL_STRING
String
.public static final java.lang.Character NULL_CHARACTER
Character
.public static final java.lang.Byte NULL_BYTE
Byte
.public static final java.lang.Short NULL_SHORT
Short
.public static final java.lang.Integer NULL_INTEGER
Integer
.public static final java.lang.Float NULL_FLOAT
Float
.public static final java.lang.Long NULL_LONG
Long
.public static final java.lang.Double NULL_DOUBLE
Double
.public static final java.math.BigInteger NULL_BIG_INTEGER
BigInteger
.public static final java.math.BigDecimal NULL_BIG_DECIMAL
BigDecimal
.public static <T> T nullOf(java.lang.Class<?> objClass)
objClass
- class of the object needednull
)java.lang.IllegalArgumentException
- if unable to create a new instancepublic static boolean isNull(java.lang.Object object)
object
- object or null
false
for null
inputpublic static java.util.Map<java.lang.String,java.lang.Object> mapOf(java.lang.Object data)
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.
data
- any key value data, represented by an object or a map, or null
public static <T> T clone(T data)
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.
data
- key/value data object or map to clone or null
for a null
return
valuenull
for null
inputpublic static void deepCopy(java.lang.Object src, java.lang.Object dest)
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.
src
- source objectdest
- destination object of identical type as source object, and any contained arrays
must be the same lengthpublic static boolean isPrimitive(java.lang.reflect.Type type)
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
, and DateTime
.
type
- type or null
for false
resultpublic static boolean isValueOfPrimitiveType(java.lang.Object fieldValue)
null
or its class is primitive as defined by
isPrimitive(Type)
.public static java.lang.Object parsePrimitiveValue(java.lang.reflect.Type type, java.lang.String stringValue)
Types are parsed as follows:
Void
: nullnull
or is assignable from String
(like Object
): no parsingchar
or Character
: String.charAt
(0) (requires
length to be exactly 1)boolean
or Boolean
: Boolean.valueOf(String)
byte
or Byte
: Byte.valueOf(String)
short
or Short
: Short.valueOf(String)
int
or Integer
: Integer.valueOf(String)
long
or Long
: Long.valueOf(String)
float
or Float
: Float.valueOf(String)
double
or Double
: 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.
type
- primitive type or null
to parse as a stringstringValue
- string value to parse or null
for null
resultnull
for null
inputjava.lang.IllegalArgumentException
- if the given class is not a primitive classpublic static java.util.Collection<java.lang.Object> newCollectionInstance(java.lang.reflect.Type type)
Creates a new collection instance specified for the first input collection class that matches as follows:
null
or an array or assignable from ArrayList
(like List
or
Collection
or Object
): returns an ArrayList
HashSet
: returns a HashSet
TreeSet
: returns a TreeSet
Types.newInstance(Class)
type
- type or null
for ArrayList
.java.lang.ClassCastException
- if result is does not extend Collection
public static java.util.Map<java.lang.String,java.lang.Object> newMapInstance(java.lang.Class<?> mapClass)
Creates a new map instance specified for the first input map class that matches as follows:
null
or assignable from ArrayMap
(like Map
or Object
):
returns an ArrayMap
TreeMap
(like SortedMap
): returns a TreeMap
Types.newInstance(Class)
mapClass
- field classjava.lang.ClassCastException
- if result is does not extend Map
public static java.lang.reflect.Type resolveWildcardTypeOrTypeVariable(java.util.List<java.lang.reflect.Type> context, java.lang.reflect.Type type)
Object.class
if the type variable cannot be
resolved.context
- context list, ordering from least specific to most specific type context, for
example container class and then its fieldtype
- type or null
for null
resultnull
for null
inputCopyright © 2011-2018 Google. All Rights Reserved.