Class Util
java.lang.Object
dev.derklaro.reflexion.internal.util.Util
A collection of general purpose utility methods used within the library.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanallMatch(T[] leftArr, T[] rightArr, @NotNull BiPredicate<T, T> tester) Checks if all elements in both arrays match taking care of some edge cases to reduce computation time.static voidensureTrue(boolean condition, @NotNull String message) Ensures that the given condition is true, throwing an exception with the given message otherwise.static <T,O> @NotNull Collection <O> filterAndMap(@NotNull Collection<T> in, @NotNull Predicate<T> tester, @NotNull Function<T, O> mapper) Filters all elements from the given collection and applies the matching ones to the given mapper and returns a collection containing all filtered and mapped elements.static <T> @UnknownNullability TfirstNonNull(T... choices) Tries to find the first non-null value from the given array, returning null if no such value was found.static @Nullable ObjectgetBinding(@NotNull Reflexion reflexionInstance, @Nullable Object requestedBinding, int modifiers) Gets the binding instance to execute the requested action on a field or method.static booleanisClassPresent(@NotNull String name) Checks if a class with the given name is present in the current runtime.
-
Constructor Details
-
Util
private Util()
-
-
Method Details
-
firstNonNull
Tries to find the first non-null value from the given array, returning null if no such value was found.- Type Parameters:
T- the type of elements in the given array.- Parameters:
choices- the array to find the first non-null element in.- Returns:
- the first non-null element in the given array, null if no such element was found.
- Throws:
NullPointerException- if the given array is null.
-
allMatch
public static <T> boolean allMatch(@Nullable T[] leftArr, @Nullable T[] rightArr, @NotNull @NotNull BiPredicate<T, T> tester) Checks if all elements in both arrays match taking care of some edge cases to reduce computation time.- Type Parameters:
T- the type of the elements in both arrays.- Parameters:
leftArr- the left array to check.rightArr- the right array to check.tester- the tester for the elements in the arrays.- Returns:
- true if all elements in both arrays match the given matcher, false otherwise.
- Throws:
NullPointerException- if the given left/right array or element tester is null.
-
filterAndMap
@NotNull public static <T,O> @NotNull Collection<O> filterAndMap(@NotNull @NotNull Collection<T> in, @NotNull @NotNull Predicate<T> tester, @NotNull @NotNull Function<T, O> mapper) Filters all elements from the given collection and applies the matching ones to the given mapper and returns a collection containing all filtered and mapped elements.- Type Parameters:
T- the type of elements before the mapping.O- the type of elements after the mapping.- Parameters:
in- the collection to filter and map the elements of.tester- the tester to filter the elements from the given input collection.mapper- the mapper to map the elements from the input collection which passed the given filter.- Returns:
- a collection containing all filtered and mapped elements from the given input collection.
- Throws:
NullPointerException- if the given input collection, tester or mapper is null.
-
isClassPresent
Checks if a class with the given name is present in the current runtime. Calling this method will not initialize the class.- Parameters:
name- the name of the class to check for presence.- Returns:
- true if a class with the given name is present, false otherwise.
- Throws:
NullPointerException- if the given name is null.
-
getBinding
@Nullable public static @Nullable Object getBinding(@NotNull @NotNull Reflexion reflexionInstance, @Nullable @Nullable Object requestedBinding, int modifiers) Gets the binding instance to execute the requested action on a field or method. If the member is static this method always returnsnullas no binding is required for the operation to succeed.The given requested binding will be preferred over the binding of the reflexion instance.
- Parameters:
reflexionInstance- the reflexion instance which created the accessor.requestedBinding- the binding instance requested by the user,nullif not given.modifiers- the modifiers of the member the operation is made on.- Returns:
- the binding instance to use for executing the operation on the member.
- Throws:
NullPointerException- if the given reflexion instance is null.IllegalArgumentException- if a binding is required but not given.
-
ensureTrue
Ensures that the given condition is true, throwing an exception with the given message otherwise.- Parameters:
condition- the condition to check.message- the message to throw the exception with.- Throws:
NullPointerException- if the given message is null.IllegalStateException- if the given condition is false.- Since:
- 1.6
-