You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/02/13 01:05:51 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9394: Groovy could provide some minimal methods to assist with migrating between Groovy versions

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 3ab3a57  GROOVY-9394: Groovy could provide some minimal methods to assist with migrating between Groovy versions
3ab3a57 is described below

commit 3ab3a577d9d7a8f094d0989f8826df527ba434fe
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Feb 12 12:48:36 2020 +1000

    GROOVY-9394: Groovy could provide some minimal methods to assist with migrating between Groovy versions
    
    (cherry picked from commit 59dc38127bc6b2a17157533c879fd084aad628b7)
---
 src/main/groovy/groovy/lang/GroovySystem.java      | 17 ++++++++++++
 .../groovy/control/CompilerConfiguration.java      | 18 ++++++-------
 .../groovy/runtime/DefaultGroovyMethods.java       | 30 ++++++++++++++++++++--
 .../groovy/runtime/StringGroovyMethods.java        | 13 ++++++++++
 .../groovy/runtime/StringGroovyMethodsTest.java    |  8 ++++++
 .../src/main/java/groovy/test/GroovyAssert.java    |  4 ++-
 6 files changed, 78 insertions(+), 12 deletions(-)

diff --git a/src/main/groovy/groovy/lang/GroovySystem.java b/src/main/groovy/groovy/lang/GroovySystem.java
index cb5ea98..1486061 100644
--- a/src/main/groovy/groovy/lang/GroovySystem.java
+++ b/src/main/groovy/groovy/lang/GroovySystem.java
@@ -20,6 +20,7 @@ package groovy.lang;
 
 import org.apache.groovy.plugin.GroovyRunner;
 import org.apache.groovy.plugin.GroovyRunnerRegistry;
+import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
 import org.codehaus.groovy.util.ReferenceBundle;
 import org.codehaus.groovy.util.ReleaseInfo;
@@ -100,4 +101,20 @@ public final class GroovySystem {
     public static String getVersion() {
         return ReleaseInfo.getVersion();
     }
+
+    /**
+     * Returns the major and minor part of the groovy version excluding the point/patch part of the version.
+     * E.g. 2.5.7, 2.5.8-SNAPSHOT, 2.5.9-rc-1 all have 2.5 as the short version.
+     *
+     * @since 2.5.10
+     */
+    public static String getShortVersion() {
+        String full = getVersion();
+        int firstDot = full.indexOf('.');
+        int secondDot = full.indexOf('.', firstDot + 1);
+        if (secondDot < 0) {
+            throw new GroovyBugError("Unexpected version found: " + full);
+        }
+        return full.substring(0, secondDot);
+    }
 }
diff --git a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
index 59da3cf..2ce176c 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
@@ -26,7 +26,6 @@ import org.objectweb.asm.Opcodes;
 
 import java.io.File;
 import java.io.PrintWriter;
-import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
@@ -40,6 +39,7 @@ import java.util.Set;
 import java.util.StringTokenizer;
 
 import static org.apache.groovy.util.SystemUtil.getSystemPropertySafe;
+import static org.codehaus.groovy.runtime.StringGroovyMethods.isAtLeast;
 
 /**
  * Compilation control flags and coordination stuff.
@@ -544,8 +544,8 @@ public class CompilerConfiguration {
      * @param bytecodeVersion The parameter can take one of the values in {@link #ALLOWED_JDKS}.
      * @return true if the bytecode version is JDK 1.5+
      */
-    public static boolean isPostJDK5(String bytecodeVersion) {
-        return new BigDecimal(bytecodeVersion).compareTo(new BigDecimal(JDK5)) >= 0;
+    public static boolean isPostJDK5(final String bytecodeVersion) {
+        return isAtLeast(bytecodeVersion, JDK5);
     }
 
     /**
@@ -554,8 +554,8 @@ public class CompilerConfiguration {
      * @param bytecodeVersion The parameter can take one of the values in {@link #ALLOWED_JDKS}.
      * @return true if the bytecode version is JDK 1.7+
      */
-    public static boolean isPostJDK7(String bytecodeVersion) {
-        return new BigDecimal(bytecodeVersion).compareTo(new BigDecimal(JDK7)) >= 0;
+    public static boolean isPostJDK7(final String bytecodeVersion) {
+        return isAtLeast(bytecodeVersion, JDK7);
     }
 
     /**
@@ -564,8 +564,8 @@ public class CompilerConfiguration {
      * @param bytecodeVersion The parameter can take one of the values in {@link #ALLOWED_JDKS}.
      * @return true if the bytecode version is JDK 1.8+
      */
-    public static boolean isPostJDK8(String bytecodeVersion) {
-        return new BigDecimal(bytecodeVersion).compareTo(new BigDecimal(JDK8)) >= 0;
+    public static boolean isPostJDK8(final String bytecodeVersion) {
+        return isAtLeast(bytecodeVersion, JDK8);
     }
 
     /**
@@ -574,8 +574,8 @@ public class CompilerConfiguration {
      * @param bytecodeVersion The parameter can take one of the values in {@link #ALLOWED_JDKS}.
      * @return true if the bytecode version is JDK 9.0+
      */
-    public static boolean isPostJDK9(String bytecodeVersion) {
-        return new BigDecimal(bytecodeVersion).compareTo(new BigDecimal(JDK9)) >= 0;
+    public static boolean isPostJDK9(final String bytecodeVersion) {
+        return isAtLeast(bytecodeVersion, JDK9);
     }
 
     /**
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index bfa7d93..9b84b3b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -882,7 +882,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param self   a generated closure
      * @param format a format string
      * @param values values referenced by the format specifiers in the format string
-     * @since 3.0.0
+     * @since 2.5.7
      */
     public static void printf(Closure self, String format, Object[] values) {
         Object owner = getClosureOwner(self);
@@ -899,7 +899,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param self   a generated closure
      * @param format a format string
      * @param value  value referenced by the format specifier in the format string
-     * @since 3.0.0
+     * @since 2.5.7
      */
     public static void printf(Closure self, String format, Object value) {
         Object owner = getClosureOwner(self);
@@ -15211,6 +15211,32 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Compare a BigDecimal to another.
+     * A fluent api style alias for {@code compareTo}.
+     *
+     * @param left  a BigDecimal
+     * @param right a BigDecimal
+     * @return true if left is equal to or bigger than right
+     * @since 2.5.10
+     */
+    public static Boolean isAtLeast(BigDecimal left, BigDecimal right) {
+        return left.compareTo(right) >= 0;
+    }
+
+    /**
+     * Compare a BigDecimal to a String representing a number.
+     * A fluent api style alias for {@code compareTo}.
+     *
+     * @param left  a BigDecimal
+     * @param right a String representing a number
+     * @return true if left is equal to or bigger than the value represented by right
+     * @since 2.5.10
+     */
+    public static Boolean isAtLeast(BigDecimal left, String right) {
+        return isAtLeast(left, new BigDecimal(right));
+    }
+
+    /**
      * Power of a Number to a certain exponent. Called by the '**' operator.
      *
      * @param self     a Number
diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 5040411..06ff1b7 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -2998,6 +2998,19 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Compare a String representing a number to another.
+     * A fluent api style alias for {@code compareTo} on {@code BigDecimal}.
+     *
+     * @param left  a String representing a number
+     * @param right a String representing a number
+     * @return true if the value represented by left is equal to or bigger than the value represented by right
+     * @since 2.5.10
+     */
+    public static Boolean isAtLeast(String left, String right) {
+        return DefaultGroovyMethods.isAtLeast(new BigDecimal(left), right);
+    }
+
+    /**
      * Convenience method to split a CharSequence (with whitespace as delimiter).
      * Similar to tokenize, but returns an Array of String instead of a List.
      *
diff --git a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
index a298e55..3b5d154 100644
--- a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
@@ -197,6 +197,14 @@ public class StringGroovyMethodsTest extends GroovyTestCase {
         assertEquals(expectedResult, result);
     }
 
+    @Test
+    public void testisAtLeast() {
+        assertTrue(StringGroovyMethods.isAtLeast("2.1", "2.1"));
+        assertTrue(StringGroovyMethods.isAtLeast("2.1", "2.0"));
+        assertTrue(StringGroovyMethods.isAtLeast("3.0", "2.1"));
+        assertFalse(StringGroovyMethods.isAtLeast("2.5", "3.0"));
+    }
+
     private Closure<String> createClosureForFindOrFindAll() {
         return new Closure<String>(this) {
             @Override
diff --git a/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java b/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
index 917fed0..4ac357e 100644
--- a/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
+++ b/subprojects/groovy-test/src/main/java/groovy/test/GroovyAssert.java
@@ -30,6 +30,8 @@ import java.math.BigDecimal;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Logger;
 
+import static org.codehaus.groovy.runtime.DefaultGroovyMethods.isAtLeast;
+
 /**
  * <p>{@code GroovyAssert} contains a set of static assertion and test helper methods and is supposed to be a Groovy
  * extension of JUnit 4's {@link org.junit.Assert} class. In case JUnit 3 is the choice, the {@link groovy.util.GroovyTestCase}
@@ -341,7 +343,7 @@ public class GroovyAssert extends org.junit.Assert {
     public static boolean isAtLeastJdk(String specVersion) {
         boolean result = false;
         try {
-            result = new BigDecimal(System.getProperty("java.specification.version")).compareTo(new BigDecimal(specVersion)) >= 0;
+            result = isAtLeast(new BigDecimal(System.getProperty("java.specification.version")), specVersion);
         } catch (Exception ignore) {
         }
         return result;