All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.restate.sdk.common.function.ThrowingBiConsumer Maven / Gradle / Ivy

The newest version!
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.common.function;

import java.util.function.BiConsumer;

/** Like {@link BiConsumer} but can throw checked exceptions. */
@FunctionalInterface
public interface ThrowingBiConsumer {
  void accept(T var1, U var2) throws Throwable;

  static  BiConsumer wrap(ThrowingBiConsumer fn) {
    return fn.asBiConsumer();
  }

  default BiConsumer asBiConsumer() {
    return (t, u) -> {
      try {
        this.accept(t, u);
      } catch (RuntimeException e) {
        throw e;
      } catch (Exception e) {
        throw new RuntimeException(e);
      } catch (Throwable e) {
        // Make sure we propagate java.lang.Error
        sneakyThrow(e);
      }
    };
  }

  @SuppressWarnings("unchecked")
  private static  void sneakyThrow(Throwable e) throws E {
    throw (E) e;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy