You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2022/03/31 05:49:52 UTC

[logging-log4j2] 02/07: Use null sentinel in LazyValue

This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 9c160a9e0df002d840fb3b494ae8dcdb35428756
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Wed Mar 30 21:51:16 2022 -0500

    Use null sentinel in LazyValue
    
    Signed-off-by: Matt Sicker <ma...@apache.org>
---
 .../java/org/apache/logging/log4j/util/LazyValue.java    | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyValue.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyValue.java
index 0398868..20d7965 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyValue.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyValue.java
@@ -27,6 +27,13 @@ import java.util.function.Supplier;
  */
 public final class LazyValue<T> implements Supplier<T> {
 
+    private static final Object NULL_INSTANCE = new Object();
+
+    @SuppressWarnings("unchecked")
+    private static <T> T nullInstance() {
+        return (T) NULL_INSTANCE;
+    }
+
     /**
      * Creates a lazy value using the provided Supplier for initialization.
      */
@@ -53,11 +60,16 @@ public final class LazyValue<T> implements Supplier<T> {
             synchronized (this) {
                 value = this.value;
                 if (value == null) {
-                    this.value = value = supplier.get();
+                    value = supplier.get();
+                    this.value = value == null ? nullInstance() : value;
                 }
             }
         }
-        return value;
+        return value == NULL_INSTANCE ? null : value;
+    }
+
+    public void set(final T value) {
+        this.value = value;
     }
 
     /**