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


The following commit(s) were added to refs/heads/master by this push:
     new dd89ed0  Add LazyValue::map
dd89ed0 is described below

commit dd89ed0b50ff22fb5dd528cb874a91ea5d4d67a1
Author: Matt Sicker <[email protected]>
AuthorDate: Sun Mar 20 17:46:40 2022 -0500

    Add LazyValue::map
    
    Signed-off-by: Matt Sicker <[email protected]>
---
 .../main/java/org/apache/logging/log4j/util/LazyValue.java   | 12 ++++++++++--
 .../org/apache/logging/log4j/plugins/util/PluginType.java    |  9 +++++----
 2 files changed, 15 insertions(+), 6 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 fe60607..8fdf269 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
@@ -17,6 +17,7 @@
 
 package org.apache.logging.log4j.util;
 
+import java.util.function.Function;
 import java.util.function.Supplier;
 
 /**
@@ -52,7 +53,14 @@ public final class LazyValue<T> implements Supplier<T> {
         return value;
     }
 
-    public synchronized void reset() {
-        value = null;
+    /**
+     * Creates a LazyValue that maps the result of this LazyValue to another 
value.
+     *
+     * @param function mapping function to transform the result of this lazy 
value
+     * @param <R>      the return type of the new lazy value
+     * @return the new lazy value
+     */
+    public <R> LazyValue<R> map(final Function<? super T, ? extends R> 
function) {
+        return new LazyValue<>(() -> function.apply(get()));
     }
 }
diff --git 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginType.java
 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginType.java
index f2d7557..55adbc5 100644
--- 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginType.java
+++ 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/util/PluginType.java
@@ -63,17 +63,18 @@ public class PluginType<T> {
      */
     public PluginType(final PluginEntry pluginEntry, final ClassLoader 
classLoader, final LookupSelector lookupSelector) {
         this.pluginEntry = pluginEntry;
-        this.pluginClass = new LazyValue<>(() -> {
+        final LazyValue<Class<T>> pluginClass = new LazyValue<>(() -> {
             try {
                 return 
TypeUtil.cast(classLoader.loadClass(pluginEntry.getClassName()));
-            } catch (ClassNotFoundException e) {
+            } catch (final ClassNotFoundException e) {
                 throw new IllegalStateException("No class named " + 
pluginEntry.getClassName() +
                         " located for element " + pluginEntry.getName(), e);
             }
         });
-        this.pluginLookup = new LazyValue<>(() -> {
+        this.pluginClass = pluginClass;
+        this.pluginLookup = pluginClass.map(clazz -> {
             try {
-                return lookupSelector.in(pluginClass.get());
+                return lookupSelector.in(clazz);
             } catch (final IllegalAccessException e) {
                 throw new IllegalAccessError(e.getMessage());
             }

Reply via email to