package com.airlock.iam.base.misc.data;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.util.function.Consumer;

public class OptionalValue<T> {
   private boolean hasValue;
   @Nullable
   private T value;

   public static <T> OptionalValue<T> none() {
      return new OptionalValue<>(false, null);
   }

   public static <T> OptionalValue<T> of(@Nullable T value) {
      return new OptionalValue<>(true, value);
   }

   public void ifPresent(Consumer<? super T> consumer) {
      if (this.hasValue) {
         consumer.accept(this.value);
      }
   }

   @Nullable
   public T orElse(T alternative) {
      return this.hasValue ? this.value : alternative;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean hasValue() {
      return this.hasValue;
   }

   @Nullable
   @SuppressFBWarnings(justification = "generated code")
   public T value() {
      return this.value;
   }

   @SuppressFBWarnings(justification = "generated code")
   public OptionalValue(boolean hasValue, @Nullable T value) {
      this.hasValue = hasValue;
      this.value = value;
   }
}
