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

com.github.chaosfirebolt.converter.api.cache.DefaultCache Maven / Gradle / Ivy

package com.github.chaosfirebolt.converter.api.cache;

import com.github.chaosfirebolt.converter.api.cache.storage.Computation;
import com.github.chaosfirebolt.converter.api.cache.storage.Storage;
import com.github.chaosfirebolt.converter.api.initialization.InitializationCapable;
import com.github.chaosfirebolt.converter.api.initialization.InitializationData;
import com.github.chaosfirebolt.converter.api.initialization.NoOpMapData;

import java.util.Map;
import java.util.function.Supplier;

/**
 * The default cache implementation.
 *
 * @param  key type
 * @param  value type
 */
public class DefaultCache implements Cache, InitializationCapable {

  private final Storage storage;
  private final Computation computation;
  private final InitializationData> initializationData;
  private final Supplier exceptionSupplier;
  private final Extraction extraction;

  /**
   * Defaults to no initialization data.
   *
   * @param storage           storage for cached values
   * @param computation       function to compute the value, if a cache is not found
   * @param exceptionSupplier supplier for an exception to be thrown, if a value can't be returned
   */
  public DefaultCache(Storage storage, Computation computation, Supplier exceptionSupplier) {
    this(storage, computation, new NoOpMapData<>(), exceptionSupplier);
  }

  /**
   * @param storage            storage for cached values
   * @param computation        function to compute the value, if a cache is not found
   * @param initializationData source of the initial data tobe stored in cache
   * @param exceptionSupplier  supplier for an exception to be thrown, if a value can't be returned
   */
  public DefaultCache(Storage storage, Computation computation, InitializationData> initializationData, Supplier exceptionSupplier) {
    this(storage, computation, initializationData, exceptionSupplier, Storage::compute);
  }

  /**
   * @param storage            storage for cached values
   * @param computation        function to compute the value, if a cache is not found
   * @param initializationData source of the initial data tobe stored in cache
   * @param exceptionSupplier  supplier for an exception to be thrown, if a value can't be returned
   * @param extraction         operation to extract the value
   */
  public DefaultCache(Storage storage, Computation computation, InitializationData> initializationData,
                      Supplier exceptionSupplier, Extraction extraction) {
    this.storage = storage;
    this.computation = computation;
    this.initializationData = initializationData;
    this.exceptionSupplier = exceptionSupplier;
    this.extraction = extraction;
  }

  /**
   * @param key key to search the value by
   * @return the value
   * @throws RuntimeException     thrown if unable to return a value
   * @throws NullPointerException if key is null
   */
  @Override
  public final V getValue(K key) {
    try {
      return computeIfAbsent(storage, key, computation);
    } catch (NullPointerException | IllegalArgumentException exc) {
      throw exc;
    } catch (Exception cause) {
      RuntimeException exc = exceptionSupplier.get();
      exc.initCause(cause);
      throw exc;
    }
  }

  /**
   * Computes, if necessary and returns the value.
   *
   * @param storage     storage for cached values
   * @param key         key to search the value by
   * @param computation computation to invoke, if there is no cached value
   * @return the value
   */
  protected V computeIfAbsent(Storage storage, K key, Computation computation) {
    return extraction.extract(storage, key, computation);
  }

  @Override
  public void clear() {
    storage.clear();
  }

  @Override
  public void initialize() {
    for (Map.Entry entry : initializationData.getData().entrySet()) {
      storage.store(entry.getKey(), entry.getValue());
    }
    initializationData.cleanup();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy