aQute.lib.exceptions.SupplierWithException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.bndtools.templating.gitrepo Show documentation
Show all versions of org.bndtools.templating.gitrepo Show documentation
org.bndtools.templating.gitrepo
package aQute.lib.exceptions;
import static java.util.Objects.requireNonNull;
import java.util.function.Supplier;
/**
* Supplier interface that allows exceptions.
*
* @param the result type
*/
@FunctionalInterface
public interface SupplierWithException {
R get() throws Exception;
default Supplier orElseThrow() {
return () -> {
try {
return get();
} catch (Exception e) {
throw Exceptions.duck(e);
}
};
}
default Supplier orElse(R orElse) {
return () -> {
try {
return get();
} catch (Exception e) {
return orElse;
}
};
}
default Supplier orElseGet(Supplier extends R> orElseGet) {
requireNonNull(orElseGet);
return () -> {
try {
return get();
} catch (Exception e) {
return orElseGet.get();
}
};
}
static Supplier asSupplier(SupplierWithException unchecked) {
return unchecked.orElseThrow();
}
static Supplier asSupplierOrElse(SupplierWithException unchecked, R orElse) {
return unchecked.orElse(orElse);
}
static Supplier asSupplierOrElseGet(SupplierWithException unchecked, Supplier extends R> orElseGet) {
return unchecked.orElseGet(orElseGet);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy