Class ObjectUtils

java.lang.Object
io.fluxcapacitor.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

    • 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.
    • 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.
    • 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)
    • newThreadName

      public static String newThreadName(String prefix)
      Generates a unique thread name with the given prefix.
    • newThreadFactory

      public static ThreadFactory newThreadFactory(String prefix)
      Creates a new ThreadFactory with a named prefix.
    • newNamedWorkStealingPool

      public static ExecutorService newNamedWorkStealingPool(int parallelism, String prefix)
      Creates a named ForkJoinPool with the given parallelism.
    • tryAccept

      public static <T> Consumer<? super T> tryAccept(Consumer<? super T> consumer)
      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.