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

sunlan 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 59dc381  GROOVY-9394: Groovy could provide some minimal methods to 
assist with migrating between Groovy versions
59dc381 is described below

commit 59dc38127bc6b2a17157533c879fd084aad628b7
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
---
 src/main/java/groovy/lang/GroovySystem.java        | 17 ++++++++++++++
 .../groovy/runtime/DefaultGroovyMethods.java       | 26 ++++++++++++++++++++++
 .../groovy/runtime/StringGroovyMethods.java        | 13 +++++++++++
 .../groovy/runtime/StringGroovyMethodsTest.java    |  8 +++++++
 .../src/main/java/groovy/test/GroovyAssert.java    |  4 +++-
 5 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/src/main/java/groovy/lang/GroovySystem.java 
b/src/main/java/groovy/lang/GroovySystem.java
index cb5ea98..6727cb4 100644
--- a/src/main/java/groovy/lang/GroovySystem.java
+++ b/src/main/java/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. 3.0.0, 3.0.0-SNAPSHOT, 3.0.0-rc-1 all have 3.0 as the short 
version.
+     *
+     * @since 3.0.1
+     */
+    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/runtime/DefaultGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 2e42850..9d1f4e9 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -15643,6 +15643,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 3.0.1
+     */
+    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 3.0.1
+     */
+    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 7191e04..fb8379e 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -2524,6 +2524,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 3.0.1
+     */
+    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 e892ce4..a7e435e 100644
--- a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
@@ -231,6 +231,14 @@ public final class StringGroovyMethodsTest {
         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 cb489b9..1844761 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.test.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;

Reply via email to