LazySupplier.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. // import javax.annotation.concurrent.ThreadSafe;
  28. import jakarta.annotation.Nonnull;
  29. import java.util.concurrent.atomic.AtomicBoolean;
  30. import java.util.concurrent.atomic.AtomicReference;
  31. import java.util.function.Supplier;
  32. import it.tidalwave.util.annotation.VisibleForTesting;
  33. import lombok.EqualsAndHashCode;
  34. import lombok.Getter;
  35. import lombok.RequiredArgsConstructor;

  36. /***************************************************************************************************************************************************************
  37.  *
  38.  * A supplier of an object that is lazily evaluated (when its value is requested for the first time). It warranties
  39.  * that the real wrapped supplier is called only once.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  * @since   3.2-ALPHA-13
  43.  *
  44.  **************************************************************************************************************************************************************/
  45. /* @ThreadSafe */ @RequiredArgsConstructor(staticName = "of") @EqualsAndHashCode(of = {"ref", "initialized"})
  46. public class LazySupplier<T> implements Supplier<T>
  47.   {
  48.     @Nonnull
  49.     private final Supplier<T> supplier;

  50.     @VisibleForTesting final AtomicReference<T> ref = new AtomicReference<>(null);

  51.     @Getter
  52.     @VisibleForTesting final AtomicBoolean initialized = new AtomicBoolean(false);

  53.     /** {@inheritDoc} */
  54.     @Override @Nonnull
  55.     public synchronized T get()
  56.       {
  57.         // AtomicReference.updateAndGet() not good because in case of contention it might call supplier multiple times
  58.         // We don't mess with double check for null since nowadays synchronized is fast
  59.         if (ref.get() == null)
  60.           {
  61.             ref.set(supplier.get());
  62.             initialized.set(true);
  63.           }

  64.         return ref.get();
  65.       }

  66.     public synchronized void clear()
  67.       {
  68.         ref.set(null);
  69.       }

  70.     public synchronized void set (@Nonnull final T ref)
  71.       {
  72.         this.ref.set(ref);
  73.       }

  74.     @Nonnull
  75.     public String toString()
  76.       {
  77.         return String.format("LazySupplier(%s)", (ref.get() == null && !initialized.get()) ? "<not initialised>" : ref.get());
  78.       }
  79.   }