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

com.github.blemale.scaffeine.AsyncCacheLoaderAdapter Maven / Gradle / Ivy

The newest version!
package com.github.blemale.scaffeine;

import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import scala.Function1;
import scala.Function2;
import scala.Option;
import scala.concurrent.Future;

import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static scala.compat.java8.FutureConverters.toJava;

class AsyncCacheLoaderAdapter implements AsyncCacheLoader {

  private final Function1> loader;
  private final Option>> reloadLoader;

  AsyncCacheLoaderAdapter(Function1> loader, Option>> reloadLoader) {
    this.loader = loader;
    this.reloadLoader = reloadLoader;
  }

  @Nonnull
  @Override
  public CompletableFuture asyncLoad(@Nonnull K key, @Nonnull Executor executor) {
    return toJava(loader.apply(key)).toCompletableFuture();
  }

  @Nonnull
  @Override
  public CompletableFuture asyncReload(@Nonnull K key, @Nonnull V oldValue, @Nonnull Executor executor) {
    if (reloadLoader.isEmpty()) {
      return AsyncCacheLoader.super.asyncReload(key, oldValue, executor);
    } else {
      return toJava(reloadLoader.get().apply(key, oldValue)).toCompletableFuture();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy