package com.airlock.iam.base.domain.model.mapification;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.function.Function;
import java.util.function.Supplier;

public class LazySupplier<R, T> implements Supplier<T> {
   private final R rawValue;
   private final Function<R, T> deserializer;
   private T result;

   @Override
   public synchronized T get() {
      if (this.result == null) {
         this.result = this.deserializer.apply(this.rawValue);
      }

      return this.result;
   }

   public boolean hasBeenSupplied() {
      return this.result != null;
   }

   public static <R, T> LazySupplier<R, T> alreadySupplied(T result) {
      return new LazySupplier<>(null, null, result);
   }

   @SuppressFBWarnings(justification = "generated code")
   public LazySupplier(R rawValue, Function<R, T> deserializer) {
      this.rawValue = rawValue;
      this.deserializer = deserializer;
   }

   @SuppressFBWarnings(justification = "generated code")
   private LazySupplier(R rawValue, Function<R, T> deserializer, T result) {
      this.rawValue = rawValue;
      this.deserializer = deserializer;
      this.result = result;
   }

   @SuppressFBWarnings(justification = "generated code")
   public R getRawValue() {
      return this.rawValue;
   }
}
