com.github.tonivade.purefun.CheckedRunnable Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2020, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
/**
* This interface represents a {@link Runnable} instance but it can throws any exception.
*/
@FunctionalInterface
public interface CheckedRunnable extends Recoverable {
void run() throws Throwable;
default Producer asProducer() {
return () -> { run(); return Unit.unit(); };
}
default CheckedRunnable andThen(CheckedRunnable next) {
return () -> { run(); next.run(); };
}
default Runnable recover(Consumer1 mapper) {
return () -> {
try {
run();
} catch(Throwable e) {
mapper.accept(e);
}
};
}
default Runnable unchecked() {
return recover(this::sneakyThrow);
}
static CheckedRunnable failure(Producer supplier) {
return () -> { throw supplier.get(); };
}
static CheckedRunnable of(CheckedRunnable runnable) {
return runnable;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy