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

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new cda6a7b939 GROOVY-12031: Provide a DGM#takeIf method
cda6a7b939 is described below

commit cda6a7b93982ac103571fa3e5f4a1b5c9b8652f5
Author: Paul King <[email protected]>
AuthorDate: Sat May 23 00:07:46 2026 +1000

    GROOVY-12031: Provide a DGM#takeIf method
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 27 ++++++++++++++++++++++
 src/test/groovy/groovy/inspect/InspectorTest.java  |  2 +-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 98c080b582..78db239c5f 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -15072,6 +15072,33 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
         }
     }
 
+    
//--------------------------------------------------------------------------
+    // takeIf
+
+    /**
+     * Returns this object if it satisfies the given predicate, or {@code null}
+     * otherwise. Useful for guarding a value through a chain, particularly 
with
+     * the Elvis or safe-navigation operators.
+     * <pre class="language-groovy groovyTestCase">
+     * assert 'hello'.takeIf{ it.startsWith('h') } == 'hello'
+     * assert 'hello'.takeIf{ it.startsWith('z') } == null
+     * assert ('input'.takeIf{ it.startsWith('http') } ?: "https://input";) == 
'https://input'
+     * assert [1, 2, 3].takeIf{ !it.isEmpty() }?.size() == 3
+     * assert [].takeIf{ !it.isEmpty() }?.size() == null
+     * </pre>
+     * The predicate is invoked with the receiver as its argument; the receiver
+     * is not used as a delegate. The receiver is passed through to the 
predicate
+     * unchanged, including when it is {@code null}.
+     *
+     * @param self      the object to test
+     * @param condition the predicate applied to {@code self}
+     * @return {@code self} if {@code condition.test(self)} is {@code true}, 
otherwise {@code null}
+     * @since 6.0.0
+     */
+    public static <T> T takeIf(T self, Predicate<? super T> condition) {
+        return condition.test(self) ? self : null;
+    }
+
     
//--------------------------------------------------------------------------
     // takeRight
 
diff --git a/src/test/groovy/groovy/inspect/InspectorTest.java 
b/src/test/groovy/groovy/inspect/InspectorTest.java
index 226f6cb88d..f848af004c 100644
--- a/src/test/groovy/groovy/inspect/InspectorTest.java
+++ b/src/test/groovy/groovy/inspect/InspectorTest.java
@@ -181,7 +181,7 @@ public class InspectorTest implements Serializable {
             "getMetaPropertyValues", "getProperties", "grep", "grep", 
"hasProperty", "identity", "inject", "inject",
             "inspect", "invokeMethod", "is", "isCase", "isNotCase", 
"iterator", "metaClass", "print", "print",
             "printf", "printf", "println", "println", "println", "putAt", 
"respondsTo", "respondsTo",
-            "setMetaClass", "split", "sprintf", "sprintf", "tap", "toString", 
"use", "use", "use", "with",
+            "setMetaClass", "split", "sprintf", "sprintf", "takeIf", "tap", 
"toString", "use", "use", "use", "with",
             "with", "withMethodClosure", "withTraits", "stream", "sleep", 
"sleep", "macro", "macro", "macro", "macro"
         };
         assertEquals(names.length, metaMethods.length, "Incorrect number of 
methods found examining: " + Arrays.stream(metaMethods)

Reply via email to