You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/09/03 01:44:48 UTC

[groovy] 01/01: GROOVY-7919: add `isCase(Iterable,Object)` for `in` and `!in` support

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

emilles pushed a commit to branch GROOVY-7919
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 163cc15fbfc7a333bbf942c23d84be166660a859
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Sep 2 20:44:31 2022 -0500

    GROOVY-7919: add `isCase(Iterable,Object)` for `in` and `!in` support
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 28 ++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 8af9eb325d..f2f407f577 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -1198,6 +1198,34 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return caseValue.contains(switchValue);
     }
 
+    /**
+     * 'Case' implementation for iterable types which tests if the 'switch'
+     * operand is contained in any of the 'case' values.
+     * For example:
+     * <pre class="groovyTestCase">Iterable it = {[1,3,5].iterator()}
+     * switch( 3 ) {
+     *   case it:
+     *     assert true
+     *     break
+     *   default:
+     *     assert false
+     * }
+     *
+     * //GROOVY-7919
+     * assert 1 in it
+     * assert 2 !in it
+     * </pre>
+     *
+     * @param caseValue   the case value
+     * @param switchValue the switch value
+     * @return true if the caseValue is deemed to contain the switchValue
+     * @see #contains(Iterable,Object)
+     * @since 5.0.0
+     */
+    public static boolean isCase(Iterable caseValue, Object switchValue) {
+        return contains(caseValue, switchValue);
+    }
+
     /**
      * 'Case' implementation for maps which tests the groovy truth
      * value obtained using the 'switch' operand as key.