Class ObjectUtils
java.lang.Object
io.fluxzero.common.ObjectUtils
Utility class for common object handling, memoization, concurrency, stream processing, and error handling.
Offers reusable tools for:
- Memoizing functional interfaces
- Retry-safe functional wrappers (callables, consumers, etc.)
- Stream deduplication and flattening
- Property file manipulation
- Exception-safe task execution and error unwrapping
- Custom thread factories and naming
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Callable<?> asCallable(ThrowingRunnable runnable) Converts a ThrowingRunnable into aCallable<Object>that returns null.static Callable<?> asCallable(Runnable runnable) Converts a standardRunnableinto aCallable<Object>that returns null.static <T> Consumer<T> asConsumer(ThrowingConsumer<T> consumer) static <T,R> Function <T, R> asFunction(ThrowingFunction<T, R> function) static PropertiesasProperties(String content) Parses JavaPropertiesfrom a string.static RunnableasRunnable(ThrowingRunnable runnable) static RunnableasRunnable(Callable<?> callable) static Stream<?> Converts an object into a stream.static <T> Supplier<T> asSupplier(Callable<T> callable) static <T> TCalls the given callable, forcibly rethrowing exceptions.static Stringcapitalize(String value) Capitalizes the first character of a string, leavingnulland empty strings unchanged.static <T> Stream<T> Concatenates multiple streams into a single flat stream.static PropertiescopyOf(Properties properties) Creates a deep copy of a Properties instance.static ExecutorReturns the classloader-wide executor for bounded chunks of CPU work, isolated from the JVM common pool.static intReturns the target CPU parallelism, including the work formerly performed by a parallel stream's caller.static <T> List<T> deduplicate(List<T> list) Deduplicates elements in the list, preserving the last occurrence.static <T> List<T> deduplicate(List<T> list, Function<T, ?> idFunction) Deduplicates elements using a key extractor, preserving the last occurrence.static <T> List<T> deduplicate(List<T> list, Function<T, ?> idFunction, boolean keepFirst) Deduplicates elements using a key extractor, optionally keeping the first occurrence.static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) Returns a predicate that filters out duplicates based on a key extractor.static ObjectforceThrow(Throwable error) Forces the given throwable to be thrown.static byte[]getBytes(ByteBuffer buffer) Extracts the byte array from aByteBuffer.ifTrue(boolean check) Returns a consumer that runs the task only ifcheckis true.static <T> TinParallel(Supplier<T> operation) Evaluates a parallel stream terminal operation on the isolated CPU pool and awaits its result.static booleanisBlank(CharSequence value) Returns whether a character sequence isnull, empty, or contains only whitespace.static booleanisNumeric(CharSequence value) Returns whether a character sequence is non-empty and consists entirely of Unicode digits.static <T> Stream<T> iterate(T seed, UnaryOperator<T> f, Predicate<T> breakCondition) Creates a stream that stops once the break condition evaluates true.static byte[]join(byte[]... chunks) Combines multiple byte arrays into a single byte array by concatenating their contents in the given order.static <T> List<T> limitByCumulativeWeight(List<T> list, long maxWeight, ToLongFunction<T> weightFunction) Returns a prefix of the list whose cumulative weight stays withinmaxWeight.static <T,U, R> MemoizingBiFunction <T, U, R> memoize(BiFunction<T, U, R> supplier) static <K,V> MemoizingFunction <K, V> static <T> MemoizingSupplier<T> static Propertiesmerge(Properties a, Properties b) Merges two Properties instances into a new one.static ThreadFactorynewPlatformThreadFactory(String prefix) Creates a newThreadFactorywith a named prefix.static ThreadFactorynewVirtualThreadFactory(String prefix) Creates a new named virtual-threadThreadFactory.static ExecutornewWorkerExecutor(String prefix) Creates a named virtual-thread-per-task executor without a queue or persistent worker resources.static ExecutorServicenewWorkerPool(String prefix, int poolSize) Creates an executor with one named virtual thread per task.static ThreadFactorynewWorkerThreadFactory(String prefix) Creates a named virtual-thread factory for workers.static <T,U> BiPredicate <T, U> Returns a bi-predicate that always evaluates to true.static <T> Predicate<T> Returns a predicate that always evaluates to true.static RuntimeExceptionUnwraps then returns the givenThrowableas an unchecked exception.static voidrun(ThrowingRunnable runnable) Executes the runnable, forcibly rethrowing exceptions as unchecked.static voidrunInParallel(Runnable operation) Runs a parallel terminal operation with the same isolation and completion contract asinParallel(Supplier).static <T> Predicate<T> silentTest(Predicate<T> predicate) Returns a predicate that silently returns false when the given predicate throws an error.static StringstackTrace(Throwable throwable) Renders a throwable and its causes to the same text produced byThrowable.printStackTrace().static StringstripAccents(String value) Removes accents from a string while retaining otherwise compatible Unicode characters.static booleanDeprecated.SDK v2 requires Java 25 or newer, so callers no longer need a runtime-version check.static <T> Consumer<? super T> Returns a consumer that logs errors instead of propagating them.static RunnableWraps a runnable in a try/catch block that logs any exception on execution.static voidExecutes the runnable and logs any thrown exception.static ThrowableRecursively unwraps the cause of common wrapping exceptions.
-
Constructor Details
-
ObjectUtils
public ObjectUtils()
-
-
Method Details
-
isBlank
Returns whether a character sequence isnull, empty, or contains only whitespace. -
isNumeric
Returns whether a character sequence is non-empty and consists entirely of Unicode digits. -
capitalize
-
stripAccents
-
stackTrace
Renders a throwable and its causes to the same text produced byThrowable.printStackTrace(). -
noOpPredicate
Returns a predicate that always evaluates to true. -
noOpBiPredicate
Returns a bi-predicate that always evaluates to true. -
distinctByKey
-
iterate
Creates a stream that stops once the break condition evaluates true. -
concat
Concatenates multiple streams into a single flat stream. -
deduplicate
-
deduplicate
-
deduplicate
-
limitByCumulativeWeight
public static <T> List<T> limitByCumulativeWeight(List<T> list, long maxWeight, ToLongFunction<T> weightFunction) Returns a prefix of the list whose cumulative weight stays withinmaxWeight.If the first item already exceeds
maxWeight, it is still included so callers can make progress. AmaxWeightof0or lower leaves the list unchanged. -
asStream
-
ifTrue
-
forceThrow
-
call
Calls the given callable, forcibly rethrowing exceptions. -
run
Executes the runnable, forcibly rethrowing exceptions as unchecked. -
asCallable
Converts a ThrowingRunnable into aCallable<Object>that returns null. -
asCallable
-
tryRun
Executes the runnable and logs any thrown exception. -
tryCatch
-
asSupplier
-
asRunnable
-
asFunction
-
asConsumer
-
asRunnable
-
getBytes
Extracts the byte array from aByteBuffer. -
unwrapException
-
rethrow
Unwraps then returns the givenThrowableas an unchecked exception. If the provided throwable is aRuntimeExceptionorError, it is returned as-is. Otherwise, it is wrapped in a newRuntimeException.- See Also:
-
asProperties
Parses JavaPropertiesfrom a string. -
copyOf
Creates a deep copy of a Properties instance. -
merge
Merges two Properties instances into a new one. -
memoize
-
memoize
-
memoize
-
newPlatformThreadFactory
Creates a newThreadFactorywith a named prefix. -
newVirtualThreadFactory
Creates a new named virtual-threadThreadFactory. -
newWorkerThreadFactory
Creates a named virtual-thread factory for workers. -
newWorkerPool
Creates an executor with one named virtual thread per task. The caller owns its shutdown. Concurrency and admission limits must be enforced by the submitting component, not by this executor.- Parameters:
prefix- the worker thread name prefixpoolSize- a positive legacy sizing hint; does not bound virtual-thread concurrency
-
newWorkerExecutor
Creates a named virtual-thread-per-task executor without a queue or persistent worker resources. Inheritable thread locals are not copied; callers must propagate their intended context explicitly. The submitting component owns admission, task completion and cancellation. There is no executor shutdown phase, so late continuations of already accepted operations can still run during component shutdown.- Parameters:
prefix- worker thread name prefix- Returns:
- an executor for independently completable, potentially blocking tasks
-
cpuExecutor
Returns the classloader-wide executor for bounded chunks of CPU work, isolated from the JVM common pool. Workers are daemon threads and may clear thread locals between submissions. Callers own batching and context propagation; blocking I/O belongs on a worker executor instead. This shared executor must not be shut down by individual components. Nested parallel work must useinParallel(Supplier). -
cpuParallelism
public static int cpuParallelism()Returns the target CPU parallelism, including the work formerly performed by a parallel stream's caller. -
runInParallel
Runs a parallel terminal operation with the same isolation and completion contract asinParallel(Supplier). -
inParallel
Evaluates a parallel stream terminal operation on the isolated CPU pool and awaits its result. Nested calls already on that pool run directly. A caller's unrelated fork/join pool is never reused. The operation must complete its parallel work before returning; returning a lazy stream does not isolate it. Context must be propagated explicitly to the operation and its parallel tasks.- Type Parameters:
T- result type- Parameters:
operation- terminal operation- Returns:
- the operation's result
-
supportsVirtualThreadWorkers
Deprecated.SDK v2 requires Java 25 or newer, so callers no longer need a runtime-version check.Returnstrue: virtual workers are available on every supported Java runtime. -
tryAccept
-
silentTest
-
join
public static byte[] join(byte[]... chunks) Combines multiple byte arrays into a single byte array by concatenating their contents in the given order.
-