Core utilities and common classes for Elasticsearch, providing fundamental building blocks like resource management, IO utilities, time handling, and reference counting.
—
Elasticsearch Core provides checked exception variants of standard Java functional interfaces, enabling functional programming patterns while maintaining proper exception handling. These interfaces allow throwing checked exceptions from lambda expressions and method references, which is not possible with standard Java functional interfaces.
Function that can throw checked exceptions, providing the same functionality as java.util.function.Function but with exception handling.
/**
* Represents a function that accepts one argument and produces a result,
* and may throw a checked exception
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
* @param <E> the type of the checked exception that may be thrown
*/
@FunctionalInterface
public interface CheckedFunction<T, R, E extends Exception> {
/**
* Applies this function to the given argument
* @param t the function argument
* @return the function result
* @throws E if an error occurs during function execution
*/
R apply(T t) throws E;
}Usage Examples:
import org.elasticsearch.core.CheckedFunction;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
// Method reference with checked exception
CheckedFunction<Path, String, IOException> fileReader = Files::readString;
// Lambda with checked exception
CheckedFunction<String, Integer, NumberFormatException> parser =
value -> {
if (value == null) throw new NumberFormatException("Null value");
return Integer.parseInt(value);
};
// Usage in stream-like operations
public <T, R> List<R> transformWithExceptions(
List<T> items,
CheckedFunction<T, R, Exception> transformer
) throws Exception {
List<R> results = new ArrayList<>();
for (T item : items) {
results.add(transformer.apply(item));
}
return results;
}Consumer that can throw checked exceptions, with support for chaining operations.
/**
* Represents an operation that accepts a single input argument and returns no
* result, and may throw a checked exception
*
* @param <T> the type of the input to the operation
* @param <E> the type of the checked exception that may be thrown
*/
@FunctionalInterface
public interface CheckedConsumer<T, E extends Exception> {
/**
* Performs this operation on the given argument
* @param t the input argument
* @throws E if an error occurs during operation execution
*/
void accept(T t) throws E;
/**
* Returns a composed CheckedConsumer that performs, in sequence, this
* operation followed by the after operation
* @param after the operation to perform after this operation
* @return a composed CheckedConsumer
*/
default CheckedConsumer<T, E> andThen(CheckedConsumer<? super T, E> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}Usage Examples:
import org.elasticsearch.core.CheckedConsumer;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
// File writing consumer
CheckedConsumer<String, IOException> fileWriter = content -> {
Path tempFile = Files.createTempFile("temp", ".txt");
Files.writeString(tempFile, content);
};
// Chaining consumers
CheckedConsumer<String, IOException> logAndWrite =
((CheckedConsumer<String, IOException>) System.out::println)
.andThen(fileWriter);
// Method reference with checked exception
CheckedConsumer<Path, IOException> fileDeleter = Files::delete;
// Usage in batch operations
public void processItems(List<String> items, CheckedConsumer<String, IOException> processor)
throws IOException {
for (String item : items) {
processor.accept(item);
}
}Supplier that can throw checked exceptions, useful for lazy evaluation with exception handling.
/**
* Represents a supplier of results that may throw a checked exception
*
* @param <T> the type of results supplied by this supplier
* @param <E> the type of the checked exception that may be thrown
*/
@FunctionalInterface
public interface CheckedSupplier<T, E extends Exception> {
/**
* Gets a result
* @return a result
* @throws E if an error occurs during result generation
*/
T get() throws E;
}Usage Examples:
import org.elasticsearch.core.CheckedSupplier;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
// Lazy configuration loading
CheckedSupplier<Properties, IOException> configLoader = () -> {
Properties props = new Properties();
Path configFile = Path.of("config.properties");
if (Files.exists(configFile)) {
props.load(Files.newInputStream(configFile));
}
return props;
};
// Database connection supplier
CheckedSupplier<Connection, SQLException> connectionSupplier =
() -> DriverManager.getConnection(url, username, password);
// Memoized supplier pattern
public static <T, E extends Exception> CheckedSupplier<T, E> memoize(
CheckedSupplier<T, E> supplier
) {
return new CheckedSupplier<T, E>() {
private volatile T cached;
private volatile boolean computed;
@Override
public T get() throws E {
if (!computed) {
synchronized (this) {
if (!computed) {
cached = supplier.get();
computed = true;
}
}
}
return cached;
}
};
}Runnable that can throw checked exceptions, perfect for background tasks that need exception handling.
/**
* Represents a runnable task that may throw a checked exception
*
* @param <E> the type of the checked exception that may be thrown
*/
@FunctionalInterface
public interface CheckedRunnable<E extends Exception> {
/**
* Executes the runnable task
* @throws E if an error occurs during task execution
*/
void run() throws E;
}Usage Examples:
import org.elasticsearch.core.CheckedRunnable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
// File cleanup task
CheckedRunnable<IOException> cleanupTask = () -> {
Path tempDir = Path.of("temp");
if (Files.exists(tempDir)) {
Files.walk(tempDir)
.sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
};
// Background maintenance task
CheckedRunnable<Exception> maintenanceTask = () -> {
// Perform maintenance operations that may throw various exceptions
cleanupOldFiles();
compactDatabase();
refreshCaches();
};
// Utility for converting CheckedRunnable to standard Runnable
public static Runnable unchecked(CheckedRunnable<? extends Exception> checkedRunnable) {
return () -> {
try {
checkedRunnable.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
// Usage with CompletableFuture
CompletableFuture<Void> future = CompletableFuture.runAsync(
unchecked(cleanupTask)
);The checked functional interfaces integrate well with exception handling patterns:
// Exception handling utility
public static <T> T handleExceptions(CheckedSupplier<T, Exception> supplier) {
try {
return supplier.get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// Batch processing with exception collection
public static <T> List<Exception> processAll(
List<T> items,
CheckedConsumer<T, Exception> processor
) {
List<Exception> exceptions = new ArrayList<>();
for (T item : items) {
try {
processor.accept(item);
} catch (Exception e) {
exceptions.add(e);
}
}
return exceptions;
}Install with Tessl CLI
npx tessl i tessl/maven-org-elasticsearch--elasticsearch-core