Package org.cyclos.utils
Class ObjectHelper
- java.lang.Object
-
- org.cyclos.utils.ObjectHelper
-
public class ObjectHelper extends Object
Helper class for objects
-
-
Constructor Summary
Constructors Constructor Description ObjectHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleananyIsNull(Object... elements)Returns true if some element is null.static booleanareAllNulls(Object... elements)Returns true if all elements are null.static <T> Tcoalesce(T... values)Returns the first non-null valuestatic <T extends Comparable<T>>
intcompare(T o1, T o2)Compares 2Comparables, handling nullsstatic <T,R>
RdefaultValue(R object, T fstArg, Function<T,R> whenNull)Lazy version ofdefaultValue(Object, Object)but accepting an argument that will be passed to the function (invoked ONLY if the first parameter is null).static <T,U,R>
RdefaultValue(R object, T fstArg, U sndArg, BiFunction<T,U,R> whenNull)Same asdefaultValue(Object, Object, Function)but accepting two argumentsstatic <T> TdefaultValue(T object, Supplier<T> whenNull)Lazy version ofdefaultValue(Object, Object)static <T> TdefaultValue(T object, T whenNull)Returns the second parameter when the first is nullstatic ObjectemptyToNull(Object value)If the given value is empty (see isEmpty(Object object)) returns null, otherwise returns the same value.static booleanequalsOrBothNull(Object first, Object second)Returns true when either both objects are non null and equals or both nullstatic inthashCode(Object object)Returns the hashCode of the given object, handling nulls (-1 if null)static <T> TifType(Object object, Class<T> expectedType)Returns the argument if it is an instance of the given class, otherwise nullstatic booleanisEmpty(Object object)Returns whether the given object is null or an empty string, collection, map or arraystatic booleanisNotEmpty(Object object)Negated value ofisEmpty(Object)static booleanisNotOneOf(Object element, Object... objects)Returns true if element is not one of the given objects.static booleanisOneOf(Object element, Object... objects)Returns true if element is one of the objects If element or objects are null it returns false;static voidrequireOneOf(Object element, Object... objects)static voidrequireOneOf(Supplier<String> errorMessageSupplier, Object element, Object... objects)Checks if element is one of the objects
-
-
-
Method Detail
-
anyIsNull
public static boolean anyIsNull(Object... elements)
Returns true if some element is null. If elements is empty or null then returns true.
-
areAllNulls
public static boolean areAllNulls(Object... elements)
Returns true if all elements are null. If elements is empty or null then returns true.
-
coalesce
@SafeVarargs public static <T> T coalesce(T... values)
Returns the first non-null value
-
compare
public static <T extends Comparable<T>> int compare(T o1, T o2)
Compares 2Comparables, handling nulls
-
defaultValue
public static <T,R> R defaultValue(R object, T fstArg, Function<T,R> whenNull)Lazy version ofdefaultValue(Object, Object)but accepting an argument that will be passed to the function (invoked ONLY if the first parameter is null). This will allow use lambda expressions an method references.
-
defaultValue
public static <T,U,R> R defaultValue(R object, T fstArg, U sndArg, BiFunction<T,U,R> whenNull)Same asdefaultValue(Object, Object, Function)but accepting two arguments- See Also:
defaultValue(Object, Object, Function)
-
defaultValue
public static <T> T defaultValue(T object, Supplier<T> whenNull)Lazy version ofdefaultValue(Object, Object)
-
defaultValue
public static <T> T defaultValue(T object, T whenNull)Returns the second parameter when the first is null
-
emptyToNull
public static Object emptyToNull(Object value)
If the given value is empty (see isEmpty(Object object)) returns null, otherwise returns the same value.
-
equalsOrBothNull
public static boolean equalsOrBothNull(Object first, Object second)
Returns true when either both objects are non null and equals or both null
-
hashCode
public static int hashCode(Object object)
Returns the hashCode of the given object, handling nulls (-1 if null)
-
ifType
public static <T> T ifType(Object object, Class<T> expectedType)
Returns the argument if it is an instance of the given class, otherwise null
-
isEmpty
public static boolean isEmpty(Object object)
Returns whether the given object is null or an empty string, collection, map or array
-
isNotEmpty
public static boolean isNotEmpty(Object object)
Negated value ofisEmpty(Object)
-
isNotOneOf
public static boolean isNotOneOf(Object element, Object... objects)
Returns true if element is not one of the given objects. If element or objects are null it returns false;
-
isOneOf
public static boolean isOneOf(Object element, Object... objects)
Returns true if element is one of the objects If element or objects are null it returns false;
-
requireOneOf
public static void requireOneOf(Object element, Object... objects) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
requireOneOf
public static void requireOneOf(Supplier<String> errorMessageSupplier, Object element, Object... objects) throws IllegalArgumentException
Checks if element is one of the objects- Parameters:
errorMessageSupplier- (Optional) Supplier to get the message in case of error- Throws:
IllegalArgumentException- if element is not one of the given objects.
-
-