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

com.github.tonivade.purefun.MemoizedFunction Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2020, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purefun;

import static java.util.Collections.synchronizedMap;
import static java.util.Objects.requireNonNull;

import java.util.HashMap;
import java.util.Map;

final class MemoizedFunction implements Function1 {

  private final Map cache = synchronizedMap(new HashMap<>());
  private final Function1 function;

  MemoizedFunction(Function1 function) {
    this.function = requireNonNull(function);
  }

  @Override
  public R run(T value) {
    return cache.computeIfAbsent(value, function::apply);
  }

  @Override
  public Function1 memoized() {
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy