public class Types extends Object
JavaScript
paradigm. Although uncommon for utility classes this one allows for sub-classing, see sample
code.
class Types extends js.util.Types { public static boolean isWunderObject(Object o) { return o instanceof WunderObject; } } ... if(Types.isInstanceOf(object, StandardObject.class)) { // predicate provided by base class ... } if(Types.isWunderObject(object)) { // predicate provided by extension ... }
Modifier and Type | Field and Description |
---|---|
private static Map<Type,Type> |
BOXING_MAP
Java language primitive values boxing classes.
|
private static Pattern |
CLASS_NAME_PATTERN
Pattern constant used for Java class name validation.
|
private static Type[] |
NUMERICAL_TYPES
Java standard classes used to represent numbers, including primitives.
|
Modifier | Constructor and Description |
---|---|
protected |
Types()
Prevent default constructor synthesis but allow sub-classing.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
asBoolean(Object o)
Returns a boolean representation of a given object instance.
|
static Iterable<?> |
asIterable(Object o)
Convert object instance to iterable.
|
static boolean |
equalsAny(Type t,
Type... typesToMatch)
Test if a requested type is identity equal with one from a given types list.
|
static Class<?> |
getBoxingClass(Type t)
Get boxing class for requested type.
|
static Object |
getEmptyValue(Type t)
Returns an empty value of requested type.
|
static boolean |
isArray(Object o)
Test if instance is an array.
|
static boolean |
isArray(Type t)
Test if type is array.
|
static boolean |
isArrayLike(Object o)
Test instance if is array like.
|
static boolean |
isArrayLike(Type t)
Test if type is array like, that is, array or collection.
|
static boolean |
isBoolean(Object o)
Test if object instance is a boolean, primitive or boxing class.
|
static boolean |
isBoolean(Type t)
Test if type is a boolean primitive or boxing class.
|
static boolean |
isCharacter(Object o)
Test if instance is a character, primitive or boxing class.
|
static boolean |
isCharacter(Type t)
Test if type is a character, primitive or boxing.
|
static boolean |
isClass(String name)
Test if given name is a valid Java class name.
|
static boolean |
isCollection(Object o)
Test if instance is a collection.
|
static boolean |
isCollection(Type t)
Test if type is collection.
|
static boolean |
isConcrete(Type t)
Test if type is concrete, that is, is not interface or abstract.
|
static boolean |
isDate(Object o)
Test if object is instance of date.
|
static boolean |
isDate(Type t)
Test if type is a calendar date.
|
static boolean |
isEmpty(Object o)
Returns true if given object is empty.
|
static boolean |
isEnum(Object o)
Test if object instance is enumeration.
|
static boolean |
isEnum(Type t)
Test if type is enumeration.
|
static boolean |
isEqual(Type type1,
Type type2)
Test two types for equality.
|
static boolean |
isInstanceOf(Object o,
Type t)
Test if object instance is not null and extends or implements expected type.
|
static boolean |
isKindOf(Type t,
Type... typesToMatch)
Determine if a given type is a kind of one of the requested types to match.
|
private static boolean |
isKindOf(Type t,
Type typeToMatch)
Determine if a given type is a kind of a requested type to match.
|
static boolean |
isMap(Object o)
Test if instance is a map.
|
static boolean |
isMap(Type t)
Test if type is map.
|
static boolean |
isNumber(Object o)
Test if object instance is primitive numeric value or related boxing class.
|
static boolean |
isNumber(Type t)
Test if type is numeric.
|
static boolean |
isPrimitive(Object o)
Test if object instance is primitive.
|
static boolean |
isPrimitive(Type t)
Test if type is primitive.
|
static boolean |
isPrimitiveLike(Object o)
Test if object instance is a primitive like.
|
static boolean |
isPrimitiveLike(Type t)
Test if type is like a primitive? Return true only if given type is a number, boolean, enumeration, character or
string.
|
static boolean |
isVoid(Type t)
Test if given type is void.
|
private static Class<?> |
typeToClass(Type t)
Cast Java reflective type to language class.
|
private static Type[] NUMERICAL_TYPES
private static final Pattern CLASS_NAME_PATTERN
protected Types()
public static boolean equalsAny(Type t, Type... typesToMatch) throws IllegalArgumentException
type
is null return
false. If a type to match happened to be null is considered no match.t
- type to search for, possible null,typesToMatch
- types list to compare with.IllegalArgumentException
- if typesToMach
is empty.public static boolean isKindOf(Type t, Type... typesToMatch) throws IllegalArgumentException
typesToMatch
and
delegates isKindOf(Type, Type)
till first positive match and returns true. If no match found returns
false. If type
is null returns false. If a type to match happened to be null is considered no match.t
- type to test, possible null,typesToMatch
- variable number of types to match.type
is a kind of one of typesToMatch
.IllegalArgumentException
- if typesToMach
is null or empty.private static boolean isKindOf(Type t, Type typeToMatch)
type
is a subclass
or implements typeToMatch
- not necessarily direct. Boxing classes for primitive values are
compatible. This depart from Class.isAssignableFrom(Class)
that consider primitive and related boxing class
as different.
If either type or type to match are parameterized types uses the raw class. If either type or type to match are null returns false.
t
- type to test,typeToMatch
- desired type to match.type
is subclass of or implements typeToMatch
.public static Class<?> getBoxingClass(Type t) throws BugError
type
is primitive returns related boxing class. If
type
is already a boxing type returns it as it is. It is considered a bug if type
is not
a primitive or a boxing type.t
- primitive or boxing type.BugError
- if type
is not a primitive or boxing type.public static boolean isInstanceOf(Object o, Type t)
1.23
is instance of Double
.o
- object instance to test, possible null,t
- expected type.public static boolean isConcrete(Type t)
t
- type to test, possible null.public static boolean isNumber(Object o)
o
- object instance, possible null.public static boolean isNumber(Type t)
t
- type to test.type
is numeric.public static boolean isCharacter(Object o)
o
- instance to test.public static boolean isCharacter(Type t)
t
- type to test.public static boolean isBoolean(Object o)
Boolean
instance or false otherwise. Return also false if object is null.o
- object instance, possible null.public static boolean isBoolean(Type t)
t
- type to test.public static boolean isEnum(Object o)
o
- object instance to test, possible null.public static boolean isEnum(Type t)
Class.isEnum()
if type is a class. If not, returns
false.t
- type to test.public static boolean isPrimitive(Object o)
o
- object instance to test.public static boolean isPrimitive(Type t)
Class.isPrimitive()
.t
- type to test.public static boolean isPrimitiveLike(Object o)
isPrimitiveLike(Type)
. If instance
to test is null returns false.o
- object instance, possible null.public static boolean isPrimitiveLike(Type t)
t
- type to test.public static boolean isArray(Object o)
isArray(Type)
and returns false if object instance is null.o
- instance to test.public static boolean isArray(Type t)
Class.isArray()
predicate value; otherwise test if type is
GenericArrayType
.t
- type to test.public static boolean isArrayLike(Object o)
isArrayLike(Type)
; otherwise
return false.o
- instance to test, possible null in which case returns false.public static boolean isArrayLike(Type t)
isArray(Type)
and
isCollection(Type)
.t
- type to test.public static boolean isCollection(Object o)
isCollection(Type)
; otherwise
return false.o
- instance to test, possible null in which case returns false.public static boolean isCollection(Type t)
Collection
interface.t
- type to test.public static boolean isMap(Object o)
isMap(Type)
; otherwise returns false.o
- instance to test, possible null.public static boolean isMap(Type t)
Map
interface.t
- type to test.public static boolean isDate(Object o)
object
is instance of Date
; returns
false if instance to test is null.o
- object instance, possible null.public static boolean isDate(Type t)
t
- type to test.public static boolean isClass(String name)
name
- name to test, possible null.public static boolean asBoolean(Object o)
JavaScript
cast of object to boolean value. Here are tested conditions:
isBoolean(Object)
returns boolean value,
isNumber(Object)
returns true if number is not zero,
isArray(Object)
, isCollection(Object)
or isMap(Object)
returns true if is not
empty,
isCharacter(Object)
returns true if character is defined.
If object instance is null returns false.
o
- object instance to interpret as boolean.public static Object getEmptyValue(Type t)
0
, false
, empty string,
current date/time, empty collection or empty array if requested type is respectively a number, boolean, string,
date, collection or array. If none of previous returns null.t
- desired type for empty value.private static Class<?> typeToClass(Type t)
type
is instance of Class
just return it. If
is parameterized type returns the raw class.t
- Java reflective type.type
.public static Iterable<?> asIterable(Object o)
This utility method is designed to be used with foreach
loop. Note that if object instance is not
iterable foreach
loop is not executed.
Object o = getObjectFromSomeSource(); for(Object item : Types.asIterable(o)) { // do something with item instance }
o
- object instance.public static boolean isVoid(Type t)
Void.TYPE
or Void
class, i.e. is
void
keyword or Void
class.t
- type to test for void.public static boolean isEmpty(Object o)
o
- object to test if empty.public static boolean isEqual(Type type1, Type type2)
If both types are null returns true.
type1
- first type, null accepted,type2
- second type, null accepted.Copyright © 2018. All rights reserved.