public class CommonHelper extends Object
| Constructor and Description |
|---|
CommonHelper() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
denullify(Boolean bool)
Returns default value of
Boolean (i.e. |
static Date |
denullify(Date date)
Returns new
Date if the given date is null. |
static Double |
denullify(Double num)
Returns default value of
Double (i.e. |
static Integer |
denullify(Integer num)
Returns default value of
Integer (i.e. |
static <T> List<T> |
denullify(List<T> list)
Returns new
ArrayList if input is null. |
static Long |
denullify(Long num)
Returns default value of
Long (i.e. |
static <K,V> Map<K,V> |
denullify(Map<K,V> map)
Returns empty
HashMap if input is null. |
static <T> Set<T> |
denullify(Set<T> set)
Returns empty
HashSet if input is null. |
static String |
denullify(String str)
Returns empty
String if input is null. |
static <T,S extends T> |
denullify(T t,
Class<S> clazz)
Returns new instance of
clazz if the input t is null. |
static <T,S extends T> |
denullify(T t,
S sub,
S... subs)
Denullifies the given object by substituting it with the provided
sub or subs. |
static <T> void |
doIfNotNull(T t,
java.util.function.Consumer<T> consumer)
Shortens null object validation before doing an operation on that object.
Note that this does not validate null sub-objects. |
static String |
generateUUID()
Generates new random UUID using
UUID.randomUUID() |
static <T,R> R |
getIfNotNull(T t,
java.util.function.Function<T,R> func)
Shortens null object validation before doing a value-returning operation on that object.
Note that this does not validate null sub-objects. Also note that the operation cannot return void. |
static String |
toLowerCase(String str)
Shortens null validation on a String before invoking
String.toLowerCase(). |
static String |
toUpperCase(String str)
Shortens null validation on a String before invoking
String.toUpperCase(). |
static <E extends Enum<E>> |
valueOf(String str,
Class<E> clazz)
Returns Enum object of the given string
s. |
public static boolean denullify(Boolean bool)
Boolean (i.e. false) if input is null.bool - Booleanpublic static Date denullify(Date date)
Date if the given date is null.date - to be checkeddate if not null, new Date otherwisepublic static <T,S extends T> T denullify(T t,
Class<S> clazz)
throws InstantiationException,
IllegalAccessException
clazz if the input t is null.
Class.newInstance() and all exceptions thrown
are propagated.T - object typeS - object type to be instantiated if t is nullt - objectclazz - new instance of this class will be generated if input t is nullt is null, otherwise returns new instance of clazzInstantiationException - propagated exception, see Class.newInstance()IllegalAccessException - propagated exception, see Class.newInstance()Class.newInstance()public static <T,S extends T> T denullify(T t,
S sub,
S... subs)
sub or subs.T - type of objectS - type of substitutes. Must extend <T>t - objectsub - first substitutesubs - other substitutest itself if not null, otherwise returns the first non-null
substitute from either sub or subs.
May return null if all substitutes are null.public static Double denullify(Double num)
Double (i.e. 0.0) if input is null.num - Double0.0 if input is null, otherwise returns inputpublic static Integer denullify(Integer num)
Integer (i.e. 0) if input is null.num - Integerpublic static <T> List<T> denullify(List<T> list)
ArrayList if input is null.T - type of the given listlist - ListArrayList if input is null, otherwise returns inputpublic static Long denullify(Long num)
Long (i.e. 0L) if input is null.num - Long0L if input is null, otherwise returns inputpublic static <K,V> Map<K,V> denullify(Map<K,V> map)
HashMap if input is null.K - type of keyV - type of valuemap - mapHashMap if null, the map itself otherwisepublic static <T> Set<T> denullify(Set<T> set)
HashSet if input is null.T - type of the given setset - SetHashSet if input is null, otherwise returns setpublic static String denullify(String str)
String if input is null.str - Stringpublic static <T> void doIfNotNull(T t,
java.util.function.Consumer<T> consumer)
T - type of object tt - object on which the given function will be invokedconsumer - operation to be done on the objectpublic static String generateUUID()
UUID.randomUUID()public static <T,R> R getIfNotNull(T t,
java.util.function.Function<T,R> func)
doIfNotNull(Object, Consumer). t is null, regardless of the return value of
func.T - type of the object tR - type of return value of funct - objectfunc - operation to be done on the objectpublic static String toLowerCase(String str)
String.toLowerCase().str - Stringpublic static String toUpperCase(String str)
String.toUpperCase().str - Stringpublic static <E extends Enum<E>> E valueOf(String str, Class<E> clazz)
s. Uses Enum.valueOf(Class, String) to
match String and Enum objectE - type of enumstr - string to be converted to Enum objectclazz - class to which the Enum object belongsstr is blank, otherwise returns Enum object of strCopyright © 2018. All rights reserved.