Using generics magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
You can use the Throws.java utility class, which encapsulates common Function interfaces. It helps you wrap lambdas that throw checked exceptions. For example:
Consumer<String> consumer = Throws.wrapConsumer(s -> {throw new Throwable();});
Using generics magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Using generics magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
You can use the Throws.java utility class, which encapsulates common Function interfaces. It helps you wrap lambdas that throw checked exceptions. For example:
Consumer<String> consumer = Throws.wrapConsumer(s -> {throw new Throwable();});
Use genericUsing generics magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Use generic magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Using generics magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Use generic magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T>ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
acceptEx((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
<EX extends Throwable> void acceptExacceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEx<ChildNode>ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Use generic magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T> extends Consumer<T> {
@Override
default void accept(T t) {
acceptEx(t);
}
<EX extends Throwable> void acceptEx(T t) throws EX;
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEx<ChildNode>) this::deleteChild);
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
Use generic magic:
public class TempTest {
@FunctionalInterface
public interface ConsumerEX<T, EX extends Throwable> extends Consumer<T> {
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
default void accept(T t) {
((ConsumerEX<T, RuntimeException>) this).acceptEX(t);
}
void acceptEX(T t) throws EX;
static <T> Consumer<T> wrap(ConsumerEX<T, ?> consumer) {
return consumer;
}
}
void deleteEntity(Node node) {
node.getChildren().forEach((ConsumerEX<ChildNode, ?>) this::deleteChild);
//or
node.getChildren().forEach(ConsumerEX.wrap(this::deleteChild));
}
void deleteChild(Object child) throws SomeException {
//some code
throw new SomeException();
}
// mock class
private static class Node {
public List<ChildNode> getChildren() {return Collections.singletonList(new ChildNode());}
}
private static class ChildNode {}
private static class SomeException extends Throwable {}
}
lang-java