You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by be...@apache.org on 2022/12/29 08:49:57 UTC

[tapestry-5] 02/02: TAP5-2743: add "ifSet" to PerThreadValue

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

benw pushed a commit to branch TAP5-2743
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git

commit 0a644287cdc21b8b271b169f244ff3fbe0032f07
Author: Ben Weidig <be...@netzgut.net>
AuthorDate: Thu Dec 29 09:49:03 2022 +0100

    TAP5-2743: add "ifSet" to PerThreadValue<T>
---
 .../tapestry5/ioc/services/PerThreadValue.java     | 19 ++++++++
 .../groovy/ioc/specs/PerThreadValueSpec.groovy     | 52 +++++++++++++++++++++-
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java
index 4c1c3b73c..5b23069a7 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java
@@ -15,6 +15,7 @@
 package org.apache.tapestry5.ioc.services;
 
 import java.util.Objects;
+import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Supplier;
 
@@ -108,4 +109,22 @@ public interface PerThreadValue<T>
 
         return newValue;
     }
+
+    /**
+     * If a value is set, performs the given action with it, otherwise it
+     * does nothing.
+     *
+     * @param action performed action if a value is set
+     * @throws NullPointerException if the action is null
+     * @since 5.8.3
+     */
+    default void ifSet(Consumer<? super T> action) {
+        Objects.requireNonNull(action);
+
+        if (!exists()) {
+            return;
+        }
+
+        action.accept(get());
+    }
 }
diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/PerThreadValueSpec.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/PerThreadValueSpec.groovy
index 2afbb03e0..9bc15c6fb 100644
--- a/tapestry-ioc/src/test/groovy/ioc/specs/PerThreadValueSpec.groovy
+++ b/tapestry-ioc/src/test/groovy/ioc/specs/PerThreadValueSpec.groovy
@@ -135,6 +135,54 @@ class PerThreadValueSpec extends Specification {
   
     then:
     thrown NullPointerException
-    }
-  
+  }
+
+  def "ifSet - value is set"() {
+    given:
+    PerThreadValue<String> perThreadValue = manager.createValue();
+    perThreadValue.set("a value");
+    def hasRun = false
+
+    when:
+    perThreadValue.ifSet { hasRun = true }
+
+    then:
+    hasRun == true
+  }
+
+  def "ifSet - no value set"() {
+    given:
+    PerThreadValue<String> perThreadValue = manager.createValue();
+    def hasRun = false
+
+    when:
+    perThreadValue.ifSet { hasRun = true }
+
+    then:
+    hasRun == false
+  }
+
+  def "ifSet - action is null with no value set"() {
+    given:
+    PerThreadValue<String> perThreadValue = manager.createValue();
+
+    when:
+    perThreadValue.ifSet(null)
+
+    then:
+    thrown NullPointerException
+  }
+
+  def "ifSet - action is null with value set"() {
+    given:
+    PerThreadValue<String> perThreadValue = manager.createValue();
+    perThreadValue.set("a value")
+
+    when:
+    perThreadValue.ifSet(null)
+
+    then:
+    thrown NullPointerException
+  }
+
 }
\ No newline at end of file