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

com.squarespace.cldrengine.utils.Cache Maven / Gradle / Ivy

The newest version!
package com.squarespace.cldrengine.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Links an arrow function to an LRU cache. The function converts
 * a string to a value of type T. The string itself is used as
 * the cache key.
 *
 * Examples:
 *  * Caching a number or date pattern. The cache key is the string
 *    representation of the pattern.
 *  * Caching any object that is expensive to create, where the cache
 *    key identifies the type of object to cache.
 *
 * Ported from @phensley/cldr-utils src/cache.ts
 * Modified to use ConcurrentHashMap
 */
public class Cache {

  private ConcurrentHashMap storage;
  private Function builder;

  public Cache(Function builder, int initialCapacity) {
    this.builder = builder;
    this.storage = new ConcurrentHashMap<>(initialCapacity);
  }

  public int size() {
    return this.storage.size();
  }

  public V get(String raw) {
    return this.storage.computeIfAbsent(raw, this.builder);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy