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

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

There is a newer version: 3.7.0
Show newest version
package com.github.chaosfirebolt.converter.api.cache;

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.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Caches values in a {@link Map}.
 *
 * @param  type of the key, by which the value is saved
 * @param  type of the cached value
 * @deprecated deprecated in favour of {@link DefaultCache}
 */
@Deprecated(since = "3.3.0", forRemoval = true)
public abstract class MapCache extends BaseCache implements InitializationCapable {

  /**
   * Map holding cached instances
   */
  private final Map cache;
  private final InitializationData> initializationData;

  /**
   * @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
   * @param cache             Map to store computed values in
   */
  protected MapCache(Function computation, Supplier exceptionSupplier, Map cache) {
    this(computation, exceptionSupplier, cache, new NoOpMapData<>());
  }

  /**
   * @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
   * @param cache              Map to store computed values in
   * @param initializationData source of the initial data tobe stored in cache
   */
  protected MapCache(Function computation, Supplier exceptionSupplier, Map cache, InitializationData> initializationData) {
    super(computation, exceptionSupplier);
    this.cache = cache;
    this.initializationData = initializationData;
  }

  @Override
  protected Optional computeIfAbsent(K key, Function computation) {
    V value = cacheIfAbsent(cache, key, computation);
    return Optional.ofNullable(value);
  }

  /**
   * Computes and caches the key-value pair, if there is no mapping found for this key.
   *
   * @param cache       cache to store value in
   * @param key         key to search the value by
   * @param computation function to compute the value, if a cache is not found
   * @return cached value, if present, otherwise computed and cached value
   */
  protected V cacheIfAbsent(Map cache, K key, Function computation) {
    return cache.computeIfAbsent(key, computation);
  }

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

  @Override
  public void initialize() {
    cache.putAll(initializationData.getData());
    initializationData.cleanup();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy