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

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

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

import static com.github.tonivade.purefun.Unit.unit;
import static java.util.Collections.synchronizedMap;
import static java.util.Objects.requireNonNull;

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

final class MemoizedProducer implements Producer {

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

  MemoizedProducer(Producer producer) {
    this.function = requireNonNull(producer).asFunction();
  }

  @Override
  public T get() {
    return cache.computeIfAbsent(unit(), function::apply);
  }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy