Class ObjectUtils

java.lang.Object
io.fluxzero.common.ObjectUtils

public class ObjectUtils extends Object
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 Details

    • ObjectUtils

      public ObjectUtils()
  • Method Details

    • isBlank

      public static boolean isBlank(CharSequence value)
      Returns whether a character sequence is null, empty, or contains only whitespace.
    • isNumeric

      public static boolean isNumeric(CharSequence value)
      Returns whether a character sequence is non-empty and consists entirely of Unicode digits.
    • capitalize

      public static String capitalize(String value)
      Capitalizes the first character of a string, leaving null and empty strings unchanged.
    • stripAccents

      public static String stripAccents(String value)
      Removes accents from a string while retaining otherwise compatible Unicode characters.
    • stackTrace

      public static String stackTrace(Throwable throwable)
      Renders a throwable and its causes to the same text produced by Throwable.printStackTrace().
    • noOpPredicate

      public static <T> Predicate<T> noOpPredicate()
      Returns a predicate that always evaluates to true.
    • noOpBiPredicate

      public static <T,U> BiPredicate<T,U> noOpBiPredicate()
      Returns a bi-predicate that always evaluates to true.
    • distinctByKey

      public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor)
      Returns a predicate that filters out duplicates based on a key extractor.
    • iterate

      public static <T> Stream<T> iterate(T seed, UnaryOperator<T> f, Predicate<T> breakCondition)
      Creates a stream that stops once the break condition evaluates true.
    • concat

      @SafeVarargs public static <T> Stream<T> concat(Stream<? extends T>... streams)
      Concatenates multiple streams into a single flat stream.
    • deduplicate

      public static <T> List<T> deduplicate(List<T> list)
      Deduplicates elements in the list, preserving the last occurrence.
    • deduplicate

      public static <T> List<T> deduplicate(List<T> list, Function<T,?> idFunction)
      Deduplicates elements using a key extractor, preserving the last occurrence.
    • deduplicate

      public static <T> List<T> deduplicate(List<T> list, Function<T,?> idFunction, boolean keepFirst)
      Deduplicates elements using a key extractor, optionally keeping the first occurrence.
    • 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 within maxWeight.

      If the first item already exceeds maxWeight, it is still included so callers can make progress. A maxWeight of 0 or lower leaves the list unchanged.

    • asStream

      public static Stream<?> asStream(Object value)
      Converts an object into a stream. Supports Collection, Stream, Optional, or single value.
    • ifTrue

      public static Consumer<Runnable> ifTrue(boolean check)
      Returns a consumer that runs the task only if check is true.
    • forceThrow

      public static Object forceThrow(Throwable error)
      Forces the given throwable to be thrown.
    • call

      public static <T> T call(Callable<T> callable)
      Calls the given callable, forcibly rethrowing exceptions.
    • run

      public static void run(ThrowingRunnable runnable)
      Executes the runnable, forcibly rethrowing exceptions as unchecked.
    • asCallable

      public static Callable<?> asCallable(ThrowingRunnable runnable)
      Converts a ThrowingRunnable into a Callable<Object> that returns null.
    • asCallable

      public static Callable<?> asCallable(Runnable runnable)
      Converts a standard Runnable into a Callable<Object> that returns null.
    • tryRun

      public static void tryRun(Runnable task)
      Executes the runnable and logs any thrown exception.
    • tryCatch

      public static Runnable tryCatch(Runnable runnable)
      Wraps a runnable in a try/catch block that logs any exception on execution.
    • asSupplier

      public static <T> Supplier<T> asSupplier(Callable<T> callable)
    • asRunnable

      public static Runnable asRunnable(ThrowingRunnable runnable)
    • asFunction

      public static <T,R> Function<T,R> asFunction(ThrowingFunction<T,R> function)
    • asConsumer

      public static <T> Consumer<T> asConsumer(ThrowingConsumer<T> consumer)
    • asRunnable

      public static Runnable asRunnable(Callable<?> callable)
    • getBytes

      public static byte[] getBytes(ByteBuffer buffer)
      Extracts the byte array from a ByteBuffer.
    • unwrapException

      public static Throwable unwrapException(Throwable e)
      Recursively unwraps the cause of common wrapping exceptions.
    • rethrow

      public static RuntimeException rethrow(Throwable e)
      Unwraps then returns the given Throwable as an unchecked exception. If the provided throwable is a RuntimeException or Error, it is returned as-is. Otherwise, it is wrapped in a new RuntimeException.
      See Also:
    • asProperties

      public static Properties asProperties(String content)
      Parses Java Properties from a string.
    • copyOf

      public static Properties copyOf(Properties properties)
      Creates a deep copy of a Properties instance.
    • merge

      public static Properties merge(Properties a, Properties b)
      Merges two Properties instances into a new one.
    • memoize

      public static <T> MemoizingSupplier<T> memoize(Supplier<T> supplier)
    • memoize

      public static <K,V> MemoizingFunction<K,V> memoize(Function<K,V> supplier)
    • memoize

      public static <T,U,R> MemoizingBiFunction<T,U,R> memoize(BiFunction<T,U,R> supplier)
    • newPlatformThreadFactory

      public static ThreadFactory newPlatformThreadFactory(String prefix)
      Creates a new ThreadFactory with a named prefix.
    • newVirtualThreadFactory

      public static ThreadFactory newVirtualThreadFactory(String prefix)
      Creates a new named virtual-thread ThreadFactory.
    • newWorkerThreadFactory

      public static ThreadFactory newWorkerThreadFactory(String prefix)
      Creates a named virtual-thread factory for workers.
    • newWorkerPool

      public static ExecutorService newWorkerPool(String prefix, int poolSize)
      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 prefix
      poolSize - a positive legacy sizing hint; does not bound virtual-thread concurrency
    • newWorkerExecutor

      public static Executor newWorkerExecutor(String prefix)
      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

      public static Executor 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 use inParallel(Supplier).
    • cpuParallelism

      public static int cpuParallelism()
      Returns the target CPU parallelism, including the work formerly performed by a parallel stream's caller.
    • runInParallel

      public static void runInParallel(Runnable operation)
      Runs a parallel terminal operation with the same isolation and completion contract as inParallel(Supplier).
    • inParallel

      public static <T> T inParallel(Supplier<T> operation)
      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 public static boolean supportsVirtualThreadWorkers()
      Deprecated.
      SDK v2 requires Java 25 or newer, so callers no longer need a runtime-version check.
      Returns true: virtual workers are available on every supported Java runtime.
    • tryAccept

      public static <T> Consumer<? super T> tryAccept(Consumer<? super T> consumer)
      Returns a consumer that logs errors instead of propagating them.
    • silentTest

      public static <T> Predicate<T> silentTest(Predicate<T> predicate)
      Returns a predicate that silently returns false when the given predicate throws an error.
    • join

      public static byte[] join(byte[]... chunks)
      Combines multiple byte arrays into a single byte array by concatenating their contents in the given order.