Package io.fluxcapacitor.common
Class ObjectUtils
java.lang.Object
io.fluxcapacitor.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 standardRunnable
into 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 Properties
asProperties
(String content) Parses JavaProperties
from a string.static Runnable
asRunnable
(ThrowingRunnable runnable) static Runnable
asRunnable
(Callable<?> callable) static Stream
<?> Converts an object into a stream.static <T> Supplier
<T> asSupplier
(Callable<T> callable) static <T> T
Calls the given callable, forcibly rethrowing exceptions.static <T> Stream
<T> Concatenates multiple streams into a single flat stream.static Properties
copyOf
(Properties properties) Creates a deep copy of a Properties instance.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 Object
forceThrow
(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 ifcheck
is true.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,
U, R> MemoizingBiFunction <T, U, R> memoize
(BiFunction<T, U, R> supplier) static <K,
V> MemoizingFunction <K, V> static <T> MemoizingSupplier
<T> static Properties
merge
(Properties a, Properties b) Merges two Properties instances into a new one.static ExecutorService
newNamedWorkStealingPool
(int parallelism, String prefix) Creates a namedForkJoinPool
with the given parallelism.static ThreadFactory
newThreadFactory
(String prefix) Creates a newThreadFactory
with a named prefix.static String
newThreadName
(String prefix) Generates a unique thread name with the given prefix.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 void
run
(ThrowingRunnable runnable) Executes the runnable, forcibly rethrowing exceptions as unchecked.static <T> Consumer
<? super T> Returns a consumer that logs errors instead of propagating them.static Runnable
Wraps a runnable in a try/catch block that logs any exception on execution.static void
Executes the runnable and logs any thrown exception.static Throwable
Recursively unwraps the cause of common wrapping exceptions.
-
Constructor Details
-
ObjectUtils
public ObjectUtils()
-
-
Method Details
-
noOpPredicate
Returns a predicate that always evaluates to true. -
noOpBiPredicate
Returns a bi-predicate that always evaluates to true. -
distinctByKey
Returns a predicate that filters out duplicates based on a key extractor. -
iterate
Creates a stream that stops once the break condition evaluates true. -
concat
Concatenates multiple streams into a single flat stream. -
deduplicate
Deduplicates elements in the list, preserving the last occurrence. -
deduplicate
Deduplicates elements using a key extractor, preserving the last occurrence. -
deduplicate
Deduplicates elements using a key extractor, optionally keeping the first occurrence. -
asStream
Converts an object into a stream. Supports Collection, Stream, Optional, or single value. -
ifTrue
Returns a consumer that runs the task only ifcheck
is true. -
forceThrow
Forces the given throwable to be thrown. -
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
Converts a standardRunnable
into aCallable<Object>
that returns null. -
tryRun
Executes the runnable and logs any thrown exception. -
tryCatch
Wraps a runnable in a try/catch block that logs any exception on execution. -
asSupplier
-
asRunnable
-
asFunction
-
asConsumer
-
asRunnable
-
getBytes
Extracts the byte array from aByteBuffer
. -
unwrapException
Recursively unwraps the cause of common wrapping exceptions. -
asProperties
Parses JavaProperties
from a string. -
copyOf
Creates a deep copy of a Properties instance. -
merge
Merges two Properties instances into a new one. -
memoize
-
memoize
-
memoize
-
newThreadName
Generates a unique thread name with the given prefix. -
newThreadFactory
Creates a newThreadFactory
with a named prefix. -
newNamedWorkStealingPool
Creates a namedForkJoinPool
with the given parallelism. -
tryAccept
Returns a consumer that logs errors instead of propagating them. -
join
public static byte[] join(byte[]... chunks) Combines multiple byte arrays into a single byte array by concatenating their contents in the given order.
-