Class ObjectMethods
-
Method Summary
Modifier and TypeMethodDescriptionstatic intclamp(int value, org.klojang.check.types.IntRelation relation, int limit) Clamps a value to some limit, making sure it does not exceed it.static <T> Tclamp(T value, org.klojang.check.types.Relation<T, T> relation, T limit) Clamps a value to some limit, making sure it does not exceed it.static booleane2nDeepEquals(Object arg0, Object arg1) Recursively tests the arguments for equality using empty-equals-null semantics.static booleanTests the provided arguments for equality using empty-equals-null semantics.static intGenerates a hash code for the provided arguments using empty-equals-null semantics.static inte2nHashCode(Object obj) Generates a hash code for the provided object using empty-equals-null semantics.static inte2nTypedHashCode(Object obj) Generates a hash code for the provided object using empty-equals-null semantics.static <T, C extends Collection<T>>
CemptyToNull(C arg) Returnsnullif the collection isnullor empty, else the collection itself.static ObjectemptyToNull(Object arg) Returnsnullif the argument is empty as perisEmpty(Object), else the argument itself.static StringemptyToNull(String arg) Returnsnullif the string isnullor empty, else the string itself.static <K, V, M extends Map<K,V>>
MemptyToNull(M arg) Returnsnullif the map isnullor empty, else the map itself.static <T, E extends Exception>
TifEmpty(T value, org.klojang.check.fallible.FallibleSupplier<? extends T, E> supplier) Returns the first argument if it is not empty (as perisEmpty(Object)), else the value produced by the specifiedSupplier(which may be empty as well).static <T> TifEmpty(T value, T defVal) Returns the first argument if it is not empty (as perisEmpty(Object)), else the second argument.static <T,U> U ifNotEmpty(T arg, Function<T, U> then) Returns the result of passing the specified argument to the specifiedFunctionif the argument is notempty, else returns null.static <T,U> U ifNotEmpty(T arg, Function<T, U> then, U dfault) Returns the result of passing the specified argument to the specifiedFunctionif the argument is notempty, else a default value.static <T,U> U Returns the result of passing the specified argument to the specifiedFunctionif the argument is notnull, else returnsnull.static <T,U> U Returns the result of passing the specified argument to the specifiedFunctionif the argument is not null, else a default value.static <T, E extends Exception>
TifNull(T value, org.klojang.check.fallible.FallibleSupplier<? extends T, E> supplier) Returns the first argument if it is notnull, else the value produced by the specifiedSupplier.static <T> TifNull(T value, T defVal) Returns the first argument if it is not null, else the second argument.static booleanisDeepNotEmpty(Object arg) Verifies that the argument is recursively non-empty.static booleanisDeepNotEmpty(Object[] arr) Verifies that the specified array is non-null, non-empty, and contains only deep-not-empty keys and values.static booleanisDeepNotEmpty(Collection<?> coll) Verifies that the specified collection is non-null, non-empty, and contains only deep-not-empty elements.static booleanisDeepNotEmpty(Map<?, ?> map) Verifies that the specified map is non-null, non-empty, and contains only deep-not-empty keys and values.static booleanisDeepNotNull(Object arg) Verifies that the argument is not null and, if it is array,CollectionorMap, does not contain any null values.static booleanisEmpty(CharSequence arg) Returnstrueif the specifiedCharSequenceis null or empty.static booleanReturnstrueif the specified argument is null or empty.static booleanReturnstrueif the specified array is null or empty.static booleanisEmpty(Collection<?> arg) Returnstrueif the specifiedCollectionis null or empty.static booleanReturns whether the specifiedMapis null or empty.static booleanReturnstrueif the specifiedOptionalis empty or contains an empty object.static booleanisEmpty(org.klojang.check.extra.Emptyable arg) Returnstrueif the specified array is null or empty.static <T> TnullIf(T value, T... forbiddenValues) Returnsnullif the first argument is among the forbidden values, else the first argument itself.static StringnullToEmpty(String arg) Null-to-empty: returns an emptyStringif the argument is null, else the argument itself.static <T> List<T> nullToEmpty(List<T> arg) Null-to-empty: returnsCollections.emptyList()if the argument is null, else the argument itself.static <K,V> Map <K, V> nullToEmpty(Map<K, V> arg) Null-to-empty: returnsCollections.emptyMap()if the argument is null, else the argument itself.static <T> Set<T> nullToEmpty(Set<T> arg) Null-to-empty: returnsCollections.emptySet()if the argument is null, else the argument itself.static BooleannullToFalse(Boolean arg) ReturnsBoolean.FALSEif the argument is null, else the argument itself.static BytenullToZero(Byte arg) Returns zero if the argument is null, else the argument itself.static CharacternullToZero(Character arg) Returns zero if the argument is null, else the argument itself.static DoublenullToZero(Double arg) Returns zero if the argument is null, else the argument itself.static FloatnullToZero(Float arg) Returns zero if the argument is null, else the argument itself.static IntegernullToZero(Integer arg) Returns zero if the argument is null, else the argument itself.static LongnullToZero(Long arg) Returns zero if the argument is null, else the argument itself.static ShortnullToZero(Short arg) Returns zero if the argument is null, else the argument itself.static <T> TnullUnless(T value, T... allowedValues) Returns this first argument if it is among the allowed values, elsenull.static StringReturns an empty string if the argument isnull, else the result of callingtoString()on the argument.static intwhen(int value, IntPredicate criterion, int then) Replaces a value with another value if it satisfies a certain criterion.static intwhen(int value, org.klojang.check.types.IntRelation relation, int compareTo, int then) Replaces a value with another value if it satisfies a certain criterion.static <T> TReplaces a value with a default value if it satisfies the specified criterion.static <T> Twhen(T value, org.klojang.check.types.Relation<T, T> relation, T compareTo, T then) Replaces a value with another value if it satisfies a certain criterion.
-
Method Details
-
stringify
Returns an empty string if the argument isnull, else the result of callingtoString()on the argument. Equivalent toObjects.toString(obj, ""), but more usable as method reference.- Parameters:
obj- the object to stringify- Returns:
- an empty string if the argument is
null, else the result of callingtoString()on the argument
-
isEmpty
Returnstrueif the specifiedCharSequenceis null or empty.- Parameters:
arg- theCharSequenceto check- Returns:
- whether it is null or empty
-
isEmpty
Returnstrueif the specified array is null or empty.- Parameters:
arg- the array to check- Returns:
- whether it is null or empty
-
isEmpty
Returnstrueif the specifiedCollectionis null or empty.- Parameters:
arg- theCollectionto check- Returns:
- whether it is null or empty
-
isEmpty
Returns whether the specifiedMapis null or empty.- Parameters:
arg- theMapto check- Returns:
- whether it is null or empty
-
isEmpty
Returnstrueif the specifiedOptionalis empty or contains an empty object. This is the onlyisEmptymethod that will throw anIllegalArgumentExceptionif the argument is null asOptionalobjects should never be null.- Parameters:
arg- theOptionalto check- Returns:
- whether it is empty or contains an empty object
-
isEmpty
public static boolean isEmpty(org.klojang.check.extra.Emptyable arg) Returnstrueif the specified array is null or empty.- Parameters:
arg- the array to check- Returns:
- whether it is null or empty
-
isEmpty
Returnstrueif the specified argument is null or empty. More precisely, this method returnstrueif any of the following applies:argisnullargis an emptyCharSequenceargis an emptyCollectionargis an emptyMapargis an emptyEmptyableargis a zero-length arrayargis an emptyOptionalor anOptionalcontaining an empty value
In any other case this method returns
false.- Parameters:
arg- the argument to check- Returns:
- whether it is null or empty
-
isDeepNotEmpty
Verifies that the argument is recursively non-empty. More precisely, this method returnstrueif the argument is any of the following:- a non-empty
CharSequence - a non-empty
Collectioncontaining only deep-not-empty elements - a non-empty
Mapcontaining only deep-not-empty keys and values - a deep-not-empty
Emptyable - a non-zero-length
Object[]containing only deep-not-empty elements - a non-zero-length array of primitives
- a non-empty
Optionalcontaining a deep-not-empty value - a non-null object of any other type
- Parameters:
arg- The object to be tested- Returns:
- whether it is recursively non-empty
- See Also:
- a non-empty
-
isDeepNotEmpty
Verifies that the specified collection is non-null, non-empty, and contains only deep-not-empty elements.- Parameters:
coll- the collection to test- Returns:
- whether it is recursively non-empty
-
isDeepNotEmpty
Verifies that the specified map is non-null, non-empty, and contains only deep-not-empty keys and values.- Parameters:
map- the map to test- Returns:
- whether it is recursively non-empty
-
isDeepNotEmpty
Verifies that the specified array is non-null, non-empty, and contains only deep-not-empty keys and values.- Parameters:
arr- the array to test- Returns:
- whether it is recursively non-empty
-
isDeepNotNull
Verifies that the argument is not null and, if it is array,CollectionorMap, does not contain any null values. It may still be an empty array,CollectionorMap, though. For maps, both keys and values are tested fornull.- Parameters:
arg- the object to be tested- Returns:
- whether it is not null and does not contain any null values
-
e2nEquals
Tests the provided arguments for equality using empty-equals-null semantics. This is roughly equivalent toObjects.equals(e2n(arg0), e2n(arg1)), except thate2nEqualsdoes take the types of the two arguments into account. So an emptyStringis not equal to an emptyArrayListand an emptyArrayListis not equal to an emptyHashSet. (An emptyHashSetis equal to an emptyTreeSet, but that is just behaviour specified by the Collections Framework.)nullequals an emptyCharSequencenullequals an emptyCollectionnullequals an emptyMapnullequals an empty arraynullequals an emptyOptionalor anOptionalcontaining an empty object- A empty instance of one type is not equal to a empty instance of another non-comparable type
- Parameters:
arg0- The 1st of the pair of objects to comparearg1- The 2nd of the pair of objects to compare- Returns:
- whether the provided arguments are equal using empty-equals-null semantics
-
e2nDeepEquals
Recursively tests the arguments for equality using empty-equals-null semantics.- Parameters:
arg0- The 1st of the pair of objects to comparearg1- The 2nd of the pair of objects to compare- Returns:
- whether the provided arguments are deeply equal using empty-equals-null semantics
-
e2nHashCode
Generates a hash code for the provided object using empty-equals-null semantics. Empty objects (whatever their type and includingnull) all have the same hash code: 0 (zero)! If the argument is any array, this method is recursively applied to the array's elements. Thereforee2nHashCodein effect generates a "deep" hash code.- Parameters:
obj- The object to generate a hash code for- Returns:
- the hash code
-
e2nTypedHashCode
Generates a hash code for the provided object using empty-equals-null semantics. This variant of
hashCode()includes the type of the argument in the computation of the hash code. As withe2nEquals(Object, Object), this ensures that an emptyStringwill not have the same hash code as an emptyArrayList, and an emptyArrayListwill not have the same hash code as an emptyHashSet. An emptyHashSetwill have the same hash code as an emptyTreeSet, because for Collection Framework classes it is the hash code of the base type that is included in the computation of the hash code (i.e.List.class,Set.classandMap.class).Thus, using
e2nTypedHashCodewill lead to fewer hash collisions thane2nHashcode. However, it may, in some circumstances break the contract forObject.hashCode(). A class could define anequalsmethod that allows instances of it to be equal to instances of its superclass. However,e2nTypedHashCodeprecludes this as the hash code for the class and the superclass will be different.Also note that this anyhow only becomes relevant if the collection of objects you work with is very heterogeneous: strings, empty strings, sets, empty sets, empty lists, empty arrays (etc.) all mixed together.
- Parameters:
obj- The object to generate a hash code for- Returns:
- the hash code
-
e2nHash
Generates a hash code for the provided arguments using empty-equals-null semantics. SeeObject.hashCode().- Parameters:
objs- The objects to generate a hash code for- Returns:
- the hash code
-
ifNull
public static <T> T ifNull(T value, T defVal) Returns the first argument if it is not null, else the second argument.- Parameters:
value- The value to return if not nulldefVal- The value to return if the first argument isnull- Returns:
- A non-null value
-
ifNull
public static <T, E extends Exception> T ifNull(T value, org.klojang.check.fallible.FallibleSupplier<? extends T, E> supplier) throws E Returns the first argument if it is notnull, else the value produced by the specifiedSupplier.- Type Parameters:
T- the type of the arguments and the return valueE- The type of the exception that can potentially be thrown by theSupplier- Parameters:
value- The value to return if it is notnullsupplier- The supplier of a default value- Returns:
- a non-null value
- Throws:
E
-
ifEmpty
public static <T> T ifEmpty(T value, T defVal) Returns the first argument if it is not empty (as perisEmpty(Object)), else the second argument.- Type Parameters:
T- the type of the arguments and the return value- Parameters:
value- The value to return if it is not emptydefVal- The value to return if the first argument is empty- Returns:
- a non-empty value
-
ifEmpty
public static <T, E extends Exception> T ifEmpty(T value, org.klojang.check.fallible.FallibleSupplier<? extends T, E> supplier) throws E Returns the first argument if it is not empty (as perisEmpty(Object)), else the value produced by the specifiedSupplier(which may be empty as well).- Type Parameters:
T- the input and return typeE- The exception potentially being thrown by the supplier as it- Parameters:
value- The value to return if not emptysupplier- The supplier of a default value ifvalueis null- Returns:
- a non-empty value
- Throws:
E
-
nullUnless
public static <T> T nullUnless(T value, T... allowedValues) Returns this first argument if it is among the allowed values, elsenull.- Type Parameters:
T- the type of the involved values- Parameters:
value- The value to testallowedValues- The values it is allowed to have- Returns:
- the first argument or
null
-
nullIf
public static <T> T nullIf(T value, T... forbiddenValues) Returnsnullif the first argument is among the forbidden values, else the first argument itself.- Type Parameters:
T- the type of the involved values- Parameters:
value- The value to testforbiddenValues- The values it must not have- Returns:
- the first argument or
null
-
ifNotNull
Returns the result of passing the specified argument to the specifiedFunctionif the argument is notnull, else returnsnull. For example:String[] strs = ifNotNull("Hello World", s -> s.split(" "));- Type Parameters:
T- the type of the first argumentU- The return type- Parameters:
arg- the value to testthen- The transformation to apply to the value if it is not null- Returns:
valueor null
-
ifNotNull
Returns the result of passing the specified argument to the specifiedFunctionif the argument is not null, else a default value. For example:String[] strs = ifNotNull("Hello World", s -> s.split(" "), new String[0]);- Type Parameters:
T- the type of the first value to transformU- The return type- Parameters:
arg- the value to transformthen- The transformation to apply to the value if it is not nulldfault- A default value to return if the argument is null- Returns:
- the result produced by the
Functionor by theSupplier
-
ifNotEmpty
Returns the result of passing the specified argument to the specifiedFunctionif the argument is notempty, else returns null.- Type Parameters:
T- the type of the value to transformU- The return type- Parameters:
arg- the value to transformthen- The function to apply to the value if it is not null- Returns:
- the result produced by the
Functionor a default value
-
ifNotEmpty
Returns the result of passing the specified argument to the specifiedFunctionif the argument is notempty, else a default value.- Type Parameters:
T- the type of the value to transformU- The return type- Parameters:
arg- the value to transformthen- The function to apply to the value if it is not nulldfault- A default value to return if the argument is empty- Returns:
- the result produced by the
Functionor a default value
-
clamp
public static <T> T clamp(T value, org.klojang.check.types.Relation<T, T> relation, T limit) Clamps a value to some limit, making sure it does not exceed it.import static nl.naturalis.check.ObjectMethods.clamp; import static nl.naturalis.check.CommonChecks.LTE; // Prevent dates from lying in the future: LocalDate dateVar = clamp(dateArg, LTE(), LocalDate.now());- Type Parameters:
T- the type of the values- Parameters:
value- the value to testrelation- the relation that the value needs to have with respect to the limitlimit- the maximum or minimum value- Returns:
- the value determined by the relation by the two values
-
clamp
public static int clamp(int value, org.klojang.check.types.IntRelation relation, int limit) Clamps a value to some limit, making sure it does not exceed it.import static nl.naturalis.check.ObjectMethods.clamp; import static nl.naturalis.check.CommonChecks.lte; // Max out at 65 MPH. int speed = clamp(someSpeed, lte(), 65);- Parameters:
value- the value to testrelation- the relation that the value needs to have with respect to the limitlimit- the maximum or minimum value- Returns:
- the value if it does not exceed the limit, else the limit
-
when
Replaces a value with a default value if it satisfies the specified criterion.import static nl.naturalis.check.ObjectMethods.when; import static nl.naturalis.check.CommonChecks.isNull; // Replace null values with blue Color newColor = when(color, isNull(), Color.BLUE);- Type Parameters:
T- the type of the values- Parameters:
value- the value to testcriterion- the criterionthen- the replacement value- Returns:
- the default value if the value satisfies the criterion, else the value itself
-
when
Replaces a value with another value if it satisfies a certain criterion. Note that theCommonChecksclass defines various predicates that might be of use.- Parameters:
value- the value to testcriterion- the criterionthen- the replacement value- Returns:
- the default value if the value satisfies the criterion, else the value itself
-
when
public static <T> T when(T value, org.klojang.check.types.Relation<T, T> relation, T compareTo, T then) Replaces a value with another value if it satisfies a certain criterion. Note that theCommonChecksclass defines various relations that might be of use. For example:import static nl.naturalis.check.ObjectMethods.when; import static nl.naturalis.check.CommonChecks.equalTo; // Replace green with blue, otherwise keep color Color newColor = when(color, equalTo(), Color.GREEN, Color.BLUE);- Type Parameters:
T- the type of the values- Parameters:
value- the value to testrelation- the relation betweenvalueandcompareTocompareTo- the compare-to valuethen- the value returned if the relation betweenvalueandcompareToexists- Returns:
thenif the relation betweenvalueandcompareToexists, elsevalueitself.
-
when
public static int when(int value, org.klojang.check.types.IntRelation relation, int compareTo, int then) Replaces a value with another value if it satisfies a certain criterion. Note that theCommonChecksclass defines various relations that might be of use. For example:- Parameters:
value- the value to testrelation- the relation betweenvalueandcompareTocompareTo- the compare-to valuethen- the value returned if the relation betweenvalueandcompareToexists- Returns:
thenif the relation betweenvalueandcompareToexists, elsevalueitself.
-
emptyToNull
Returnsnullif the argument is empty as perisEmpty(Object), else the argument itself.- Parameters:
arg- the argument- Returns:
- the argument itself if not empty, else
null
-
emptyToNull
-
emptyToNull
Returnsnullif the collection isnullor empty, else the collection itself.- Parameters:
arg- the collection- Returns:
- the collection itself if not
nullor empty, elsenull
-
emptyToNull
Returnsnullif the map isnullor empty, else the map itself.- Parameters:
arg- the map- Returns:
- the map itself if not
nullor empty, elsenull
-
nullToEmpty
-
nullToEmpty
Null-to-empty: returnsCollections.emptyList()if the argument is null, else the argument itself.- Parameters:
arg- an argument of typeList- Returns:
- the argument or the default value of the corresponding primitive type
-
nullToEmpty
Null-to-empty: returnsCollections.emptySet()if the argument is null, else the argument itself.- Parameters:
arg- an argument of typeList- Returns:
- the argument or the default value of the corresponding primitive type
-
nullToEmpty
Null-to-empty: returnsCollections.emptyMap()if the argument is null, else the argument itself.- Parameters:
arg- an argument of typeList- Returns:
- the argument or the default value of the corresponding primitive type
-
nullToZero
-
nullToZero
-
nullToZero
-
nullToZero
-
nullToZero
-
nullToZero
-
nullToZero
-
nullToFalse
ReturnsBoolean.FALSEif the argument is null, else the argument itself.- Parameters:
arg- an argument of typeByte- Returns:
- the argument or the default value of the corresponding primitive type
-