public final class ObjectUtils extends Object
Some of the key features provided by this utility class include:
Note: This class is a part of the Chronicle core library and is intended to be used by developers who need to perform various common operations on objects and classes within their Java applications.
Modifier and Type | Class and Description |
---|---|
static class |
ObjectUtils.Immutability |
Modifier and Type | Method and Description |
---|---|
static <T> T[] |
addAll(T first,
T... additional)
Creates an array containing the given element followed by the elements of the additional array.
|
static @NotNull ClassCastException |
asCCE(Exception e)
Wraps the provided exception in a ClassCastException.
|
static <E> E |
convertTo(@Nullable Class<E> eClass,
@Nullable Object o)
Converts an object to the desired class if possible.
|
static void |
defaultObjectForInterface(ThrowingFunction<Class<?>,Class<?>,ClassNotFoundException> defaultObjectForInterface)
Sets a default implementation to be used for interfaces.
|
static Object |
defaultValue(Class<?> type)
Returns the default value for the given class.
|
static Class<?>[] |
getAllInterfaces(Object o)
Retrieves all the interfaces implemented by the given object or class.
|
static void |
getAllInterfaces(Object o,
Function<Class<?>,Boolean> accumulator)
Recursively accumulates all interfaces implemented by the given object or class.
|
static <E extends Enum<E>> |
getSingletonForEnum(Class<E> eClass)
Returns the single enum constant from the specified enum class, or the first one if there are multiple.
|
static void |
immutabile(Class<?> clazz,
boolean isImmutable)
Deprecated.
|
static void |
immutable(Class<?> clazz,
boolean isImmutable)
Registers the immutability status of a class.
|
static <T> Class<T> |
implementationToUse(Class<T> tClass)
Retrieves the implementation class to use for a given class.
|
static boolean |
isConcreteClass(@NotNull Class<?> tClass)
Checks if the given class is a concrete class (not abstract or interface).
|
static boolean |
isFalse(CharSequence s)
Checks if the given CharSequence is considered 'false'.
|
static ObjectUtils.Immutability |
isImmutable(@NotNull Class<?> clazz)
Checks if a class is immutable.
|
static boolean |
isTrue(CharSequence s)
Checks if the given CharSequence is considered 'true'.
|
static boolean |
matchingClass(@NotNull Class<?> base,
@NotNull Class<?> toMatch)
Checks if two classes are matching.
|
static <T> T |
newInstance(@NotNull Class<T> clazz)
Creates a new instance of the specified class.
|
static <T> T |
newInstance(@NotNull String className)
Creates a new instance of the class with the given class name.
|
static @Nullable Object |
newInstanceOrNull(Class<?> type)
Creates a new instance of the specified class, returning null if instantiation fails.
|
static <T> T |
onMethodCall(@NotNull BiFunction<Method,Object[],Object> biFunction,
@NotNull Class<T> tClass,
Class<?>... additional)
Creates a dynamic proxy instance that delegates method calls to the provided BiFunction.
|
static Class |
primToWrapper(Class<?> eClass)
If the class is a primitive type, change it to the equivalent wrapper.
|
static Object |
readResolve(@NotNull Object o)
Invokes the readResolve method on the given object if it exists.
|
static <T> T |
requireNonNull(T o)
Standard mechanism to determine objects as not null.
|
static @Nullable Boolean |
toBoolean(@Nullable String s)
Converts a string to a Boolean.
|
static <E extends Enum<E>> |
valueOfIgnoreCase(@NotNull Class<E> eClass,
@NotNull String name)
Returns the enum constant of the specified enum class with the specified name, case is ignored.
|
public static void immutable(Class<?> clazz, boolean isImmutable)
clazz
- The class whose immutability status is to be registered.isImmutable
- True if the class is immutable, false otherwise.@Deprecated public static void immutabile(Class<?> clazz, boolean isImmutable)
public static ObjectUtils.Immutability isImmutable(@NotNull @NotNull Class<?> clazz)
clazz
- The class to check.public static boolean isTrue(CharSequence s)
s
- The CharSequence to check.public static boolean isFalse(CharSequence s)
s
- The CharSequence to check.public static Class primToWrapper(Class<?> eClass)
eClass
- to check@Nullable public static <E> E convertTo(@Nullable @Nullable Class<E> eClass, @Nullable @Nullable Object o) throws ClassCastException, IllegalArgumentException
E
- The type of the target class.eClass
- The target class to convert to.o
- The object to be converted.ClassCastException
- if the object cannot be cast to the target class.IllegalArgumentException
- if an illegal argument is provided.@NotNull public static <E extends Enum<E>> E valueOfIgnoreCase(@NotNull @NotNull Class<E> eClass, @NotNull @NotNull String name)
E
- The type of the enum.eClass
- The enum class.name
- The name of the enum constant to be returned.public static <E extends Enum<E>> E getSingletonForEnum(Class<E> eClass)
E
- The type of the enum.eClass
- The enum class.@NotNull public static @NotNull ClassCastException asCCE(Exception e)
e
- The exception to be wrapped.@NotNull public static <T> T newInstance(@NotNull @NotNull String className)
T
- The type of the class to be instantiated.className
- The fully qualified name of the class to be instantiated.ClassCastException,
- if the class cannot be cast to the type T.@NotNull public static <T> T newInstance(@NotNull @NotNull Class<T> clazz)
T
- The type of the class to be instantiated.clazz
- The class to be instantiated.@Nullable public static @Nullable Object newInstanceOrNull(Class<?> type)
type
- The class to be instantiated.public static <T> T[] addAll(@NotNull T first, @NotNull T... additional)
T
- The type of the elements in the arrays.first
- The first element to be added to the array.additional
- Additional elements to be added after the first element.public static boolean matchingClass(@NotNull @NotNull Class<?> base, @NotNull @NotNull Class<?> toMatch)
base
- The base class to be compared.toMatch
- The class to be matched against the base class.public static Object defaultValue(Class<?> type)
type
- The class for which to return the default value.@NotNull public static <T> T onMethodCall(@NotNull @NotNull BiFunction<Method,Object[],Object> biFunction, @NotNull @NotNull Class<T> tClass, Class<?>... additional) throws IllegalArgumentException
T
- The type of the proxy instance.biFunction
- The BiFunction to which method calls will be delegated.tClass
- The primary interface to be implemented by the proxy instance.additional
- Additional interfaces to be implemented by the proxy instance.IllegalArgumentException
- If the arguments are invalid.public static boolean isConcreteClass(@NotNull @NotNull Class<?> tClass)
tClass
- The class to check.public static Object readResolve(@NotNull @NotNull Object o)
o
- The object on which to invoke readResolve.@Nullable public static @Nullable Boolean toBoolean(@Nullable @Nullable String s)
s
- The string to be converted.public static Class<?>[] getAllInterfaces(Object o)
o
- The object or class for which to retrieve the implemented interfaces.AssertionError
- If an illegal argument is encountered.public static void getAllInterfaces(Object o, Function<Class<?>,Boolean> accumulator) throws IllegalArgumentException
o
- The object or class for which to retrieve the implemented interfaces.accumulator
- A function that accumulates the interfaces.IllegalArgumentException
- If the accumulator is null.public static void defaultObjectForInterface(ThrowingFunction<Class<?>,Class<?>,ClassNotFoundException> defaultObjectForInterface)
defaultObjectForInterface
- A function that takes a class and returns a default implementation for it.public static <T> Class<T> implementationToUse(Class<T> tClass)
T
- The type of the class.tClass
- The class for which to retrieve the implementation.public static <T> T requireNonNull(@NotNull T o)
Objects.requireNonNull(Object)
and also decorated with NotNull
so that IntelliJ and other static analysis tools can work their magic.o
- reference to check for nullityNullPointerException
- if o is null
Copyright © 2023. All rights reserved.