1 /* 2 * ************************************************************************************************************************************************************* 3 * 4 * TheseFoolishThings: Miscellaneous utilities 5 * http://tidalwave.it/projects/thesefoolishthings 6 * 7 * Copyright (C) 2009 - 2026 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 28 // import javax.annotation.concurrent.ThreadSafe; 29 import jakarta.annotation.Nonnull; 30 import java.util.Optional; 31 import java.util.concurrent.atomic.AtomicBoolean; 32 import java.util.concurrent.atomic.AtomicReference; 33 import java.util.function.Supplier; 34 import it.tidalwave.util.annotation.VisibleForTesting; 35 import lombok.EqualsAndHashCode; 36 import lombok.RequiredArgsConstructor; 37 38 /*************************************************************************************************************************************************************** 39 * A supplier of an object that is lazily evaluated (when its value is requested for the first time). It warranties 40 * that the real wrapped supplier is called only once. 41 * @author Fabrizio Giudici 42 * @since 3.2-ALPHA-13 43 **************************************************************************************************************************************************************/ 44 /* @ThreadSafe */ @RequiredArgsConstructor(staticName = "of") @EqualsAndHashCode(of = {"ref", "initialized"}) 45 public class LazySupplier<T> implements Supplier<T> 46 { 47 @Nonnull 48 private final Supplier<T> supplier; 49 50 @VisibleForTesting final AtomicReference<T> ref = new AtomicReference<>(null); 51 52 @VisibleForTesting final AtomicBoolean initialized = new AtomicBoolean(false); 53 54 /*********************************************************************************************************************************************************** 55 * {@inheritDoc} 56 **********************************************************************************************************************************************************/ 57 @Override @Nonnull 58 public synchronized T get() 59 { 60 // AtomicReference.updateAndGet() not good because in case of contention it might call supplier multiple times 61 // We don't mess with double check for null since nowadays synchronized is fast 62 if (ref.get() == null) 63 { 64 ref.set(supplier.get()); 65 initialized.set(true); 66 } 67 68 return ref.get(); 69 } 70 71 /*********************************************************************************************************************************************************** 72 * Returns the value without computing it: unlike {@link #get()}, this method never calls the wrapped supplier, so it can be used to find out whether the 73 * value is available without the side effects of computing it. The returned {@link Optional} contains the value if it has been computed and is not null. 74 * @return the value, if available, or an empty {@code Optional} 75 * @since 5.0-ALPHA-4 76 **********************************************************************************************************************************************************/ 77 @Nonnull 78 public synchronized Optional<T> peek() 79 { 80 return initialized.get() ? Optional.ofNullable(ref.get()) : Optional.empty(); 81 } 82 83 /*********************************************************************************************************************************************************** 84 * Clears the supplier. 85 **********************************************************************************************************************************************************/ 86 public synchronized void clear() 87 { 88 ref.set(null); 89 } 90 91 /*********************************************************************************************************************************************************** 92 * Changes the reference. 93 * @param ref the new reference 94 **********************************************************************************************************************************************************/ 95 public synchronized void set (@Nonnull final T ref) 96 { 97 this.ref.set(ref); 98 initialized.set(true); 99 } 100 101 /*********************************************************************************************************************************************************** 102 * {@inheritDoc} 103 **********************************************************************************************************************************************************/ 104 @Nonnull 105 public String toString() 106 { 107 return String.format("LazySupplier(%s)", (ref.get() == null && !initialized.get()) ? "<not initialised>" : ref.get()); 108 } 109 }