Skip to main content

Function Package

The org.apache.juneau.commons.function package extends the standard java.util.function toolkit with higher-arity functional interfaces, checked-exception–friendly variants, small immutable tuples, and lazy/nullable suppliers used throughout the framework.

Higher-Arity Functional Interfaces​

Two-to-five argument extensions of the standard Consumer and Function patterns.

Consumer2​

An operation that accepts two arguments and returns no result. Higher-arity siblings: Consumer3, Consumer4, Consumer5.

Function2​

A function that accepts two arguments and produces a result. Higher-arity siblings: Function3, Function4, Function5.

Checked-Exception Variants​

Functional interfaces whose functional method is allowed to throw checked exceptions — useful for passing exception-throwing code where the JDK interfaces would force a try/catch.

Snippet​

Identical to Runnable but its run() method may throw a checked exception.

ThrowingConsumer​

A Consumer whose acceptThrows(T) may throw a checked exception. Higher-arity siblings: ThrowingConsumer2, ThrowingConsumer3, ThrowingConsumer4, ThrowingConsumer5.

ThrowingFunction​

A Function whose functional method may throw a checked exception. Higher-arity siblings: ThrowingFunction2, ThrowingFunction3, ThrowingFunction4, ThrowingFunction5.

ThrowingSupplier​

A Supplier whose get() may throw a checked exception.

Lazy and Nullable Suppliers​

NullableSupplier​

A Supplier whose value may be null, with Optional-like null-safe query methods.

NullableSupplier<String> s = NullableSupplier.ofNullable(computeOrNull());

Memoizer​

A thread-safe supplier that caches the result of its first call and can be reset.

Memoizer<Config> config = new Memoizer<>(() -> loadExpensiveConfig());
Config c = config.get(); // computed once
config.reset(); // next get() recomputes

Suppliers​

Static factory methods for creating memoizing suppliers, without needing to call the NullableSupplier/Memoizer constructors directly.

  • Suppliers.memoize(Supplier) — wraps a Supplier in a thread-safe, non-resettable memoizing NullableSupplier. The underlying supplier is invoked at most once (races are tolerated — if multiple threads call get() before the first result is cached, the supplier may be invoked more than once, but every caller ends up with the same cached value).
  • Suppliers.memoizer(Supplier) — wraps a Supplier in a resettable Memoizer, equivalent to new Memoizer<>(supplier).
// One-shot memoization - no reset needed
NullableSupplier<Config> config = Suppliers.memoize(() -> loadExpensiveConfig());
Config c = config.get(); // computed once and cached

// Resettable memoization - equivalent to new Memoizer<>(supplier)
Memoizer<Config> config2 = Suppliers.memoizer(() -> loadExpensiveConfig());
config2.reset(); // next get() recomputes

Tuples​

Immutable value tuples with content-based equals()/hashCode() — handy as composite map keys.

Tuple1​

A single-value tuple. Wider tuples: Tuple2, Tuple3, Tuple4, Tuple5.

Tuple2<String,Integer> key = Tuple2.of("foo", 42);
String a = key.getA();
Integer b = key.getB();

Bean Streaming​

Lifecycle-aware source/target interfaces that let the marshalling layer stream beans one at a time without buffering an entire document.

BeanSupplier​

A lazy, one-at-a-time producer used as a serializer source (extends Iterable).

BeanConsumer​

A one-at-a-time consumer used as a parser target (extends ThrowingConsumer).

BeanChannel​

Combines BeanSupplier and BeanConsumer for round-trip use on the same property or class. ListBeanChannel is a simple ArrayList-backed implementation.

BeanFactory​

A universal factory interface used with @Marshalled(factory=...) as the single DI hook for framework-instantiated classes. BeanFactory.Void is the sentinel default meaning "no factory specified".