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

com.zeroleak.throwingsupplier.ThrowingSupplier Maven / Gradle / Ivy

Go to download

Guava cache supplier extension to memoize exceptions, fallback, expiration control.

The newest version!
package com.zeroleak.throwingsupplier;

import com.google.common.base.Supplier;

public abstract class ThrowingSupplier implements Supplier> {
  private int attempts = 1;

  public abstract T getOrThrow() throws Exception;

  @Override
  public Throwing get() {
    Exception lastError = null;
    for (int i = 0; i < attempts; i++) {
      try {
        T value = getOrThrow();
        return new Throwing(value);
      } catch (Exception e) {
        lastError = e;
      }
    }
    return new Throwing(lastError);
  }

  public ThrowingSupplier attempts(int attempts) {
    this.attempts = attempts;
    return this;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy